Reputation: 897
I am use mongo driver 2.2.2 to connect from my rails app. I noticed that I have too many open connections. This is how i test this:
@db = Mongo::Client.new('mongodb://user:password!@localhost:27017/db')
# nothing to do! only for test!
@db.close
and i starting both - rails app and mongodb
$ rails server
$ mongod
after this i am create single request to my app in debug mode and check mongod output:
2016-01-30T14:08:30.047+0200 [initandlisten] connection accepted from 127.0.0.1:65005 #1 (1 connection now open)
2016-01-30T14:08:30.048+0200 [conn1] end connection 127.0.0.1:65005 (0 connections now open)
2016-01-30T14:08:30.048+0200 [initandlisten] connection accepted from 127.0.0.1:65006 #2 (1 connection now open)
2016-01-30T14:08:30.051+0200 [initandlisten] connection accepted from 127.0.0.1:65007 #3 (2 connections now open)
2016-01-30T14:08:30.051+0200 [conn3] end connection 127.0.0.1:65007 (1 connection now open)
2016-01-30T14:08:30.051+0200 [initandlisten] connection accepted from 127.0.0.1:65008 #4 (2 connections now open)
2016-01-30T14:08:30.052+0200 [conn4] end connection 127.0.0.1:65008 (1 connection now open)
In my rails output i see next:
D, [2016-01-30T14:08:30.046532 #12981] DEBUG -- : MONGODB | Adding localhost:27017 to the cluster.
D, [2016-01-30T14:08:30.050656 #12981] DEBUG -- : MONGODB | Adding localhost:27017 to the cluster.
I am going and shut down mongod and check rails output again:
D, [2016-01-30T14:08:50.060270 #12981] DEBUG -- : MONGODB | IOError
D, [2016-01-30T14:09:00.065535 #12981] DEBUG -- : MONGODB | Connection refused - connect(2) for 127.0.0.1:27017
D, [2016-01-30T14:09:10.066730 #12981] DEBUG -- : MONGODB | Broken pipe
D, [2016-01-30T14:09:20.073243 #12981] DEBUG -- : MONGODB | Connection refused - connect(2) for 127.0.0.1:27017
D, [2016-01-30T14:09:30.079056 #12981] DEBUG -- : MONGODB | Broken pipe
And finally my question: how to really close the connection to mongo? What am I doing wrong?
Upvotes: 1
Views: 1491
Reputation: 21
you can use pool configuration to restrict the number of connections, like this:
@db = Mongo::Client.new('mongodb://user:password!@localhost:27017/db? maxPoolSize=10')
You can check more options on this link https://docs.mongodb.com/ruby-driver/master/tutorials/ruby-driver-create-client/
Upvotes: 2