2

目前使用 node.js (Native MongoDB) 驱动程序使用 Mongodb,我知道可以设置池连接计数,但无论观察到相同的行为。

有问题的代码:

(function init(){
    db.connect("mongodb://localhost/test",function(err, database){

    });
})();

这是来自服务器的日志:

Thu Dec 06 20:19:32 [initandlisten] connection accepted from 127.0.0.1:58663 #6
(1 connection now open)
Thu Dec 06 20:19:32 [initandlisten] connection accepted from 127.0.0.1:58664 #7
(2 connections now open)
Thu Dec 06 20:19:32 [initandlisten] connection accepted from 127.0.0.1:58665 #8
(3 connections now open)
Thu Dec 06 20:19:32 [initandlisten] connection accepted from 127.0.0.1:58666 #9
(4 connections now open)
Thu Dec 06 20:19:32 [initandlisten] connection accepted from 127.0.0.1:58667 #10
 (5 connections now open)
Thu Dec 06 20:19:36 [conn9] end connection 127.0.0.1:58666 (4 connections now op
en)
Thu Dec 06 20:19:36 [conn10] end connection 127.0.0.1:58667 (4 connections now o
pen)
Thu Dec 06 20:19:36 [conn8] end connection 127.0.0.1:58665 (4 connections now op
en)
Thu Dec 06 20:19:36 [conn6] end connection 127.0.0.1:58663 (4 connections now op
en)
Thu Dec 06 20:19:36 [conn7] end connection 127.0.0.1:58664 (4 connections now op
en)

连接显然正在关闭,但即使连接正确增加,当前打开的连接计数也不会减少。预期行为?

PS。请不要使用 mongodb 的 nodejs 包装器来回答。

4

1 回答 1

1

这是 MongoDB 计数器的问题。数据库在关闭所有连接(多个关闭信号)时异步工作,并且它发生得如此之快以至于它无法跟上更新计数器的速度。浏览 MongoDB 源代码后,您可以看到消息输出远不及计数器更新。

低于类似的输出,但精度高达 1ms,这更好地说明了 IMO 的问题。

Mon Dec  2 17:42:09.059 [conn675] end connection 127.0.0.1:65198 (9 connections now open)
Mon Dec  2 17:42:09.059 [conn676] end connection 127.0.0.1:65199 (8 connections now open)
Mon Dec  2 17:42:09.059 [conn677] end connection 127.0.0.1:65200 (8 connections now open)
Mon Dec  2 17:42:09.059 [conn678] end connection 127.0.0.1:65201 (7 connections now open)
Mon Dec  2 17:42:09.059 [conn679] end connection 127.0.0.1:65202 (6 connections now open)
Mon Dec  2 17:42:09.059 [conn680] end connection 127.0.0.1:65203 (5 connections now open)
Mon Dec  2 17:42:09.059 [conn681] end connection 127.0.0.1:65204 (4 connections now open)
Mon Dec  2 17:42:09.059 [conn682] end connection 127.0.0.1:65205 (3 connections now open)
Mon Dec  2 17:42:09.059 [conn683] end connection 127.0.0.1:65206 (2 connections now open)
Mon Dec  2 17:42:09.059 [conn684] end connection 127.0.0.1:65207 (2 connections now open)
于 2013-12-10T09:06:49.833 回答