@@ -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' ;
@@ -80,6 +80,10 @@ export interface RedisClientOptions<
80
80
* TODO
81
81
*/
82
82
commandOptions ?: CommandOptions < TYPE_MAPPING > ;
83
+ /**
84
+ * Provides a timeout in milliseconds.
85
+ */
86
+ commandTimeout ?: number ;
83
87
}
84
88
85
89
type WithCommands <
@@ -730,9 +734,34 @@ export default class RedisClient<
730
734
return Promise . reject ( new ClientOfflineError ( ) ) ;
731
735
}
732
736
737
+ let controller : AbortController ;
738
+ if ( this . _self . #options?. commandTimeout ) {
739
+ controller = new AbortController ( )
740
+ options = {
741
+ ...options ,
742
+ abortSignal : controller . signal
743
+ }
744
+ }
733
745
const promise = this . _self . #queue. addCommand < T > ( args , options ) ;
746
+
734
747
this . _self . #scheduleWrite( ) ;
735
- return promise ;
748
+ if ( ! this . _self . #options?. commandTimeout ) {
749
+ return promise ;
750
+ }
751
+
752
+ return new Promise < T > ( ( resolve , reject ) => {
753
+ const timeoutId = setTimeout ( ( ) => {
754
+ controller . abort ( ) ;
755
+ reject ( new CommandTimeoutError ( ) ) ;
756
+ } , this . _self . #options?. commandTimeout )
757
+ promise . then ( result => {
758
+ clearInterval ( timeoutId ) ;
759
+ resolve ( result )
760
+ } ) . catch ( error => {
761
+ clearInterval ( timeoutId ) ;
762
+ reject ( error )
763
+ } ) ;
764
+ } )
736
765
}
737
766
738
767
async SELECT ( db : number ) : Promise < void > {
0 commit comments