Skip to content

Introduce socketTimeout option #2965

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

Merged
merged 1 commit into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion docs/client-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
| socket.family | `0` | IP Stack version (one of `4 \| 6 \| 0`) |
| socket.path | | Path to the UNIX Socket |
| socket.connectTimeout | `5000` | Connection timeout (in milliseconds) |
| socket.socketTimeout | | The maximum duration (in milliseconds) that the socket can remain idle (i.e., with no data sent or received) before being automatically closed |
| socket.noDelay | `true` | Toggle [`Nagle's algorithm`](https://nodejs.org/api/net.html#net_socket_setnodelay_nodelay) |
| socket.keepAlive | `true` | Toggle [`keep-alive`](https://nodejs.org/api/net.html#socketsetkeepaliveenable-initialdelay) functionality |
| socket.keepAliveInitialDelay | `5000` | If set to a positive number, it sets the initial delay before the first keepalive probe is sent on an idle socket |
Expand Down Expand Up @@ -40,7 +41,12 @@ By default the strategy uses exponential backoff, but it can be overwritten like
```javascript
createClient({
socket: {
reconnectStrategy: retries => {
reconnectStrategy: (retries, cause) => {
// By default, do not reconnect on socket timeout.
if (cause instanceof SocketTimeoutError) {
return false;
}

// Generate a random jitter between 0 – 200 ms:
const jitter = Math.floor(Math.random() * 200);
// Delay is an exponential back off, (times^2) * 50 ms, with a maximum value of 2000 ms:
Expand Down
69 changes: 65 additions & 4 deletions packages/client/lib/client/socket.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { strict as assert } from 'node:assert';
import { spy } from 'sinon';
import { once } from 'node:events';
import RedisSocket, { RedisSocketOptions } from './socket';
import testUtils, { GLOBAL } from '../test-utils';
import { setTimeout } from 'timers/promises';

describe('Socket', () => {
function createSocket(options: RedisSocketOptions): RedisSocket {
const socket = new RedisSocket(
() => Promise.resolve(),
options
);
const socket = new RedisSocket(() => Promise.resolve(), options);

socket.on('error', () => {
// ignore errors
Expand Down Expand Up @@ -84,4 +83,66 @@ describe('Socket', () => {
assert.equal(socket.isOpen, false);
});
});

describe('socketTimeout', () => {
const timeout = 50;
testUtils.testWithClient(
'should timeout with positive socketTimeout values',
async client => {
let timedOut = false;

assert.equal(client.isReady, true, 'client.isReady');
assert.equal(client.isOpen, true, 'client.isOpen');

client.on('error', err => {
assert.equal(
err.message,
`Socket timeout timeout. Expecting data, but didn't receive any in ${timeout}ms.`
);

assert.equal(client.isReady, false, 'client.isReady');

// This is actually a bug with the onSocketError implementation,
// the client should be closed before the error is emitted
process.nextTick(() => {
assert.equal(client.isOpen, false, 'client.isOpen');
});

timedOut = true;
});
await setTimeout(timeout * 2);
if (!timedOut) assert.fail('Should have timed out by now');
},
{
...GLOBAL.SERVERS.OPEN,
clientOptions: {
socket: {
socketTimeout: timeout
}
}
}
);

testUtils.testWithClient(
'should not timeout with undefined socketTimeout',
async client => {

assert.equal(client.isReady, true, 'client.isReady');
assert.equal(client.isOpen, true, 'client.isOpen');

client.on('error', err => {
assert.fail('Should not have timed out or errored in any way');
});
await setTimeout(100);
},
{
...GLOBAL.SERVERS.OPEN,
clientOptions: {
socket: {
socketTimeout: undefined
}
}
}
);
});
});
24 changes: 21 additions & 3 deletions packages/client/lib/client/socket.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EventEmitter, once } from 'node:events';
import net from 'node:net';
import tls from 'node:tls';
import { ConnectionTimeoutError, ClientClosedError, SocketClosedUnexpectedlyError, ReconnectStrategyError } from '../errors';
import { ConnectionTimeoutError, ClientClosedError, SocketClosedUnexpectedlyError, ReconnectStrategyError, SocketTimeoutError } from '../errors';
import { setTimeout } from 'node:timers/promises';
import { RedisArgument } from '../RESP/types';

Expand All @@ -23,6 +23,10 @@ type RedisSocketOptionsCommon = {
* 3. `(retries: number, cause: Error) => false | number | Error` -> `number` is the same as configuring a `number` directly, `Error` is the same as `false`, but with a custom error.
*/
reconnectStrategy?: false | number | ReconnectStrategyFunction;
/**
* The timeout (in milliseconds) after which the socket will be closed. `undefined` means no timeout.
*/
socketTimeout?: number;
}

type RedisTcpOptions = RedisSocketOptionsCommon & NetOptions & Omit<
Expand Down Expand Up @@ -55,6 +59,7 @@ export default class RedisSocket extends EventEmitter {
readonly #connectTimeout;
readonly #reconnectStrategy;
readonly #socketFactory;
readonly #socketTimeout;

#socket?: net.Socket | tls.TLSSocket;

Expand Down Expand Up @@ -85,6 +90,7 @@ export default class RedisSocket extends EventEmitter {
this.#connectTimeout = options?.connectTimeout ?? 5000;
this.#reconnectStrategy = this.#createReconnectStrategy(options);
this.#socketFactory = this.#createSocketFactory(options);
this.#socketTimeout = options?.socketTimeout;
}

#createReconnectStrategy(options?: RedisSocketOptions): ReconnectStrategyFunction {
Expand All @@ -103,7 +109,7 @@ export default class RedisSocket extends EventEmitter {
return retryIn;
} catch (err) {
this.emit('error', err);
return this.defaultReconnectStrategy(retries);
return this.defaultReconnectStrategy(retries, err);
}
};
}
Expand Down Expand Up @@ -253,6 +259,13 @@ export default class RedisSocket extends EventEmitter {
socket.removeListener('timeout', onTimeout);
}

if (this.#socketTimeout) {
socket.once('timeout', () => {
socket.destroy(new SocketTimeoutError(this.#socketTimeout!));
});
socket.setTimeout(this.#socketTimeout);
}

socket
.once('error', err => this.#onSocketError(err))
.once('close', hadError => {
Expand Down Expand Up @@ -341,7 +354,12 @@ export default class RedisSocket extends EventEmitter {
this.#socket?.unref();
}

defaultReconnectStrategy(retries: number) {
defaultReconnectStrategy(retries: number, cause: unknown) {
// By default, do not reconnect on socket timeout.
if (cause instanceof SocketTimeoutError) {
return false;
}

// Generate a random jitter between 0 – 200 ms:
const jitter = Math.floor(Math.random() * 200);
// Delay is an exponential back off, (times^2) * 50 ms, with a maximum value of 2000 ms:
Expand Down
6 changes: 6 additions & 0 deletions packages/client/lib/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ export class ConnectionTimeoutError extends Error {
}
}

export class SocketTimeoutError extends Error {
constructor(timeout: number) {
super(`Socket timeout timeout. Expecting data, but didn't receive any in ${timeout}ms.`);
}
}

export class ClientClosedError extends Error {
constructor() {
super('The client is closed');
Expand Down
Loading