I have a Docker cluster running 10 web services (of the same kind). They are all using MongoDB, among other things for data persistence.
This is the code that gets called from main()
, when the service is booting up:
// Init establishes a connection with MongoDB instance.
func Init(mongoURL string) *mgo.Session {
mongo, err := mgo.Dial(mongoURL)
misc.PanicIf(err)
// make sure we are strongly consistent
mongo.SetMode(mgo.Strong, true)
// update global state
db = mongo
Entries = db.DB("").C("entries")
Channels = db.DB("").C("channels")
Settings = db.DB("").C("settings")
Metadata = db.DB("").C("metadata")
// only use this on first load, to confirm the settings are there
// every refresh should be done via UpdateGlobalSettings (thread-safe)
GlobalSettings = &GlobalSettingsStruct{}
GlobalSettings.Init()
return mongo
}
So basically the API and the workers just use global variables such as Entries
, Settings
, etc.
After running for a while, the service stopped working properly. Every mongo action, such as Entries.find(...)
returns an err of: Closed Explicitly
.
What does that mean? Should I be refreshing the mongoDB connection periodically or should I make it close and re-establish connection with each request?
The app is performance oriented, so despite mongo connection being down, everything is still up and running as everything operates on in-memory or cluster cache. I don't want to do something stupid that delays processing.