从简单的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
从简单的mongodb example 的观察的更多相关文章
- mongoDB介绍、安装、搭建简单的mongoDB服务器(一)
相关网站 1. http://www.mongodb.org/ 官网,可以下载安装程序,和doc,和驱动等. 2. http://www.mongoing.com/ 国内官方网站,博客,问题谈论等 ...
- C# Asp.net中简单操作MongoDB数据库(二)
C# Asp.net中简单操作MongoDB数据库(一) , mongodb数据库连接可以回顾上面的篇幅. 1.model类: public class BaseEntity { /// < ...
- C# Asp.net中简单操作MongoDB数据库(一)
需要引用MongoDB.Driver.dll.MongoDB.Driver.core.dll.MongoDB.Bson.dll三个dll. 1.数据库连接: public class MongoDb ...
- 简单封装mongodb
首先安装mongodb npm i mongodb --save 简单封装,在modules目录下新建db.js var MongoClient=require('mongodb').MongoCl ...
- CentOS7+Docker+MangoDB下部署简单的MongoDB分片集群
简单的在Docker上快速部署MongoDB分片集群 前言 文中使用的环境如下 OS:CentOS Linux release 7.5.1804 (Core) Docker:Docker versio ...
- nodejs 简单对mongodb 操作
路由到了 index.js /* * GET home page. 控制器 */ exports.index = function(req, res){ // res.render('index', ...
- window10简单安装MongoDB
文章参考 在Windows上安装MongoDB 首先,在官网下载安装包.下载地址 内容如下所示: 配置 1. 创建数据目录 E:\MongoDB\data\db 2. 配置环境变量 运行 1. 命令行 ...
- windows如何简单安装mongodb
windows如何安装mongodb 步骤: 1.下载地址 2.选择zip(解压版本) 3.压缩文件解压到 /D:盘 4.在 D:盘 下建一个 data文件夹,data下建 db文件夹: D: ...
- C#简单操作MongoDB
一 安装MongoDB 官网按需下载, 安装, 一步到位. 二 VS创建新项目 创建一个.netcore console项目, 然后nuget安装驱动MongoDB.Driver 三 建立连接 在Pr ...
随机推荐
- Annotation方式配置AOP
package com.xk.spring.kp04_aop.aop.s02_annotation; public interface IStudentService { public void sa ...
- Neo4J 教程
好文转载: W3C: https://www.w3cschool.cn/neo4j/neo4j_cypher_api_example.html neo4j图数据库入门: http://blog.csd ...
- java接口和抽象类的区别和作用(功能、用途、好处)
Java接口: 总结了4点关于JAVA中接口存在的意义: 1.重要性:在Java语言中, abstract class 和interface 是支持抽象类定义的两种机制.正是由于这两种机制的存在,才赋 ...
- C++标准库头文件名字和C语言头文件名字的区别
1.C++版本的C标准库头文件,一般是cname,而C语言头文件一般是name.h 2.命名为cname的头文件中定义的名字都是从std中来的,而如果是name.h则不是这样的. 3.与是用name. ...
- Linux学习 :移植U-boot_2012.04.01到JZ2440开发板
一.下载U-boot源码:ftp://ftp.denx.de/pub/u-boot/ 二.uboot的启动过程: 部分硬件初始化——>加载完整uboot到RAM——>跳转到第二阶段入口开始 ...
- ylz 开发学习笔记一(注意事项)
Eclipse快捷键 断点 F6执行下一步 等等 C+S+R 搜索文件名字 C+鼠标移动左击 跳入类文件 jsp 文件的断点是使用 debugger 之后再chrom 用F10单步调 ...
- L327 找灵魂伴侣
Looking for the Perfect Partner I'm sure we all remember a time when we fell in love. For some it wa ...
- vnode的挂载和更新流程 -- 简介.
来源 vnode原理 diff图解 <div id="app"> {{someVar}} </div> <script type="text ...
- ob 函数的使用
ob 函数的使用1. 页面静态化 $id = isset($_GET['id'])?$_GET['id']-0:0; $filename = "html/".date(" ...
- python如何进行内存管理的
python引用了一个内存池(memory pool)机制,即pymalloc机制(malloc:n,分配内存),用于管理对小块的申请和释放.