nodejs의 제3자 모듈generic-pool 모듈에 대한 개선

3424 단어
generic-pool 모듈은 nodejs의 제3자 모듈로 일반적인 연결 탱크 모듈을 제공하는 역할을 한다.generic-pool을 통해 tcp 연결 탱크나 mysql 데이터베이스 연결 탱크 등에 대한 관리를 실현할 수 있다.github의 주소는 다음과 같습니다.https://github.com/coopernurse/node-pool
그 기본적인 사용 방법은 작자가 준 예를 참조할 수 있다.
// Create a MySQL connection pool with
// a max of 10 connections and a 30 second max idle time
var poolModule = require('generic-pool');
var pool = poolModule.Pool({
    name     : 'mysql',
    create   : function(callback) {
        var Client = require('mysql').Client;
        var c = new Client();
        c.user     = 'scott';
        c.password = 'tiger';
        c.database = 'mydb';
        c.connect();

        // parameter order: err, resource
        // new in 1.0.6
        callback(null, c);
    },
    destroy  : function(client) { client.end(); },
    max      : 10,
    idleTimeoutMillis : 30000,
    log : true
});

// acquire connection - callback function is called
// once a resource becomes available
pool.acquire(function(err, client) {
    client.query("select * from foo", [], function() {
        // return object back to pool
        pool.release(client);
    });
});
        。      。
name        ,        。
create       ,                  ,  c     ,  c        。callback(null,c)     ,               ,          ,            。                 error  ,  callback     :
c.on("error",function(e){callback(e,c)});
callback(null,c);
destroy        ,            。
max            。
idleTimeoutMills       ,                  ,      。
log         。
accquire        。       ,               。
    generc-pool    ,       ,   js    。         。           ,  A   B    ,      poolAB  ,    A              ,  poolAB          ,  generic-pool              ,                          。                。             ,           。
function removeNotWritable() {
     var toKeep = [],
         i,
         al;
     // Go through the available (idle) items,
     // check if they are not writeable
     for (i = 0, al = availableObjects.length; i < al; i += 1) {
       if (availableObjects[i]["obj"]["writable"] != false &&  availableObjects[i]["obj"]["readable"] != false ) {
         // Client is writeable, so keep it.
         toKeep.push(availableObjects[i]);
       } else {
         // The client is not writeable, call it's destroyer.
   //      log("removeIdle() destroying obj - now:" + now + "not writeable");
         me.destroy(availableObjects[i].obj);
       }
     }
 
     // Replace the available items with the ones to keep.
     availableObjects = toKeep;
     al = availableObjects.length;
 }
                    ,        ,    ,        ,     。 dispense()                        。
 function dispense() {
       ......
    if (waitingCount > 0) {
      removeNotWritable();
      if (availableObjects.length > 0) {

좋은 웹페이지 즐겨찾기