我有一个有效的服务器配置,其中无法访问redis,但服务器可以正常运行(我只是在无法找到redis时删除功能).
但是,我无法很好地管理连接错误.我想知道连接错误何时失败并在这种情况下关闭客户端.
我发现连接重试永远不会停止.实际上吞下了quit() - "排队等待下一次服务器连接". - 被叫时
在没有建立连接的情况下,有没有办法杀死客户端?
var redis = require("redis"),
client = redis.createClient();
client.on("error", function(err) {
logme.error("Bonk. The worker framework cannot connect to redis, which might be ok on a dev server!");
logme.error("Resque error : "+err);
client.quit();
});
client.on("idle", function(err) {
logme.error("Redis queue is idle. Shutting down...");
});
client.on("end", function(err) {
logme.error("Redis is shutting down. This might be ok if you chose not to run it in your dev environment");
});
client.on("ready", function(err) {
logme.info("Redis up! Now connecting the worker queue client...");
});
Run Code Online (Sandbox Code Playgroud)
有趣的是,"结束"事件会被释放.为什么?
控制客户端重新连接行为的正确方法是使用retry_strategy。
断开连接后,redisClient 将尝试按照默认行为重新连接。通过在创建客户端时提供 retry_strategy 可以覆盖默认行为。
文档中一些细粒度控件的示例用法。
var client = redis.createClient({
retry_strategy: function (options) {
if (options.error && options.error.code === 'ECONNREFUSED') {
// End reconnecting on a specific error and flush all commands with
// a individual error
return new Error('The server refused the connection');
}
if (options.total_retry_time > 1000 * 60 * 60) {
// End reconnecting after a specific timeout and flush all commands
// with a individual error
return new Error('Retry time exhausted');
}
if (options.attempt > 10) {
// End reconnecting with built in error
return undefined;
}
// reconnect after
return Math.min(options.attempt * 100, 3000);
}
});
Run Code Online (Sandbox Code Playgroud)
参考:https : //www.npmjs.com/package/redis#options-object-properties
为了在连接丢失时杀死客户端,我们可以使用以下 retry_strategy。
var client = redis.createClient({
retry_strategy: function (options) {
return undefined;
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3487 次 |
| 最近记录: |