从简单的mongodb example 的观察
https://github.com/no7dw/mongodb-example
这是最基础的连接查询。(branch master)
var MongoClient = require('mongodb').MongoClient , assert = require('assert'); // Connection URL var url = 'mongodb://localhost:27017/demo'; var findaddr = function(db, callback) { // Get the addr collection var collection = db.collection('addr'); // Find some addr collection.find({}).toArray(function(err, docs) { callback(err, docs); }); } // Use connect method to connect to the Server MongoClient.connect(url, function(err, db) { assert.equal(null, err); console.log("Connected correctly to server"); findaddr(db,function(err, docs){ if(err) console.log('we get err', err); else console.log('result:', docs); db.close(); }); });
留意到log:
2014-11-20T22:15:52.348+0800 [initandlisten] connection accepted from 127.0.0.1:50076 #39 (1 connection now open)
2014-11-20T22:15:52.384+0800 [conn39] end connection 127.0.0.1:50076 (0 connections now open)
2014-11-20T22:15:52.425+0800 [initandlisten] connection accepted from 127.0.0.1:50077 #40 (1 connection now open)
2014-11-20T22:15:52.426+0800 [initandlisten] connection accepted from 127.0.0.1:50078 #41 (2 connections now open)
2014-11-20T22:15:52.426+0800 [initandlisten] connection accepted from 127.0.0.1:50079 #42 (3 connections now open)
2014-11-20T22:15:52.427+0800 [initandlisten] connection accepted from 127.0.0.1:50080 #43 (4 connections now open)
2014-11-20T22:15:52.428+0800 [initandlisten] connection accepted from 127.0.0.1:50081 #44 (5 connections now open)
2014-11-20T22:15:52.455+0800 [conn40] end connection 127.0.0.1:50077 (4 connections now open)
2014-11-20T22:15:52.455+0800 [conn41] end connection 127.0.0.1:50078 (3 connections now open)
2014-11-20T22:15:52.455+0800 [conn42] end connection 127.0.0.1:50079 (2 connections now open)
2014-11-20T22:15:52.456+0800 [conn43] end connection 127.0.0.1:50080 (1 connection now open)
2014-11-20T22:15:52.456+0800 [conn44] end connection 127.0.0.1:50081 (0 connections now open)
原因:
默认mongodb 使用connection poolsize =5 的设置
所以把他根据业务来设置大小。(branch less-connection)
var options = { db: { native_parser: true }, server: { poolSize: 1 } } // Use connect method to connect to the Server MongoClient.connect(url, options, function(err, db) { assert.equal(null, err); console.log("Connected correctly to server"); findaddr(db,function(err, docs){ if(err) console.log('we get err', err); else console.log('result:', docs); db.close(); }); });
now see mongd log:
2014-11-20T22:21:46.243+0800 [initandlisten] connection accepted from 127.0.0.1:50157 #45 (1 connection now open) 2014-11-20T22:21:46.255+0800 [conn45] end connection 127.0.0.1:50157 (0 connections now open) 2014-11-20T22:21:46.266+0800 [initandlisten] connection accepted from 127.0.0.1:50158 #46 (1 connection now open) 2014-11-20T22:21:46.278+0800 [conn46] end connection 127.0.0.1:50158 (0 connections now open)
seems good. However, 这样设置不能根据业务压力来调整,even 有 maxPoolSize, minPoolSize :
- uri.maxPoolSize¶
The maximum number of connections in the connection pool. The default value is 100.
- uri.minPoolSize
The minimum number of connections in the connection pool. The default value is 0.
--- 答案是:
generic-pool
2014-11-20T23:35:25.954+0800 [initandlisten] connection accepted from 127.0.0.1:52277 #1 (1 connection now open)
2014-11-20T23:35:25.955+0800 [initandlisten] connection accepted from 127.0.0.1:52278 #2 (2 connections now open)
2014-11-20T23:35:35.877+0800 [clientcursormon] mem (MB) res:67 virt:837
2014-11-20T23:35:35.877+0800 [clientcursormon] mapped (incl journal view):640
2014-11-20T23:35:35.878+0800 [clientcursormon] connections:2
2014-11-20T23:35:56.021+0800 [conn2] end connection 127.0.0.1:52278 (1 connection now open)
2014-11-20T23:35:56.024+0800 [conn1] end connection 127.0.0.1:52277 (0 connections now open)
var http=require('http'), mongodb = require("mongodb"), poolModule = require('generic-pool'); var pool = poolModule.Pool({ name : 'mongodb', create : function(callback) { var server_options={'auto_reconnect':false,poolSize:1}; //http://docs.mongodb.org/manual/reference/connection-string/#uri.w var db_options={w:-1}; var mongoserver = new mongodb.Server('localhost', 27017,server_options ); var db=new mongodb.Db('addr', mongoserver, db_options); db.open(function(err,db){ if(err)return callback(err); callback(null,db); }); }, destroy : function(db) { db.close(); }, max : 10, // optional. if you set this, make sure to drain() (see step 3) // min : 2, // specifies how long a resource can stay idle in pool before being removed idleTimeoutMillis : 30000, // if true, logs via console.log - can also be a function log : false }); var server=http.createServer(function(req,res){ // acquire connection - callback function is called // once a resource becomes available pool.acquire(function(err, db) { if (err) { res.statusCode=500; res.end(JSON.stringify(err,null,2)); } else { db.collection('addr').find({}).toArray(function(err, docs) { res.end(JSON.stringify(docs,null,2)); // return object back to pool pool.release(db); }); } }); }); server.listen(8080,function(){ console.log('server listen to %d',this.address().port); }); setTimeout(function(){ for (var i = 0; i < 20; i++) { //serval second later , connection will close console.log("request #%d",i); http.get('http://localhost:8080',function(res){ console.log('request ok') }); } },2000);
//只开了5个connection ?why?
2014-11-21T00:06:04.342+0800 [initandlisten] connection accepted from 127.0.0.1:52642 #25 (1 connection now open)
2014-11-21T00:06:04.369+0800 [initandlisten] connection accepted from 127.0.0.1:52643 #26 (2 connections now open)
2014-11-21T00:06:04.370+0800 [initandlisten] connection accepted from 127.0.0.1:52644 #27 (3 connections now open)
2014-11-21T00:06:04.370+0800 [initandlisten] connection accepted from 127.0.0.1:52645 #28 (4 connections now open)
2014-11-21T00:06:04.370+0800 [initandlisten] connection accepted from 127.0.0.1:52646 #29 (5 connections now open)
2014-11-21T00:06:34.430+0800 [conn28] end connection 127.0.0.1:52645 (4 connections now open)
2014-11-21T00:06:34.436+0800 [conn29] end connection 127.0.0.1:52646 (3 connections now open)
2014-11-21T00:06:34.437+0800 [conn25] end connection 127.0.0.1:52642 (2 connections now open)
2014-11-21T00:06:34.438+0800 [conn26] end connection 127.0.0.1:52643 (1 connection now open)
2014-11-21T00:06:34.438+0800 [conn27] end connection 127.0.0.1:52644 (0 connections now open)
2014-11-21T00:08:04.517+0800 [initandlisten] connection accepted from 127.0.0.1:52669 #30 (1 connection now open)
2014-11-21T00:08:04.533+0800 [initandlisten] connection accepted from 127.0.0.1:52670 #31 (2 connections now open)
2014-11-21T00:08:04.533+0800 [initandlisten] connection accepted from 127.0.0.1:52671 #32 (3 connections now open)
2014-11-21T00:08:04.539+0800 [initandlisten] connection accepted from 127.0.0.1:52672 #33 (4 connections now open)
2014-11-21T00:08:04.539+0800 [initandlisten] connection accepted from 127.0.0.1:52673 #34 (5 connections now open)
2014-11-21T00:08:34.558+0800 [conn30] end connection 127.0.0.1:52669 (4 connections now open)
2014-11-21T00:08:34.559+0800 [conn31] end connection 127.0.0.1:52670 (3 connections now open)
2014-11-21T00:08:34.560+0800 [conn32] end connection 127.0.0.1:52671 (2 connections now open)
2014-11-21T00:08:34.562+0800 [conn33] end connection 127.0.0.1:52672 (1 connection now open)
2014-11-21T00:08:34.563+0800 [conn34] end connection 127.0.0.1:52673 (0 connections now open)
2014-11-21T00:09:36.124+0800 [clientcursormon] mem (MB) res:38 virt:1002
2014-11-21T00:09:36.124+0800 [clientcursormon] mapped (incl journal view):800
2014-11-21T00:09:36.124+0800 [clientcursormon] connections:0
2014-11-21T00:10:04.634+0800 [initandlisten] connection accepted from 127.0.0.1:52695 #35 (1 connection now open)
2014-11-21T00:10:04.636+0800 [initandlisten] connection accepted from 127.0.0.1:52697 #36 (2 connections now open)
2014-11-21T00:10:04.645+0800 [initandlisten] connection accepted from 127.0.0.1:52698 #37 (3 connections now open)
2014-11-21T00:10:04.649+0800 [initandlisten] connection accepted from 127.0.0.1:52699 #38 (4 connections now open)
2014-11-21T00:10:04.649+0800 [initandlisten] connection accepted from 127.0.0.1:52700 #39 (5 connections now open)
2014-11-21T00:10:34.686+0800 [conn36] end connection 127.0.0.1:52697 (4 connections now open)
2014-11-21T00:10:34.687+0800 [conn35] end connection 127.0.0.1:52695 (3 connections now open)
2014-11-21T00:10:34.689+0800 [conn37] end connection 127.0.0.1:52698 (2 connections now open)
2014-11-21T00:10:34.690+0800 [conn38] end connection 127.0.0.1:52699 (1 connection now open)
2014-11-21T00:10:34.691+0800 [conn39] end connection 127.0.0.1:52700 (0 connections now open)
2014-11-21T00:12:04.773+0800 [initandlisten] connection accepted from 127.0.0.1:52720 #40 (1 connection now open)
2014-11-21T00:12:04.796+0800 [initandlisten] connection accepted from 127.0.0.1:52721 #41 (2 connections now open)
2014-11-21T00:12:04.796+0800 [initandlisten] connection accepted from 127.0.0.1:52722 #42 (3 connections now open)
2014-11-21T00:12:04.797+0800 [initandlisten] connection accepted from 127.0.0.1:52723 #43 (4 connections now open)
2014-11-21T00:12:04.799+0800 [initandlisten] connection accepted from 127.0.0.1:52724 #44 (5 connections now open)
output:
request 返回是每5个返回的,其他都还在request 当中。
wade@V1088:~/projects/github/d/mongodb-example$ node genric.js
server listen to 8080
request #0
request #1
request #2
request #3
request #4
request #5
request #6
request #7
request #8
request #9
request #10
request #11
request #12
request #13
request #14
request #15
request #16
request #17
request #18
request #19
request ok
request ok
request ok
request ok
request ok//...分开时间的
request ok
request ok
request ok
request ok
request ok//...分开时间的
request ok
request ok
request ok
request ok
request ok//...分开时间的
request okrequest ok
request ok
request ok
request ok
BUT 只开了5个connection, 而非10个(max) ?why?
EOF