npm模块generic-pool源码分析

About generic-pool

github地址:https://github.com/coopernurse/node-pool
description:
Generic resource pool. Can be used to reuse or throttle expensive resources such as database connections.
由此可以看出,该模块是一般的资源池,用来重复使用昂贵的资源,比如数据库连接等。

使用这个模块

Step 1:创建资源池

//Step 1 - Create pool using a factory object

// Create a MySQL connection pool with,创建Mysql连接
// a max of 10 connections, a min of 2, and a 30 second max idle time
var Pool = require('generic-pool').Pool;
var mysql = require('mysql'); // v2.10.x

var pool = new Pool({
    name     : 'mysql',
    create   : function(callback) {
        //创建连接
        var c = mysql.createConnection({
                user: 'scott',
                password: 'tiger',
                database:'mydb'
        })

        // parameter order: err, resource
        callback(null, c);
    },
    //销毁连接
    destroy  : function(client) { client.end(); },
    //最大并发数
    max      : 10,
    // optional. if you set this, make sure to drain() (see step 3),如果设置了并发数,记得在不使用时执行drain()方法,(英文意思为排干,流尽)
    //最小并发数
    min      : 2,
    // specifies how long a resource can stay idle in pool before being removed,idle:资源超时时间
    idleTimeoutMillis : 30000,
     // if true, logs via console.log - can also be a function
     //是否打印日志,log可以为一个函数,执行函数log
    log : true 
});

Step 3 - Drain pool during shutdown (optional)

If you are shutting down a long-lived process, you may notice that node fails to exit for 30 seconds or so. This is a side effect of the idleTimeoutMillis behavior -- the pool has a setTimeout() call registered that is in the event loop queue, so node won't terminate until all resources have timed out, and the pool stops trying to manage them.

This behavior will be more problematic when you set factory.min > 0, as the pool will never become empty, and the setTimeout calls will never end.

In these cases, use the pool.drain() function. This sets the pool into a "draining" state which will gracefully wait until all idle resources have timed out. For example, you can call:

// Only call this once in your application -- at the point you want
// to shutdown and stop using this pool.
pool.drain(function() {
    pool.destroyAllNow();
});

Step 2:使用资源池(acquire,release)

//Step 2 - Use pool in your code to acquire/release resources

// acquire connection - callback function is called
// once a resource becomes available
pool.acquire(function(err, client) {
   
    if (err) {
        // handle error - this is generally the err from your
        // factory.create function
    }
    else {
        client.query("select * from foo", [], function() {
   
            // return object back to pool,将资源对象返回给资源池
            pool.release(client);
        });
    }
});

Step 3:把资源池的水排干,可选

在一个已经运行了很长的进程中,如果你想把资源池关闭了,你可能会发现,有节点会在30s后才执行关闭动作。这个是由设置的idleTimeoutMillis 引发的副作用:资源池存在一个setTimeout()调用,该调用被添加到事件循环队列中,所以该节点不能被关闭,直到所有的资源都超时,此时,资源池

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值