求助各位,netstat 的ESTABLISHED连接数 一直增长
发布于 7 年前 作者 HelloKevinTian 9026 次浏览 来自 问答

最近项目用了redis + generic_pool + twemproxy + nodejs搭建服务,但是客户端连接来了之后,tcp的ESTABLISHED数量猛涨,涨到60000+文件句柄数就不够用了,redis也就无法访问了,请问大家如何解决这个问题。我目前怀疑是redis的连接没有及时释放掉。谢谢大家,本人初学乍道,请大家帮帮忙~

tcp汇总:(重启node服务可以解决问题,现在我的方案时每日都重启(很笨的方案)) TIME_WAIT 47 FIN_WAIT1 5 FIN_WAIT2 20 ESTABLISHED 50861

redis部分代码:(因为用了redis代理twemproxy,所以configfile.proxy为true)

var redis = require('redis');
var generic_pool = require('generic-pool');
 
//redis启动函数
function initRedisPool(config_file) {
    for (var item in config_file) {
        var _pool = createRedisPool(item, config_file[item]);
        pools[item] = _pool;
    }
}
 
//redis实例创建函数
function createRedisPool(db_name,config_file){
    var opts = {
        "no_ready_check" : config_file.proxy
    };
    return generic_pool.Pool({
        name : db_name,
        dbIndex : 0,
        create : function(cb) {
            var client = redis.createClient(config_file.port, config_file.hostname, opts);
            client.on('error', function(err) {
                console.error('error at connect redis: %s', err.stack);
            });
            cb(null, client);
        },
        destroy : function(client) {
            if (!config_file.proxy) {
                client.quit();
            }
        },
        max : config_file.max,
        //  min      : 2,
        idleTimeoutMillis : config_file.idleTimeoutMillis,
        // if true, logs via console.log - can also be a function
        log : false
    });
}
//redis实例使用函数
function execute(db_name, execb) {
    ++redis_count_statistics;
    var pool = pools[db_name];
    pool.acquire(function(err, client) {
        var release = function() { pool.release(client); };
        if (err) {
            console.error('error at execute command: %s', err.stack);
            release();
        } else {
            execb(client, release);
        }
    }, 0);
}

//redis使用部分代码:
redis_pools.execute('pool_1',function(client, release){
    client.hset(h_activity,activity_key,JSON.stringify(activity),function (err, reply){
        if(err){
            console.error(err);
        }
        release();
    });
});
8 回复

为什么怀疑一定是redis的问题呢?服务中还有其他用到tcp啊

netstat -antp 看一下ESTABLISHED是谁连的谁,定位一下那些链接有问题不就行了

@jiangliqin netstat -ant查了 是redis的端口

@jizhuofeng 定位到了,就是redis的端口,连接redis的tcp持续增长,没有释放掉

@HelloKevinTian 我们系统也是高并发频繁用redis没有出现没有释放的问题啊

        destroy : function(client) {
            if (!config_file.proxy) {
                client.quit();
            }
        },

因为 configfile.proxytrue 导致 pool 里的 destroy 没有调用 client.quit 从而没有真正断开与 redis 的连接?

@zbinlin 看来就是这里了,我已经改好测试了。连接数确实没有暴增了,谢谢,当时写这行代码时不记得是什么思路了。

问题已解决,谢谢大家!

回到顶部