Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit ee63866

Browse files
committed
2 parents 05a9a63 + 0b1a364 commit ee63866

File tree

1 file changed

+21
-44
lines changed

1 file changed

+21
-44
lines changed

src/ServiceStack.Redis/RedisPubSubServer.cs

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,9 @@ public bool AutoRestart
5454
set { Interlocked.CompareExchange(ref autoRestart, value ? YES : NO, autoRestart); }
5555
}
5656

57-
public DateTime CurrentServerTime
58-
{
59-
get { return new DateTime(serverTimeAtStart.Ticks + startedAt.ElapsedTicks, DateTimeKind.Utc); }
60-
}
57+
public DateTime CurrentServerTime => new DateTime(serverTimeAtStart.Ticks + startedAt.ElapsedTicks, DateTimeKind.Utc);
6158

62-
public long BgThreadCount
63-
{
64-
get { return Interlocked.CompareExchange(ref bgThreadCount, 0, 0); }
65-
}
59+
public long BgThreadCount => Interlocked.CompareExchange(ref bgThreadCount, 0, 0);
6660

6761
public const string AllChannelsWildCard = "*";
6862
public IRedisClientsManager ClientsManager { get; set; }
@@ -76,10 +70,7 @@ public RedisPubSubServer(IRedisClientsManager clientsManager, params string[] ch
7670
this.Channels = channels;
7771

7872
var failoverHost = clientsManager as IRedisFailover;
79-
if (failoverHost != null)
80-
{
81-
failoverHost.OnFailover.Add(HandleFailover);
82-
}
73+
failoverHost?.OnFailover.Add(HandleFailover);
8374
}
8475

8576
public IRedisPubSubServer Start()
@@ -89,8 +80,7 @@ public IRedisPubSubServer Start()
8980
if (Interlocked.CompareExchange(ref status, 0, 0) == Status.Started)
9081
{
9182
//Start any stopped worker threads
92-
if (OnStart != null)
93-
OnStart();
83+
OnStart?.Invoke();
9484

9585
return this;
9686
}
@@ -106,8 +96,7 @@ public IRedisPubSubServer Start()
10696

10797
SleepBackOffMultiplier(Interlocked.CompareExchange(ref noOfContinuousErrors, 0, 0));
10898

109-
if (OnStart != null)
110-
OnStart();
99+
OnStart?.Invoke();
111100

112101
//Don't kill us if we're the thread that's retrying to Start() after a failure.
113102
if (bgThread != Thread.CurrentThread)
@@ -132,8 +121,7 @@ public IRedisPubSubServer Start()
132121
}
133122
catch (Exception ex)
134123
{
135-
if (this.OnError != null)
136-
this.OnError(ex);
124+
OnError?.Invoke(ex);
137125
}
138126
}
139127

@@ -154,8 +142,7 @@ private void Init()
154142
}
155143
catch (Exception ex)
156144
{
157-
if (OnError != null)
158-
OnError(ex);
145+
OnError?.Invoke(ex);
159146
}
160147

161148
DisposeHeartbeatTimer();
@@ -168,8 +155,7 @@ private void Init()
168155

169156
Interlocked.CompareExchange(ref lastHeartbeatTicks, DateTime.UtcNow.Ticks, lastHeartbeatTicks);
170157

171-
if (OnInit != null)
172-
OnInit();
158+
OnInit?.Invoke();
173159
}
174160

175161
void SendHeartbeat(object state)
@@ -181,8 +167,7 @@ void SendHeartbeat(object state)
181167
if (DateTime.UtcNow - new DateTime(lastHeartbeatTicks) < HeartbeatInterval.Value)
182168
return;
183169

184-
if (OnHeartbeatSent != null)
185-
OnHeartbeatSent();
170+
OnHeartbeatSent?.Invoke();
186171

187172
NotifyAllSubscribers(ControlCommand.Pulse);
188173

