node-mongo-pool | |
| mongo-pool.js |
Module dependencies
|
var EventEmitter = require('events').EventEmitter;
var mongodb = require('mongodb');
|
MongoPool
|
var MongoPool = exports.MongoPool = function MongoPool(options) {
this._pool = [];
this._queue = [];
this._closing = false;
options = options || {};
options.host = options.host || '127.0.0.1';
options.port = options.port || 27017;
options.db = options.db || 'mongo_pool_test';
options.poolSize = options.poolSize > 0 ? options.poolSize : 5;
this.on('release', function(client) {
if (this._closing) {
client.close();
} else if (this._queue.length > 0) {
this._queue.shift().call(this, client);
} else if (this._pool.indexOf(client) === -1) {
this._pool.push(client);
}
});
var self = this;
for (var i = 0; i < options.poolSize; i++) {
this.createClient(options.host, options.port, options.db)
.open(function(error, client) {
if (error) throw error;
self.release(client);
});
}
}
|
Inherits EventEmitter
|
MongoPool.prototype.__proto__ = EventEmitter.prototype;
|
Create and return a mongodb.Db instance
param: String host Hostname of database server param: Number | String port Port of database server param: String db Name of database return: Object
|
MongoPool.prototype.createClient = function(host, port, db) {
var client = new mongodb.Db(db, new mongodb.Server(host, port, {auto_reconnect: true}));
return client;
};
|
Get a client object from pool or put the callback function into queue
|
MongoPool.prototype.getClient = function(callback) {
if (this._pool.length > 0) {
callback.call(this, this._pool.shift());
} else {
this._queue.push(callback);
}
};
|
Get a collection object
param: String name Name of the collection param: Function callback Callback function to receive the collection object: function(error, collection) {}
|
MongoPool.prototype.getCollection = function(name, callback) {
this.getClient(function(client) {
client.collection(name, callback);
});
};
|
Release a mongodb client to the connection pool,
return true if the releasing process is handled by MongoPool .
|
MongoPool.prototype.release = function(client) {
if (client instanceof mongodb.Db) {
return this.emit('release', client);
} else if (client instanceof mongodb.Collection) {
return this.emit('release', client.db);
}
return false;
}
|
Close all db connections
|
MongoPool.prototype.destory = function() {
this._closing = true;
this._pool.forEach(function(client) {
client.close();
});
};
|