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

Commit 51d056a

Browse files
committed
Collapse nearly identical SendReceiveVoid into SendReceive with action func hacks
1 parent c1c5d1e commit 51d056a

File tree

1 file changed

+32
-69
lines changed

1 file changed

+32
-69
lines changed

src/ServiceStack.Redis/RedisNativeClient_Utils.cs

Lines changed: 32 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -400,13 +400,13 @@ private void PushCurrentBuffer()
400400

401401
public Action OnBeforeFlush { get; set; }
402402

403-
public void FlushAndResetSendBuffer()
403+
internal void FlushAndResetSendBuffer()
404404
{
405405
FlushSendBuffer();
406406
ResetSendBuffer();
407407
}
408408

409-
public void FlushSendBuffer()
409+
internal void FlushSendBuffer()
410410
{
411411
if (currentBufferIndex > 0)
412412
PushCurrentBuffer();
@@ -458,7 +458,10 @@ private int SafeReadByte()
458458
return Bstream.ReadByte();
459459
}
460460

461-
protected T SendReceive<T>(byte[][] cmdWithBinaryArgs, Func<T> fn, Action<Func<T>> completePipelineFn = null)
461+
protected T SendReceive<T>(byte[][] cmdWithBinaryArgs,
462+
Func<T> fn,
463+
Action<Func<T>> completePipelineFn = null,
464+
bool sendWithoutRead = false)
462465
{
463466
var i = 0;
464467
Exception originalEx = null;
@@ -478,7 +481,7 @@ protected T SendReceive<T>(byte[][] cmdWithBinaryArgs, Func<T> fn, Action<Func<T
478481
{
479482
FlushSendBuffer();
480483
}
481-
else
484+
else if (!sendWithoutRead)
482485
{
483486
if (completePipelineFn == null)
484487
throw new NotSupportedException("Pipeline is not supported.");
@@ -487,12 +490,15 @@ protected T SendReceive<T>(byte[][] cmdWithBinaryArgs, Func<T> fn, Action<Func<T
487490
return default(T);
488491
}
489492

490-
var result = fn();
493+
var result = default(T);
494+
if (fn != null)
495+
result = fn();
491496

492497
if (Pipeline == null)
493-
{
494498
ResetSendBuffer();
495-
}
499+
500+
if (i > 0)
501+
Interlocked.Increment(ref RedisState.TotalRetrySuccess);
496502

497503
return result;
498504
}
@@ -512,70 +518,15 @@ protected T SendReceive<T>(byte[][] cmdWithBinaryArgs, Func<T> fn, Action<Func<T
512518

513519
var retry = DateTime.UtcNow - firstAttempt < retryTimeout;
514520
if (!retry)
515-
throw CreateRetryTimeoutException(retryTimeout, originalEx);
516-
517-
Thread.Sleep(GetBackOffMultiplier(++i));
518-
}
519-
}
520-
}
521-
522-
protected void SendReceiveVoid(byte[][] cmdWithBinaryArgs, Action fn, Action<Action> completePipelineFn, bool sendWithoutRead = false)
523-
{
524-
var i = 0;
525-
Exception originalEx = null;
526-
527-
var firstAttempt = DateTime.UtcNow;
528-
529-
while (true)
530-
{
531-
try
532-
{
533-
TryConnectIfNeeded();
534-
535-
if (i == 0) //only write to buffer once
536-
WriteCommandToSendBuffer(cmdWithBinaryArgs);
537-
538-
if (Pipeline == null) //pipeline will handle flush if in pipeline
539-
{
540-
FlushSendBuffer();
541-
}
542-
else if (!sendWithoutRead)
543-
{
544-
if (completePipelineFn == null)
545-
throw new NotSupportedException("Pipeline is not supported.");
546-
547-
completePipelineFn(fn);
548-
return;
549-
}
550-
551-
if (fn != null)
552-
fn();
553-
554-
if (Pipeline == null)
555521
{
556-
ResetSendBuffer();
557-
}
558-
559-
return;
560-
}
561-
catch (Exception outerEx)
562-
{
563-
var retryableEx = outerEx as RedisRetryableException;
522+
if (Pipeline == null)
523+
ResetSendBuffer();
564524

565-
if (outerEx is RedisException && retryableEx == null)
566-
throw;
567-
568-
var ex = retryableEx ?? GetRetryableException(outerEx);
569-
if (ex == null)
570-
throw CreateConnectionError();
571-
572-
if (originalEx == null)
573-
originalEx = ex;
574-
575-
var retry = DateTime.UtcNow - firstAttempt < retryTimeout;
576-
if (!retry)
525+
Interlocked.Increment(ref RedisState.TotalRetryTimedout);
577526
throw CreateRetryTimeoutException(retryTimeout, originalEx);
527+
}
578528

529+
Interlocked.Increment(ref RedisState.TotalRetryCount);
579530
Thread.Sleep(GetBackOffMultiplier(++i));
580531
}
581532
}
@@ -616,12 +567,17 @@ private static int GetBackOffMultiplier(int i)
616567

617568
protected void SendWithoutRead(params byte[][] cmdWithBinaryArgs)
618569
{
619-
SendReceiveVoid(cmdWithBinaryArgs, null, null, sendWithoutRead: true);
570+
SendReceive<long>(cmdWithBinaryArgs, null, null, sendWithoutRead: true);
620571
}
621572

622573
protected void SendExpectSuccess(params byte[][] cmdWithBinaryArgs)
623574
{
624-
SendReceiveVoid(cmdWithBinaryArgs, ExpectSuccess, Pipeline != null ? Pipeline.CompleteVoidQueuedCommand : (Action<Action>)null);
575+
//Turn Action into Func Hack
576+
var completePipelineFn = Pipeline != null
577+
? f => { Pipeline.CompleteVoidQueuedCommand(() => f()); }
578+
: (Action<Func<long>>)null;
579+
580+
SendReceive(cmdWithBinaryArgs, ExpectSuccessFn, completePipelineFn);
625581
}
626582

627583
protected long SendExpectLong(params byte[][] cmdWithBinaryArgs)
@@ -699,6 +655,13 @@ protected void CmdLog(byte[][] args)
699655
log.Debug("S: " + this.lastCommand);
700656
}
701657

658+
//Turn Action into Func Hack
659+
protected long ExpectSuccessFn()
660+
{
661+
ExpectSuccess();
662+
return 0;
663+
}
664+
702665
protected void ExpectSuccess()
703666
{
704667
int c = SafeReadByte();

0 commit comments

Comments
 (0)