嘿,
我无法使该模块重新连接Redis。
进行此配置:
const client = redis.createClient({
no_ready_check: false,
enable_offline_queue: true,
retry_unfulfilled_commands: true,
socket_keepalive: true,
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;
}
console.log('here');
// reconnect after
return 100;
},
host: process.env.REDIS_HOST || '127.0.0.1',
port: 6379
});
当我停止本地redis时,我看到here
,但是随后我再次重新启动它并等待任何时间,任何后续的redis调用都会给我:
{ AbortError: EXEC can't be processed. The connection is already closed.
at handle_offline_command (/Users/kstoney/git/autotrader/platform-alert-forwarder/node_modules/redis/index.js:851:15)
at RedisClient.internal_send_command (/Users/kstoney/git/autotrader/platform-alert-forwarder/node_modules/redis/index.js:885:9)
at Multi.exec_transaction (/Users/kstoney/git/autotrader/platform-alert-forwarder/node_modules/redis/lib/multi.js:115:18)
at Object.RedisCache.self.set (/Users/kstoney/git/autotrader/platform-alert-forwarder/lib/caches/redis.js:41:8)
at Object.sendMessage [as handle] (/Users/kstoney/git/autotrader/platform-alert-forwarder/lib/forwarders/slack.js:197:28)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
command: 'EXEC',
code: 'NR_CLOSED',
errors:
[ { AbortError: SET can't be processed. The connection is already closed.
有任何想法吗? 我需要一定的容忍度来重做redis提示。
没有一些选项(如脱机排队)的相同问题,我的配置如下:
var client = redis.createClient({
port: process.env.REDIS_PORT,
host: process.env.REDIS_HOST,
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
logger.debug('[Redis Strategy] - Connection Refused options:', options);
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
logger.debug('[Redis Strategy] - Time exhausted options:', options);
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);
}
});
client.on('connect', function () {
console.log('Redis client connected');
});
client.on('error', function (err) {
console.log('Something went wrong ' + err);
});
哇,不敢相信我没有注意到这种行为。
我使用https://github.com/Unitech/pm2运行我的应用程序,一旦发生故障,该应用程序将重新启动应用程序,因此这种情况已经持续了相当长的一段时间,没有引起人们的注意。
很棒的收获!
还有其他方法可以完成重新连接,直到修复为止? (我看到最新版本是2年前)
是的,我们改用了ioredis哈哈
@Stono我同意,我今天要切换到ioredis 。 它更新的频率更高,具有更多的功能,并且非常有趣。
谢谢,听起来确实更好,我不熟悉这个库。
Unfourtantley对于这个当前项目,我将无法切换,但是很高兴知道以后的工作。
最有用的评论
是的,我们改用了ioredis哈哈