dongmen1925 2015-05-06 20:04
浏览 62
已采纳

Go Lang和Labix mgo-在后续请求后获得EOF

I have a web process, similar to

func main() {
    // mgo
    mongoDatabase, err := mgopath.Connect(envMongoPath)
    if err != nil {
        log.Fatal(err)
    }

    r := mux.NewRouter()
    // ....
    r.HandleFunc("/apps/{app:.+}", stuffHandler(mongoDatabase)).Methods("GET")
    http.Handle("/", r)

    listen := fmt.Sprintf("%s:%s", host, port)
    log.Fatal(http.ListenAndServe(listen, nil))
}

while mgopath.Connect looks like

func Connect(mongoPath string) (*mgo.Database, error) {
    dbConfig, err := url.Parse(mongoPath)
    if err != nil {
        return nil, err
    }

    log.Printf("Connecting to %s", dbConfig.Host)
    sess, err := mgo.Dial(dbConfig.Host)
    if err != nil {
        return nil, err
    }

    dbName := dbConfig.Path
    log.Printf("Using database %s", dbName)
    if len(dbName) < 2 {
        return nil, errors.New("No database name specified.")
    }

    dbName = dbConfig.Path[1:len(dbConfig.Path)]
    return sess.DB(dbName), err
}

Somewhere down the road:

c := database.C("stuff")
err = c.Find(bson.M{"id": id}).One(&item) // First ~1-2 minutes work as expected, then I receive EOFs
// c.Insert(), c.Update(), c.All()...

The problem is, that after a couple of minutes, all queries to mongodb return the error EOF. I have to restart the process to get things working again. Experienced both issues on Mac and (more frequently) on Windows. MongoDB runs inside of Docker, which in turn runs inside of boot2docker. VM Port forwarding have been done (that's why queries work for a certain amount of time).

Does mgo require me to dial every time I'm doing a query? Is there some timeout I don't know about?

  • 写回答

2条回答 默认 最新

  • 普通网友 2015-05-14 11:09
    关注

    The answer can be found here: https://groups.google.com/forum/#!topic/mgo-users/XM0rc6p-V-8

    There are two easy ways to get rid of the error:

    1) Call Refresh on the session, which makes it discard (or put back in the pool, if the connection is good) the connection it's holding, and pick a new one when necessary.

    2) Instead of using a single session, use many by calling session.Copy when you need a new session, and then call session.Close when you're done with it. This will also mean you're using multiple connections to the database, when necessary.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