@@ -164,7 +164,7 @@ private void Init()
164
164
if ( HeartbeatInterval != null )
165
165
{
166
166
heartbeatTimer = new Timer ( SendHeartbeat , null ,
167
- TimeSpan . FromMilliseconds ( 0 ) , HeartbeatInterval . Value ) ;
167
+ TimeSpan . FromMilliseconds ( 0 ) , HeartbeatInterval . GetValueOrDefault ( ) ) ;
168
168
}
169
169
170
170
Interlocked . CompareExchange ( ref lastHeartbeatTicks , DateTime . UtcNow . Ticks , lastHeartbeatTicks ) ;
@@ -178,7 +178,7 @@ void SendHeartbeat(object state)
178
178
if ( currentStatus != Status . Started )
179
179
return ;
180
180
181
- if ( DateTime . UtcNow - new DateTime ( lastHeartbeatTicks ) < HeartbeatInterval . Value )
181
+ if ( DateTime . UtcNow - new DateTime ( lastHeartbeatTicks ) < HeartbeatInterval . GetValueOrDefault ( ) )
182
182
return ;
183
183
184
184
OnHeartbeatSent ? . Invoke ( ) ;
@@ -229,97 +229,95 @@ private void RunLoop()
229
229
//RESET
230
230
while ( Interlocked . CompareExchange ( ref status , 0 , 0 ) == Status . Started )
231
231
{
232
- using ( var redis = ClientsManager . GetReadOnlyClient ( ) )
233
- {
234
- masterClient = redis ;
232
+ using var redis = ClientsManager . GetReadOnlyClient ( ) ;
233
+ masterClient = redis ;
235
234
236
- //Record that we had a good run...
237
- Interlocked . CompareExchange ( ref noOfContinuousErrors , 0 , noOfContinuousErrors ) ;
235
+ //Record that we had a good run...
236
+ Interlocked . CompareExchange ( ref noOfContinuousErrors , 0 , noOfContinuousErrors ) ;
238
237
239
- using ( var subscription = redis . CreateSubscription ( ) )
240
- {
241
- subscription . OnUnSubscribe = HandleUnSubscribe ;
238
+ using var subscription = redis . CreateSubscription ( ) ;
239
+ subscription . OnUnSubscribe = HandleUnSubscribe ;
242
240
243
- if ( OnMessageBytes != null )
244
- {
245
- bool IsCtrlMessage ( byte [ ] msg )
246
- {
247
- if ( msg . Length < 4 )
248
- return false ;
249
- return msg [ 0 ] == 'C' && msg [ 1 ] == 'T' && msg [ 0 ] == 'R' && msg [ 0 ] == 'L' ;
250
- }
241
+ if ( OnMessageBytes != null )
242
+ {
243
+ bool IsCtrlMessage ( byte [ ] msg )
244
+ {
245
+ if ( msg . Length < 4 )
246
+ return false ;
247
+ return msg [ 0 ] == 'C' && msg [ 1 ] == 'T' && msg [ 0 ] == 'R' && msg [ 0 ] == 'L' ;
248
+ }
251
249
252
- ( ( RedisSubscription ) subscription ) . OnMessageBytes = ( channel , msg ) => {
253
- if ( IsCtrlMessage ( msg ) )
254
- return ;
250
+ ( ( RedisSubscription ) subscription ) . OnMessageBytes = ( channel , msg ) => {
251
+ if ( IsCtrlMessage ( msg ) )
252
+ return ;
255
253
256
- OnMessageBytes ( channel , msg ) ;
257
- } ;
258
- }
254
+ OnMessageBytes ( channel , msg ) ;
255
+ } ;
256
+ }
259
257
260
- subscription . OnMessage = ( channel , msg ) =>
261
- {
262
- if ( string . IsNullOrEmpty ( msg ) )
263
- return ;
258
+ subscription . OnMessage = ( channel , msg ) =>
259
+ {
260
+ if ( string . IsNullOrEmpty ( msg ) )
261
+ return ;
264
262
265
- var ctrlMsg = msg . LeftPart ( ':' ) ;
266
- if ( ctrlMsg == ControlCommand . Control )
267
- {
268
- var op = Interlocked . CompareExchange ( ref doOperation , Operation . NoOp , doOperation ) ;
263
+ var ctrlMsg = msg . LeftPart ( ':' ) ;
264
+ if ( ctrlMsg == ControlCommand . Control )
265
+ {
266
+ var op = Interlocked . CompareExchange ( ref doOperation , Operation . NoOp , doOperation ) ;
269
267
270
- var msgType = msg . IndexOf ( ':' ) >= 0
271
- ? msg . RightPart ( ':' )
272
- : null ;
268
+ var msgType = msg . IndexOf ( ':' ) >= 0
269
+ ? msg . RightPart ( ':' )
270
+ : null ;
273
271
274
- OnControlCommand ? . Invoke ( msgType ?? Operation . GetName ( op ) ) ;
272
+ OnControlCommand ? . Invoke ( msgType ?? Operation . GetName ( op ) ) ;
275
273
276
- switch ( op )
274
+ switch ( op )
275
+ {
276
+ case Operation . Stop :
277
+ if ( Log . IsDebugEnabled )
278
+ Log . Debug ( "Stop Command Issued" ) ;
279
+
280
+ Interlocked . CompareExchange ( ref status , Status . Stopping , Status . Started ) ;
281
+ try
277
282
{
278
- case Operation . Stop :
279
- if ( Log . IsDebugEnabled )
280
- Log . Debug ( "Stop Command Issued" ) ;
281
-
282
- Interlocked . CompareExchange ( ref status , Status . Stopping , Status . Started ) ;
283
- try
284
- {
285
- if ( Log . IsDebugEnabled )
286
- Log . Debug ( "UnSubscribe From All Channels..." ) ;
287
-
288
- subscription . UnSubscribeFromAllChannels ( ) ; //Un block thread.
289
- }
290
- finally
291
- {
292
- Interlocked . CompareExchange ( ref status , Status . Stopped , Status . Stopping ) ;
293
- }
294
- return ;
295
-
296
- case Operation . Reset :
297
- subscription . UnSubscribeFromAllChannels ( ) ; //Un block thread.
298
- return ;
299
- }
283
+ if ( Log . IsDebugEnabled )
284
+ Log . Debug ( "UnSubscribe From All Channels..." ) ;
300
285
301
- switch ( msgType )
286
+ // ReSharper disable once AccessToDisposedClosure
287
+ subscription . UnSubscribeFromAllChannels ( ) ; //Un block thread.
288
+ }
289
+ finally
302
290
{
303
- case ControlCommand . Pulse :
304
- Pulse ( ) ;
305
- break ;
291
+ Interlocked . CompareExchange ( ref status , Status . Stopped , Status . Stopping ) ;
306
292
}
307
- }
308
- else
309
- {
310
- OnMessage ( channel , msg ) ;
311
- }
312
- } ;
313
-
314
- //blocks thread
315
- if ( ChannelsMatching != null && ChannelsMatching . Length > 0 )
316
- subscription . SubscribeToChannelsMatching ( ChannelsMatching ) ;
317
- else
318
- subscription . SubscribeToChannels ( Channels ) ;
319
-
320
- masterClient = null ;
293
+ return ;
294
+
295
+ case Operation . Reset :
296
+ // ReSharper disable once AccessToDisposedClosure
297
+ subscription . UnSubscribeFromAllChannels ( ) ; //Un block thread.
298
+ return ;
299
+ }
300
+
301
+ switch ( msgType )
302
+ {
303
+ case ControlCommand . Pulse :
304
+ Pulse ( ) ;
305
+ break ;
306
+ }
321
307
}
322
- }
308
+ else
309
+ {
310
+ OnMessage ( channel , msg ) ;
311
+ }
312
+ } ;
313
+
314
+ //blocks thread
315
+ if ( ChannelsMatching != null && ChannelsMatching . Length > 0 )
316
+ subscription . SubscribeToChannelsMatching ( ChannelsMatching ) ;
317
+ else
318
+ subscription . SubscribeToChannels ( Channels ) ;
319
+
320
+ masterClient = null ;
323
321
}
324
322
325
323
OnStop ? . Invoke ( ) ;
@@ -363,7 +361,7 @@ private void Stop(bool shouldRestart)
363
361
if ( Log . IsDebugEnabled )
364
362
Log . Debug ( "Stopping RedisPubSubServer..." ) ;
365
363
366
- //Unblock current bgthread by issuing StopCommand
364
+ //Unblock current bg thread by issuing StopCommand
367
365
SendControlCommand ( Operation . Stop ) ;
368
366
}
369
367
}
@@ -382,12 +380,10 @@ private void NotifyAllSubscribers(string commandType=null)
382
380
383
381
try
384
382
{
385
- using ( var redis = ClientsManager . GetClient ( ) )
383
+ using var redis = ClientsManager . GetClient ( ) ;
384
+ foreach ( var channel in Channels )
386
385
{
387
- foreach ( var channel in Channels )
388
- {
389
- redis . PublishMessage ( channel , msg ) ;
390
- }
386
+ redis . PublishMessage ( channel , msg ) ;
391
387
}
392
388
}
393
389
catch ( Exception ex )
@@ -406,13 +402,11 @@ private void HandleFailover(IRedisClientsManager clientsManager)
406
402
if ( masterClient != null )
407
403
{
408
404
//New thread-safe client with same connection info as connected master
409
- using ( var currentlySubscribedClient = ( ( RedisClient ) masterClient ) . CloneClient ( ) )
405
+ using var currentlySubscribedClient = ( ( RedisClient ) masterClient ) . CloneClient ( ) ;
406
+ Interlocked . CompareExchange ( ref doOperation , Operation . Reset , doOperation ) ;
407
+ foreach ( var channel in Channels )
410
408
{
411
- Interlocked . CompareExchange ( ref doOperation , Operation . Reset , doOperation ) ;
412
- foreach ( var channel in Channels )
413
- {
414
- currentlySubscribedClient . PublishMessage ( channel , ControlCommand . Control ) ;
415
- }
409
+ currentlySubscribedClient . PublishMessage ( channel , ControlCommand . Control ) ;
416
410
}
417
411
}
418
412
else
@@ -466,12 +460,12 @@ private void KillBgThreadIfExists()
466
460
private void SleepBackOffMultiplier ( int continuousErrorsCount )
467
461
{
468
462
if ( continuousErrorsCount == 0 ) return ;
469
- const int MaxSleepMs = 60 * 1000 ;
463
+ const int maxSleepMs = 60 * 1000 ;
470
464
471
465
//exponential/random retry back-off.
472
466
var nextTry = Math . Min (
473
467
rand . Next ( ( int ) Math . Pow ( continuousErrorsCount , 3 ) , ( int ) Math . Pow ( continuousErrorsCount + 1 , 3 ) + 1 ) ,
474
- MaxSleepMs ) ;
468
+ maxSleepMs ) ;
475
469
476
470
if ( Log . IsDebugEnabled )
477
471
Log . Debug ( "Sleeping for {0}ms after {1} continuous errors" . Fmt ( nextTry , continuousErrorsCount ) ) ;
0 commit comments