mgo在多次session.Copy()之后存在连接泄漏。

huangapple go评论78阅读模式
英文:

mgo connection leak after multiple session.Copy()

问题

我正在使用Go编写一个使用MongoDB作为数据库和mgo作为驱动程序的REST API。
对于路由器,我正在使用一个自定义的路由器:pi

在程序启动时,我创建一个主mgo.Session,然后对于每个处理的请求,我复制主会话,并在完成后关闭它。

但是,当同时处理多个请求时,我观察到即使我关闭了每个复制的mgo.Session,MongoDB连接仍然保持打开状态。

这是lsof命令的输出示例:

milano-ru 18790 neel_v  118u  IPv4 34115804      0t0     TCP localhost:44238->localhost:27017 (ESTABLISHED)
milano-ru 18790 neel_v  119u  IPv4 34115812      0t0     TCP localhost:44241->localhost:27017 (ESTABLISHED)
milano-ru 18790 neel_v  120u  IPv4 34115813      0t0     TCP localhost:44242->localhost:27017 (ESTABLISHED)
milano-ru 18790 neel_v  121u  IPv4 34115814      0t0     TCP localhost:44243->localhost:27017 (ESTABLISHED)
milano-ru 18790 neel_v  122u  IPv4 34115815      0t0     TCP localhost:44244->localhost:27017 (ESTABLISHED)

因此,当我的应用程序运行了几个小时后,它停止工作,因为它无法打开另一个MongoDB连接,达到了限制(1024)。

我已经阅读了关于增加MongoDB的ulimit的内容,但这真的是一个解决方案吗?

英文:

I'm writing a REST API in Go using MongoDB as a database and mgo as a driver.
For the router, I'm using a custome one: pi.

At the program start up, I create a master mgo.Session and then, for each request handled, I copy the master session and closes it when I'm done.

But, when multiple requests are handled concurrently, I observe that MongoDB connection are still open even though I closed every mgo.Session copied.

Here is a sample of the output of lsof command:

milano-ru 18790 neel_v  118u  IPv4 34115804      0t0     TCP localhost:44238->localhost:27017 (ESTABLISHED)
milano-ru 18790 neel_v  119u  IPv4 34115812      0t0     TCP localhost:44241->localhost:27017 (ESTABLISHED)
milano-ru 18790 neel_v  120u  IPv4 34115813      0t0     TCP localhost:44242->localhost:27017 (ESTABLISHED)
milano-ru 18790 neel_v  121u  IPv4 34115814      0t0     TCP localhost:44243->localhost:27017 (ESTABLISHED)
milano-ru 18790 neel_v  122u  IPv4 34115815      0t0     TCP localhost:44244->localhost:27017 (ESTABLISHED)

So, after a couple of hours my application is running, it stops working since it cannot opens another MongoDB connection because a limit is reached (1024).

I've read about increasing ulimit of MongoDB, but is it really a solution?

答案1

得分: 1

我有点沮丧和宽慰,但是在我发布问题后,我立刻找到了答案...

在检查了mgo会话之后,我发现Dial设置了默认的maxPoolSize为4096...在尝试减小连接池大小后,问题得到解决。

英文:

I'm kind of upset and relieved at the same time, but I found my answer right after I posted my question...

After checking on the mgo sessions, I can see that Dial set, by default a maxPoolSize of 4096... After trying to reduce the pool size it solved my problem.

huangapple
  • 本文由 发表于 2015年4月13日 22:13:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/29607533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定