Search code examples
mongodbhttpsessiongomgo

mgo connection leak after multiple session.Copy()


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?


Solution

  • 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.