对于我们的Node.js到Redis连接,我们使用Redis模块。
偶尔,我们会收到以下错误,
message: read ECONNRESET, stack: Error: read ECONNRESET
at exports._errnoException (util.js:1020:11)
at TCP.onread (net.js:568:26)
Redis的设置是,内部网中的一个redis服务器和两个节点js服务器,每个服务器在DMZ中运行8个PM2实例。节点服务器和Redis服务器之间有防火墙。
节点版本- 6.11.2 REDIS版本- 3.2.9 PM2版本- 2.4.6
我们也做了TCP转储。TCP转储显示了一些RST/ACK数据包。TCP转储
在nodeJS中,我们正在创建单个redis连接,并试图对所有请求使用相同的redis连接。
const Redis = require('redis');
const Config = require('../../config');
const Logger = require('../helpers/logger');
const redisClient = (function() {
// Start with a fake client so that we have a client that works
// even when Redis server is down
let client = {
get: function(key, callback) {
callback(null, null);
},
setex: function(key, time, value) {
Logger.info('Value:',value);
// Do nothing in particular
}
};
// Attempt to create a new instance of an actual redis client
const redisConfig = Config.get('/redis');
const tempClient = Redis.createClient(redisConfig.port,redisConfig.host, {
//eslint-disable-next-line
// console.log('[redis]','Creating the retry strategy');
retry_strategy: function(options) { //eslint-disable-line
//eslint-disable-next-line
console.log('[redis]','Creating the retry strategy');
if (options.error && options.error.code === 'ECONNREFUSED') {
// End reconnecting on a specific error and flush all commands with
// a individual error
//eslint-disable-next-line
console.log('[redis,error]','Connection refused error');
return new Error('The server refused the connection');
}
if (options.error && options.error.code === 'NR_CLOSED') {
//eslint-disable-next-line
console.log('[redis,error]','Connection closed error');
return new Error('The server closed the connection');
}
if (options.attempt > 5) {
// End reconnecting with built in error
//eslint-disable-next-line
console.log('Exceeded attempts');
return undefined;
}
if (options.total_retry_time > 1000 * 60 * 60) {
// End reconnecting after a specific timeout and flush all commands
// with a individual error
//eslint-disable-next-line
console.log('Retrial time:' + options.total_retry_time);
return 1000;
}
// reconnect after
return Math.min(options.attempt * 100, 3000);
}
});
// Set the "client" variable to the actual redis client instance
// once a connection is established with the Redis server
tempClient.on('ready', () => {
client = tempClient;
});
tempClient.on('error', (error) => {
Logger.info(['redis','error'],'Redis client error:', error);
if (error.code === 'NR_CLOSED') {
tempClient.end();
client = Redis.createClient(redisConfig.port,redisConfig.host, {
retry_strategy: function(options) { //eslint-disable-line
if (options.error && options.error.code === 'NR_CLOSED') {
Logger.info(['redis','error'],'Connection closed error');
return new Error('The server refused the connection');
}
}
});
}
});
/**
* Get a redis client
* @return {Object} client - eventually a proper redis client object
* (if redis is up) or a fake client object (if redis is down)
*/
const getClient = function() {
Logger.info('Getting the client ' + client);
return client;
};
return {
getClient: getClient
};
}());
module.exports = redisClient;
我们想知道到底是什么导致了连接问题,以及为什么和什么是解决办法。
发布于 2018-10-30 00:32:36
我在函数上也有同样的问题。问题是,最新版本的Redis让这些连接永远存在。如果调用Redis的进程在调用Redis后不久结束,就像我们使用云函数一样,您必须将timeout
设置设置为与默认的0不同的设置。
您可以通过更改名为redis.conf
的Redis配置文件中的超时值来做到这一点。
文档说,您还可以在不重新启动实例的情况下设置超时设置,如果您的Redis实例受到保护,并且因此无法通过redis-cli运行类似CONFIG SET timeout 15
的内容,这是不可能的,因为您将得到以下错误:
(错误)错误未知命令
CONFIG
https://stackoverflow.com/questions/48002036
复制相似问题