Skip to content
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

feat: add support for socketTimeout in Redis #1882

Merged
merged 1 commit into from
Apr 16, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/Redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
private connectionEpoch = 0;
private retryAttempts = 0;
private manuallyClosing = false;
private socketTimeoutTimer: NodeJS.Timeout | undefined;

// Prepare autopipelines structures
private _autoPipelines = new Map();
Expand Down Expand Up @@ -523,6 +524,10 @@
if (Command.checkFlag("WILL_DISCONNECT", command.name)) {
this.manuallyClosing = true;
}

if (this.options.socketTimeout !== undefined && this.socketTimeoutTimer === undefined) {
this.setSocketTimeout();
}
}

if (command.name === "select" && isInt(command.args[0])) {
Expand All @@ -537,35 +542,52 @@
return command.promise;
}

private setSocketTimeout() {
this.socketTimeoutTimer = setTimeout(() => {
this.stream.destroy(new Error(`Socket timeout. Expecting data, but didn't receive any in ${this.options.socketTimeout}ms.`));
this.socketTimeoutTimer = undefined;
}, this.options.socketTimeout);

// this handler must run after the "data" handler in "DataHandler"
// so that `this.commandQueue.length` will be updated
this.stream.once("data", () => {
console.log('GOT DATA, CLEARING TIMER');
leibale marked this conversation as resolved.
Show resolved Hide resolved
clearTimeout(this.socketTimeoutTimer);
this.socketTimeoutTimer = undefined;
if (this.commandQueue.length === 0) return;
this.setSocketTimeout();
});
}

scanStream(options?: ScanStreamOptions) {

Check warning on line 562 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (12.x)

Member scanStream should be declared before all private instance method definitions

Check warning on line 562 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (14.x)

Member scanStream should be declared before all private instance method definitions

Check warning on line 562 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (16.x)

Member scanStream should be declared before all private instance method definitions

Check warning on line 562 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (18.x)

Member scanStream should be declared before all private instance method definitions
return this.createScanStream("scan", { options });
}

scanBufferStream(options?: ScanStreamOptions) {

Check warning on line 566 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (12.x)

Member scanBufferStream should be declared before all private instance method definitions

Check warning on line 566 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (14.x)

Member scanBufferStream should be declared before all private instance method definitions

Check warning on line 566 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (16.x)

Member scanBufferStream should be declared before all private instance method definitions

Check warning on line 566 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (18.x)

Member scanBufferStream should be declared before all private instance method definitions
return this.createScanStream("scanBuffer", { options });
}

sscanStream(key: string, options?: ScanStreamOptions) {

Check warning on line 570 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (12.x)

Member sscanStream should be declared before all private instance method definitions

Check warning on line 570 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (14.x)

Member sscanStream should be declared before all private instance method definitions

Check warning on line 570 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (16.x)

Member sscanStream should be declared before all private instance method definitions

Check warning on line 570 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (18.x)

Member sscanStream should be declared before all private instance method definitions
return this.createScanStream("sscan", { key, options });
}

sscanBufferStream(key: string, options?: ScanStreamOptions) {

Check warning on line 574 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (12.x)

Member sscanBufferStream should be declared before all private instance method definitions

Check warning on line 574 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (14.x)

Member sscanBufferStream should be declared before all private instance method definitions

Check warning on line 574 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (16.x)

Member sscanBufferStream should be declared before all private instance method definitions

Check warning on line 574 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (18.x)

Member sscanBufferStream should be declared before all private instance method definitions
return this.createScanStream("sscanBuffer", { key, options });
}

hscanStream(key: string, options?: ScanStreamOptions) {

Check warning on line 578 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (12.x)

Member hscanStream should be declared before all private instance method definitions

Check warning on line 578 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (14.x)

Member hscanStream should be declared before all private instance method definitions

Check warning on line 578 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (16.x)

Member hscanStream should be declared before all private instance method definitions

Check warning on line 578 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (18.x)

Member hscanStream should be declared before all private instance method definitions
return this.createScanStream("hscan", { key, options });
}

hscanBufferStream(key: string, options?: ScanStreamOptions) {

Check warning on line 582 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (12.x)

Member hscanBufferStream should be declared before all private instance method definitions

Check warning on line 582 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (14.x)

Member hscanBufferStream should be declared before all private instance method definitions

Check warning on line 582 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (16.x)

Member hscanBufferStream should be declared before all private instance method definitions

Check warning on line 582 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (18.x)

Member hscanBufferStream should be declared before all private instance method definitions
return this.createScanStream("hscanBuffer", { key, options });
}

zscanStream(key: string, options?: ScanStreamOptions) {

Check warning on line 586 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (12.x)

Member zscanStream should be declared before all private instance method definitions

Check warning on line 586 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (14.x)

Member zscanStream should be declared before all private instance method definitions

Check warning on line 586 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (16.x)

Member zscanStream should be declared before all private instance method definitions

Check warning on line 586 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (18.x)

Member zscanStream should be declared before all private instance method definitions
return this.createScanStream("zscan", { key, options });
}

zscanBufferStream(key: string, options?: ScanStreamOptions) {

Check warning on line 590 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (12.x)

Member zscanBufferStream should be declared before all private instance method definitions

Check warning on line 590 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (14.x)

Member zscanBufferStream should be declared before all private instance method definitions

Check warning on line 590 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (16.x)

Member zscanBufferStream should be declared before all private instance method definitions

Check warning on line 590 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (18.x)

Member zscanBufferStream should be declared before all private instance method definitions
return this.createScanStream("zscanBuffer", { key, options });
}

Expand All @@ -574,7 +596,7 @@
*
* @ignore
*/
silentEmit(eventName: string, arg?: unknown): boolean {

Check warning on line 599 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (12.x)

Member silentEmit should be declared before all private instance method definitions

Check warning on line 599 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (14.x)

Member silentEmit should be declared before all private instance method definitions

Check warning on line 599 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (16.x)

Member silentEmit should be declared before all private instance method definitions

Check warning on line 599 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (18.x)

Member silentEmit should be declared before all private instance method definitions
let error: unknown;
if (eventName === "error") {
error = arg;
Expand Down Expand Up @@ -609,7 +631,7 @@
/**
* @ignore
*/
recoverFromFatalError(

Check warning on line 634 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (12.x)

Member recoverFromFatalError should be declared before all private instance method definitions

Check warning on line 634 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (14.x)

Member recoverFromFatalError should be declared before all private instance method definitions

Check warning on line 634 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (16.x)

Member recoverFromFatalError should be declared before all private instance method definitions

Check warning on line 634 in lib/Redis.ts

View workflow job for this annotation

GitHub Actions / test / test (18.x)

Member recoverFromFatalError should be declared before all private instance method definitions
_commandError: Error,
err: Error,
options: FlushQueueOptions
Expand Down
9 changes: 9 additions & 0 deletions lib/redis/RedisOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ export interface CommonRedisOptions extends CommanderOptions {
* a "Command timed out" error will be thrown.
*/
commandTimeout?: number;

/**
* If the socket does not receive data within a set number of milliseconds:
* 1. the socket is considered "dead" and will be destroyed
* 2. the client will reject any running commands (altought they might have been processed by the server)
* 3. the reconnect strategy will kick in (depending on the configuration)
*/
socketTimeout?: number;

/**
* Enable/disable keep-alive functionality.
* @link https://nodejs.org/api/net.html#socketsetkeepaliveenable-initialdelay
Expand Down