Skip to content
Closed
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
1 change: 1 addition & 0 deletions packages/client/lib/client/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe('Client', () => {
},
username: 'user',
password: 'secret',
commandTimeout: 1000,
database: 0,
credentialsProvider: {
type: 'async-credentials-provider',
Expand Down
30 changes: 29 additions & 1 deletion packages/client/lib/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export interface RedisClientOptions<
* TODO
*/
commandOptions?: CommandOptions<TYPE_MAPPING>;
/**
* Provides a timeout in milliseconds.
*/
commandTimeout?: number;
}

type WithCommands<
Expand Down Expand Up @@ -678,19 +682,43 @@ export default class RedisClient<
/**
* @internal
*/
async _executeCommand(
async _executeCommand<T>(
command: Command,
parser: CommandParser,
commandOptions: CommandOptions<TYPE_MAPPING> | undefined,
transformReply: TransformReply | undefined,
) {
// If no timeout is set, execute the command normally
if (!this._self.#options?.commandTimeout) {
const reply = await this.sendCommand(parser.redisArgs, commandOptions);


if (transformReply) {
return transformReply(reply, parser.preserve, commandOptions?.typeMapping);
}

return reply;
}

// Wrap the command execution in a timeout
return new Promise<T>((resolve, reject) => {
const timeoutId = setTimeout(() => {
reject(new Error(`Command timed out after ${this._self.#options?.commandTimeout}ms`));
}, this._self.#options?.commandTimeout);

this.sendCommand(parser.redisArgs, commandOptions)
.then(reply => {
clearTimeout(timeoutId); // Clear the timeout if the command succeeds
const transformedReply = transformReply
? transformReply(reply, parser.preserve, commandOptions?.typeMapping)
: reply;
resolve(transformedReply);
})
.catch(err => {
clearTimeout(timeoutId); // Clear the timeout if the command fails
reject(err);
});
});
}

/**
Expand Down