dongrandi8411 2015-09-20 18:19
浏览 189

Go中的“明确关闭” mgo MongoDB

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.

  • 写回答

3条回答 默认 最新

  • duanhe6718 2015-09-20 20:23
    关注

    First of all try enabling debug mode in mgo in order to get some more info about what's happening.

    I suppose the server is dropping the connection after some inactivity time. In any case, usually the approach is to do a mgo Dial and then Copy the connection at the beginning of every request handling(using a middleware).

    Pietro

    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探