@@ -200,8 +185,7 @@ void Pulse()
200185
{
201186
Interlocked.CompareExchange(ref lastHeartbeatTicks, DateTime.UtcNow.Ticks, lastHeartbeatTicks);
202187

203-
if (OnHeartbeatReceived != null)
204-
OnHeartbeatReceived();
188+
OnHeartbeatReceived?.Invoke();
205189
}
206190

207191
private void DisposeHeartbeatTimer()
@@ -215,7 +199,7 @@ private void DisposeHeartbeatTimer()
215199
}
216200
catch (Exception ex)
217201
{
218-
if (this.OnError != null) this.OnError(ex);
202+
OnError?.Invoke(ex);
219203
}
220204
heartbeatTimer = null;
221205
}
@@ -256,8 +240,7 @@ private void RunLoop()
256240
? ctrlMsg[1]
257241
: null;
258242

259-
if (OnControlCommand != null)
260-
OnControlCommand(msgType ?? Operation.GetName(op));
243+
OnControlCommand?.Invoke(msgType ?? Operation.GetName(op));
261244

262245
switch (op)
263246
{
@@ -308,8 +291,7 @@ private void RunLoop()
308291
}
309292
}
310293

311-
if (OnStop != null)
312-
OnStop();
294+
OnStop?.Invoke();
313295
}
314296
catch (Exception ex)
315297
{
@@ -320,11 +302,9 @@ private void RunLoop()
320302
if (Interlocked.CompareExchange(ref status, Status.Stopped, Status.Started) != Status.Started)
321303
Interlocked.CompareExchange(ref status, Status.Stopped, Status.Stopping);
322304

323-
if (OnStop != null)
324-
OnStop();
305+
OnStop?.Invoke();
325306

326-
if (this.OnError != null)
327-
this.OnError(ex);
307+
OnError?.Invoke(ex);
328308
}
329309

330310
if (AutoRestart && Interlocked.CompareExchange(ref status, 0, 0) != Status.Disposed)
@@ -381,7 +361,7 @@ private void NotifyAllSubscribers(string commandType=null)
381361
}
382362
catch (Exception ex)
383363
{
384-
if (this.OnError != null) this.OnError(ex);
364+
OnError?.Invoke(ex);
385365
Log.Warn("Could not send '{0}' message to bg thread: {1}".Fmt(msg, ex.Message));
386366
}
387367
}
@@ -390,8 +370,7 @@ private void HandleFailover(IRedisClientsManager clientsManager)
390370
{
391371
try
392372
{
393-
if (OnFailover != null)
394-
OnFailover(this);
373+
OnFailover?.Invoke(this);
395374

396375
if (masterClient != null)
397376
{
@@ -412,7 +391,7 @@ private void HandleFailover(IRedisClientsManager clientsManager)
412391
}
413392
catch (Exception ex)
414393
{
415-
if (this.OnError != null) this.OnError(ex);
394+
OnError?.Invoke(ex);
416395
Log.Warn("Error trying to UnSubscribeFromChannels in OnFailover. Restarting...", ex);
417396
Restart();
418397
}
@@ -423,8 +402,7 @@ void HandleUnSubscribe(string channel)
423402
if (Log.IsDebugEnabled)
424403
Log.Debug("OnUnSubscribe: " + channel);
425404

426-
if (OnUnSubscribe != null)
427-
OnUnSubscribe(channel);
405+
OnUnSubscribe?.Invoke(channel);
428406
}
429407

430408
public void Restart()
@@ -553,8 +531,7 @@ public virtual void Dispose()
553531

554532
try
555533
{
556-
if (OnDispose != null)
557-
OnDispose();
534+
OnDispose?.Invoke();
558535
}
559536
catch (Exception ex)
560537
{
@@ -568,7 +545,7 @@ public virtual void Dispose()
568545
}
569546
catch (Exception ex)
570547
{
571-
if (this.OnError != null) this.OnError(ex);
548+
OnError?.Invoke(ex);
572549
}
573550

574551
DisposeHeartbeatTimer();

0 commit comments

Comments
 (0)