@@ -4,7 +4,7 @@ import { BasicAuth, CredentialsError, CredentialsProvider, StreamingCredentialsP
4
4
import RedisCommandsQueue , { CommandOptions } from './commands-queue' ;
5
5
import { EventEmitter } from 'node:events' ;
6
6
import { attachConfig , functionArgumentsPrefix , getTransformReply , scriptArgumentsPrefix } from '../commander' ;
7
- import { ClientClosedError , ClientOfflineError , DisconnectsClientError , WatchError } from '../errors' ;
7
+ import { ClientClosedError , ClientOfflineError , CommandTimeoutError , DisconnectsClientError , WatchError } from '../errors' ;
8
8
import { URL } from 'node:url' ;
9
9
import { TcpSocketConnectOpts } from 'node:net' ;
10
10
import { PUBSUB_TYPE , PubSubType , PubSubListener , PubSubTypeListeners , ChannelListeners } from './pub-sub' ;
@@ -144,6 +144,10 @@ export interface RedisClientOptions<
144
144
* Tag to append to library name that is sent to the Redis server
145
145
*/
146
146
clientInfoTag ?: string ;
147
+ /**
148
+ * Provides a timeout in milliseconds.
149
+ */
150
+ commandTimeout ?: number ;
147
151
}
148
152
149
153
type WithCommands <
@@ -889,9 +893,34 @@ export default class RedisClient<
889
893
return Promise . reject ( new ClientOfflineError ( ) ) ;
890
894
}
891
895
896
+ let controller : AbortController ;
897
+ if ( this . _self . #options?. commandTimeout ) {
898
+ controller = new AbortController ( )
899
+ options = {
900
+ ...options ,
901
+ abortSignal : controller . signal
902
+ }
903
+ }
892
904
const promise = this . _self . #queue. addCommand < T > ( args , options ) ;
905
+
893
906
this . _self . #scheduleWrite( ) ;
894
- return promise ;
907
+ if ( ! this . _self . #options?. commandTimeout ) {
908
+ return promise ;
909
+ }
910
+
911
+ return new Promise < T > ( ( resolve , reject ) => {
912
+ const timeoutId = setTimeout ( ( ) => {
913
+ controller . abort ( ) ;
914
+ reject ( new CommandTimeoutError ( ) ) ;
915
+ } , this . _self . #options?. commandTimeout )
916
+ promise . then ( result => {
917
+ clearInterval ( timeoutId ) ;
918
+ resolve ( result )
919
+ } ) . catch ( error => {
920
+ clearInterval ( timeoutId ) ;
921
+ reject ( error )
922
+ } ) ;
923
+ } )
895
924
}
896
925
897
926
async SELECT ( db : number ) : Promise < void > {
0 commit comments