-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
socket.reconnectStrategy is not applied #1985
Comments
import { createClient } from '@node-redis/client';
console.time('reconnectStrategy');
(async () => {
try {
const client = createClient({
socket: {
host: '127.0.0.1',
port: 6379,
connectTimeout: 3000,
reconnectStrategy() {
console.timeLog('reconnectStrategy', 'reconnectStrategy');
return 3000;
}
}
}).on('connect', () => {
console.timeLog('reconnectStrategy', 'connect');
}).on('ready', () => {
console.timeLog('reconnectStrategy', 'ready');
}).on('reconnecting', () => {
console.timeLog('reconnectStrategy', 'reconnecting');
}).on('error', error => {
console.timeLog('reconnectStrategy', 'error');
console.error(`Redis client error: ${JSON.stringify(error)}`);
});
await client.connect();
} catch (err) {
console.error(`Service start error`, err);
}
})(); I've just tested it locally, and the code above tries to reconnect to redis every 3 seconds. |
Hi leibale, thanks for trying out. ...
reconnectStrategy: 29.181s error
Redis client error: {}
reconnectStrategy: 29.182s reconnecting
reconnectStrategy: 29.184s connect
reconnectStrategy: 29.185s ready
reconnectStrategy: 29.197s error
Redis client error: {}
reconnectStrategy: 29.198s reconnecting
reconnectStrategy: 29.200s connect
reconnectStrategy: 29.201s ready
reconnectStrategy: 29.213s error
Redis client error: {}
reconnectStrategy: 29.215s reconnecting
reconnectStrategy: 29.216s connect
reconnectStrategy: 29.217s ready
reconnectStrategy: 29.232s error
Redis client error: {}
reconnectStrategy: 29.233s reconnecting
...
... Any idea? |
please run |
@micodix "it works on my machine" :P |
Ok, I was able to find the reason for the different behavior. Basically, it's the type of connection error. In my test setup, I connect from my local development machine to another server running the Redis server in a Docker container. If I stop the container, I get a |
I have the same issue.
and socket.#connect gets called immediately,
then socket.#retryConnection gets called with retries reset to 0,
then socket.#createSocket gets called to repeat the error again. This is running into a constant loop, and in my case starved my entire node, and I had to kill the node to get it out of the bad condition. What bothers me most was that the server was definitely gone, and yet, the socket fired, 'connect' and 'ready' events, meaning the #createSocket seemed to have succeeded after the connectEvent fired. In my case, both the redis client and the redis server were running as k8s services in the same namespace of a cluster. Anyway, this is a showstopper for us to migrate to v4. |
We've been encountering similar problems and on investigation I have found that:
If the client service tries to use the connection after this an error |
@leibale i just tested 4.1.0 and it seems this issue still persists, reconnecting still doesn't work like it used to like in v3. after "Socket closed unexpectedly", it just doesn't reconnect without restarting the client application. We simulate this by restarting redis (docker). |
@rpong this does not reproduce the issue (I ran import { createClient } from '@redis/client';
const client = createClient();
client.on('error', err => console.error('client err', err));
await client.connect();
setInterval(async () => {
try {
console.log(await client.ping());
} catch (err) {
console.error('command error', err);
}
}, 1000); |
Thanks @leibale , it turns out we needed this before "await client.connect();"
All working now. Thank you, we can finally upgrade to v4. |
Hi,
I have a TypeScript application in which I am using the new
socket.reconnectStrategy
option described in https://github.com/redis/node-redis/blob/master/docs/client-configuration.md#reconnect-strategy.However, this does not seem to be applied. After stopping the Redis server, the
reconnecting
event is still fired within the default delay of 500ms. But according to my configuration, this should happen after 3000ms.My code:
Environment:
The text was updated successfully, but these errors were encountered: