Skip to content

Commit b76035b

Browse files
committed
fix redis#1598 fix redis#2276 - add pingInterval to client config
1 parent 2a5dc75 commit b76035b

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

docs/client-configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
| readonly | `false` | Connect in [`READONLY`](https://redis.io/commands/readonly) mode |
2626
| legacyMode | `false` | Maintain some backwards compatibility (see the [Migration Guide](./v3-to-v4.md)) |
2727
| isolationPoolOptions | | See the [Isolated Execution Guide](./isolated-execution.md) |
28+
| pingInterval | | Send `PING` command on interval (in ms). Useful with "[Azure Cache for Redis](https://learn.microsoft.com/en-us/azure/azure-cache-for-redis/cache-best-practices-connection#idle-timeout)" |
2829

2930
## Reconnect Strategy
3031

packages/client/lib/client/index.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -862,4 +862,18 @@ describe('Client', () => {
862862
client.unref();
863863
client.ref();
864864
}, GLOBAL.SERVERS.OPEN);
865+
866+
describe.only('', () => {
867+
testUtils.testWithClient('pingInterval', async client => {
868+
assert.deepEqual(
869+
await once(client, 'ping-interval'),
870+
['PONG']
871+
);
872+
}, {
873+
...GLOBAL.SERVERS.OPEN,
874+
clientOptions: {
875+
pingInterval: 1
876+
}
877+
});
878+
});
865879
});

packages/client/lib/client/index.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export interface RedisClientOptions<
3131
readonly?: boolean;
3232
legacyMode?: boolean;
3333
isolationPoolOptions?: PoolOptions;
34+
pingInterval?: number;
3435
}
3536

3637
type WithCommands = {
@@ -281,7 +282,10 @@ export default class RedisClient<
281282
this.#queue.flushAll(err);
282283
}
283284
})
284-
.on('connect', () => this.emit('connect'))
285+
.on('connect', () => {
286+
this.emit('connect');
287+
this.#setPingTimer();
288+
})
285289
.on('ready', () => {
286290
this.emit('ready');
287291
this.#tick();
@@ -348,6 +352,22 @@ export default class RedisClient<
348352
(...args: Array<unknown>): void => (this as any).sendCommand(name, ...args);
349353
}
350354

355+
#pingTimer?: NodeJS.Timer;
356+
357+
#setPingTimer(): void {
358+
if (!this.#options?.pingInterval || !this.#socket.isOpen) return;
359+
clearTimeout(this.#pingTimer);
360+
361+
this.#pingTimer = setTimeout(() => {
362+
if (!this.#socket.isOpen) return;
363+
364+
(this as unknown as RedisClientType<M, F, S>).ping()
365+
.then(reply => this.emit('ping-interval', reply))
366+
.catch(err => this.emit('error', err))
367+
.finally(() => this.#setPingTimer());
368+
}, this.#options.pingInterval);
369+
}
370+
351371
duplicate(overrides?: Partial<RedisClientOptions<M, F, S>>): RedisClientType<M, F, S> {
352372
return new (Object.getPrototypeOf(this).constructor)({
353373
...this.#options,

0 commit comments

Comments
 (0)