Reconnect strategy not being used and ready event fired without being ready.
See original GitHub issueI’ve recently upgraded to node redis 4.1.0 and I’m having issues with reconnectStrategy and connection handling in general.
Logs from reconnectstrategy are never printed. When I start with redis server down. I still get the ready event triggered. A bit later I get an error event and it immediately “connects” (triggering ready again); And this just continues; The 5 second reconnect timer is not used.
When redis is online when I start the application and I later turn it off: it seems to go even faster. It does not wait long between the ready event and an error.
I’m initializing redis like so:
const options: RedisClientOptions<RedisModules, RedisFunctions, S> = {
username: this.username,
password: this.password,
socket: {
host: this.host,
port: this.port,
reconnectStrategy: (attempts) => {
console.log(`Redis reconnecting attempt ${attempts}`);
this.connected = false;
if (attempts == 1) {
console.log(`${this.constructor.name} failed to connect to ${this.host}:${this.port}. Reconnecting...`);
}
return 5000;
},
},
scripts: scripts,
};
const redisClient = createClient(options);
redisClient.on('ready', () => {
console.log(`${this.constructor.name} connected to ${this.host}:${this.port}`);
});
redisClient.on('error', (err) => {
console.log('error');
return;
});
redisClient.on('reconnecting', () => {
return;
});
redisClient.on('end', () => {
return;
});
redisClient.on('connect', () => {
return;
});
redisClient.connect();
Environment:
- Node.js Version: v14.16.0
- Node Redis Version: 4.1.0
- Platform: Mac OS 12.3.1
Issue Analytics
- State:
- Created a year ago
- Reactions:4
- Comments:8
Top Results From Across the Web
The Proper Way To Connect Redis and Node.js
Some events will be fired according to the state of the connection. It' s better to listen them to take actions. Because this...
Read more >Lettuce Reference Guide
Redis connections are designed to be long-lived and thread-safe, and if the connection is lost will reconnect until close() is called. Pending ...
Read more >Setting time interval in HTML5 server sent events
With server sent events it is always for the browser to reconnect & the frequency is determined by the retry interval. actually, a...
Read more >How Do You Deal With Difficult Employees? - SHRM
Or the worker whose manager never helped set her up for success or who put her in a no-win situation? Is it the...
Read more >TIP 57 Trauma-Informed Care in Behavioral Health Services
in the public domain and may be reproduced or copied without permission from SAMHSA ... Strategy #2: Use Trauma-Informed Principles in Strategic Planning...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Problem: The reconnect strategy behavior is not consistent in handling all error events (as in the previous version)Problem: The specified reconnect strategy is not being invoked initially after the socket is closed unexpectedlyProblem: Even more alarming is that the client goes into an infinite loop reconnect death spiral if I artificially manufacture a disconnect by temporarily disabling my Ethernet adapterSo to me sounds like reconnectstrategy should be incorporated into this error flow as well. No?
EDIT: Anyway I did more testing and it seems that literally just changing the port causes the error to go from ECONNREFUSED to ECONNRESET. So I disabled my local redis service and stopped kubernetes portforwarding. So neither of those ports have anything running on them.
When I run the same piece of code you provided just adding
localhostas host and either 6379 or 7000 as port. I get ECONNREFUSED on 6379 and ECONNRESET on 7000I started investigating and found out that there is something called afs-fileserver ruinning on my PC on port 7000. On a mac it is Airplay Receiver apparently… I disabled it and now I get ECONNREFUSED and it uses reconnectstrategy.
However, that makes me think that node-redis does not handle other kinds of disconnects properly? When can
ECONNRESET/Socket closed unexpectedlyhappen? Should I implement ways to recover from that?