@@ -19,7 +19,7 @@ export class ConnectionManager extends EventEmitter {
1919 private _connect : typeof connect ;
2020 private _defaultPrefetch : number ;
2121 private _channelStore : ChannelStore ;
22- private _isSIGTERMReceived : boolean ;
22+ private _closingServer : boolean ;
2323
2424 constructor ( config ?: RabbitMQConfigConnection ) {
2525 super ( ) ;
@@ -28,6 +28,7 @@ export class ConnectionManager extends EventEmitter {
2828 this . _connection = null ;
2929 this . _isConnecting = false ;
3030 this . _isConnected = false ;
31+ this . _closingServer = false ;
3132 this . _options = Object . assign ( { } , config ) ;
3233 this . _options . retry = Object . assign ( { delay : 5000 , maximum_attempts : - 1 } , this . _options . retry ) ;
3334
@@ -48,9 +49,9 @@ export class ConnectionManager extends EventEmitter {
4849
4950 // Will block new connection if SIGTERM is received
5051 /* istanbul ignore next */
51- process . once ( 'SIGTERM' , ( ) => this . _isSIGTERMReceived = true ) ;
52+ process . once ( 'SIGTERM' , ( ) => this . _closingServer = true ) ;
5253 /* istanbul ignore next */
53- process . once ( 'SIGINT' , ( ) => this . _isSIGTERMReceived = true ) ;
54+ process . once ( 'SIGINT' , ( ) => this . _closingServer = true ) ;
5455
5556 this . setDefaultPrefetch ( this . _options . default_prefetch ) ;
5657
@@ -88,13 +89,15 @@ export class ConnectionManager extends EventEmitter {
8889 return Observable . of ( null )
8990 . flatMap ( ( ) => {
9091 debug ( 'try to open connection ...' ) ;
91- debug ( this . _options . retry . delay ) ;
92+ debug ( `Retry delay: ${ this . _options . retry . delay } ` ) ;
9293 return Observable . fromPromise ( this . _connect ( this . _uri ) ) ;
9394 } )
9495 . retryWhen ( errors => {
9596 errors . forEach ( err => debug ( err . message , err . stack ) ) ;
9697 return errors
98+ . scan ( attempts => attempts + 1 , 0 )
9799 . delay ( this . _options . retry . delay )
100+ . takeWhile ( ( attempts ) => attempts < this . _options . retry . maximum_attempts && ! this . _closingServer )
98101 . take ( this . _options . retry . maximum_attempts )
99102 . concat ( Observable . throw ( new Error ( 'Retry limit exceeded' ) ) )
100103 } ) ;
@@ -105,11 +108,8 @@ export class ConnectionManager extends EventEmitter {
105108 return Observable . of ( null ) ;
106109 }
107110
108- /* istanbul ignore next */
109- if ( this . _isSIGTERMReceived ) {
110- return Observable . of ( null ) ;
111- }
112111
112+ this . _closingServer = false ;
113113 this . _isConnecting = true ;
114114
115115 debug ( 'Connecting' , this . _uri ) ;
@@ -138,6 +138,7 @@ export class ConnectionManager extends EventEmitter {
138138
139139 close ( ) : Observable < void > {
140140 this . _isConnected = false ;
141+ this . _closingServer = true ;
141142 return Observable . fromPromise ( this . _connection . close ( ) ) ;
142143 }
143144
0 commit comments