noobie2023
noobie2023

Reputation: 783

Golang: RabbitMQ receiver + concurrent map + http server

TL;DR

What can I do to make two services (rabbitMQ consumer + HTTP server) share the same map?

More info

I'm new to Golang. Here's what I'm trying to achieve:

I have a RabbitMQ consumer receiving some json-format messages and store them into a concurrent map. On the other hand, I need an HTTP server that sends data from the concurrent map whenever a GET request arrives.

I kinda know that I need the"net/http" package for the HTTP server and the rabbitMQ client package.

However, I'm not sure how these two services can share the same map. Could anyone please offer some idea? Thank you in advance!

EDIT

One possible solution I can think of is to replace the concurrent map with Redis. So the running consumer will send the data to Redis server whenever a message arrives and then the http server will serve GET request from the data in Redis. But is there a better way to achieve my goal without adding this extra layer (Redis)?

Upvotes: 2

Views: 816

Answers (1)

blackgreen
blackgreen

Reputation: 44665

Assuming that your two "services" live inside the same Go program, dependency injection. You can define a type that wraps your map (or provides equivalent functionality), instantiate it when your application starts, and inject it into both the HTTP handler and the MQ consumer.

The following code is meant to illustrate the concept:

package mymap

// imports

type MyMap struct {
   // fields
}
package main

// imports

func main() {
   ...

   // instantiate the shared map object
   sharedMap := &MyMap{ /* init fields as appropriate */ }

   mqconsumer := &mqpkg.Consumer{
       SharedMap: sharedMap // inject the map obj into the mq consumer
       // ...
   }
   // start the mq consumer

   // inject the map obj into the http handler
   http.HandleFunc("/foo", handlerWithMap(sharedMap))

   log.Fatal(http.ListenAndServe(":8080", nil))
}


func handlerWithMap(mymap *mymap.MyMap) http.HandlerFunc {
   return func(w http.ResponseWriter, r *http.Request) {
       // here the http handler is able to access the shared map object
   }
}

With that said, unless your application has particular requirements, I would recommend to implement your own synchronized map. This isn't too difficult to accomplish with the sync package. The disadvantage of using third-party libraries is that you lose type safety, because their signatures must be designed to accept and return interface{}'s.

Upvotes: 1

Related Questions