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

Commit 048e621

Browse files
committed
Implement Auto Retry of Socket and Retryable Exceptions
1 parent 0ab36ed commit 048e621

18 files changed

+391
-179
lines changed

src/ServiceStack.Redis/Generic/RedisTypedClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ internal int ReadMultiDataResultCount()
479479
}
480480
public void FlushSendBuffer()
481481
{
482-
client.FlushSendBuffer();
482+
client.FlushAndResetSendBuffer();
483483
}
484484
public void ResetSendBuffer()
485485
{

src/ServiceStack.Redis/Pipeline/RedisAllPurposePipeline.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protected virtual void Init()
3434
public void Flush()
3535
{
3636
// flush send buffers
37-
RedisClient.FlushSendBuffer();
37+
RedisClient.FlushAndResetSendBuffer();
3838

3939
//receive expected results
4040
foreach (var queuedCommand in QueuedCommands)

src/ServiceStack.Redis/Pipeline/RedisPipelineCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public bool ReadAllAsIntsHaveSuccess()
3838

3939
public void Flush()
4040
{
41-
client.FlushSendBuffer();
41+
client.FlushAndResetSendBuffer();
4242
}
4343
}
4444
}

src/ServiceStack.Redis/RedisConfig.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ public class RedisConfig
2222
public static int DefaultConnectTimeout = 0;
2323
public static int DefaultSendTimeout = -1;
2424
public static int DefaultReceiveTimeout = -1;
25+
public static int DefaultRetryTimeout = 3 * 1000;
2526
public static int DefaultIdleTimeOutSecs = 240;
27+
public static int BackOffMultiplier = 10;
2628

2729
public static int BufferLength = 1450;
2830

src/ServiceStack.Redis/RedisEndpoint.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public RedisEndpoint()
1616
ConnectTimeout = RedisConfig.DefaultConnectTimeout;
1717
SendTimeout = RedisConfig.DefaultSendTimeout;
1818
ReceiveTimeout = RedisConfig.DefaultReceiveTimeout;
19+
RetryTimeout = RedisConfig.DefaultRetryTimeout;
1920
IdleTimeOutSecs = RedisConfig.DefaultIdleTimeOutSecs;
2021
}
2122

@@ -34,6 +35,7 @@ public RedisEndpoint(string host, int port, string password = null, long db = Re
3435
public int ConnectTimeout { get; set; }
3536
public int SendTimeout { get; set; }
3637
public int ReceiveTimeout { get; set; }
38+
public int RetryTimeout { get; set; }
3739
public int IdleTimeOutSecs { get; set; }
3840
public long Db { get; set; }
3941
public string Client { get; set; }
@@ -61,6 +63,8 @@ public override string ToString()
6163
args.Add("SendTimeout=" + SendTimeout);
6264
if (ReceiveTimeout != RedisConfig.DefaultReceiveTimeout)
6365
args.Add("ReceiveTimeout=" + ReceiveTimeout);
66+
if (RetryTimeout != RedisConfig.DefaultRetryTimeout)
67+
args.Add("RetryTimeout=" + RetryTimeout);
6468
if (IdleTimeOutSecs != RedisConfig.DefaultIdleTimeOutSecs)
6569
args.Add("IdleTimeOutSecs=" + IdleTimeOutSecs);
6670
if (NamespacePrefix != null)
@@ -80,6 +84,7 @@ protected bool Equals(RedisEndpoint other)
8084
&& ConnectTimeout == other.ConnectTimeout
8185
&& SendTimeout == other.SendTimeout
8286
&& ReceiveTimeout == other.ReceiveTimeout
87+
&& RetryTimeout == other.RetryTimeout
8388
&& IdleTimeOutSecs == other.IdleTimeOutSecs
8489
&& Db == other.Db
8590
&& string.Equals(Client, other.Client)
@@ -105,6 +110,7 @@ public override int GetHashCode()
105110
hashCode = (hashCode * 397) ^ ConnectTimeout;
106111
hashCode = (hashCode * 397) ^ SendTimeout;
107112
hashCode = (hashCode * 397) ^ ReceiveTimeout;
113+
hashCode = (hashCode * 397) ^ RetryTimeout;
108114
hashCode = (hashCode * 397) ^ IdleTimeOutSecs;
109115
hashCode = (hashCode * 397) ^ Db.GetHashCode();
110116
hashCode = (hashCode * 397) ^ (Client != null ? Client.GetHashCode() : 0);

src/ServiceStack.Redis/RedisExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ public static RedisEndpoint ToRedisEndpoint(this string connectionString, int? d
9393
case "receivetimeout":
9494
endpoint.ReceiveTimeout = int.Parse(value);
9595
break;
96+
case "retrytimeout":
97+
endpoint.RetryTimeout = int.Parse(value);
98+
break;
9699
case "idletimeout":
97100
case "idletimeoutsecs":
98101
endpoint.IdleTimeOutSecs = int.Parse(value);

src/ServiceStack.Redis/RedisNativeClient.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,12 @@ internal bool Active
104104
/// </summary>
105105
public string NamespacePrefix { get; set; }
106106
public int ConnectTimeout { get; set; }
107-
public int RetryTimeout { get; set; }
107+
private TimeSpan retryTimeout;
108+
public int RetryTimeout
109+
{
110+
get { return (int) retryTimeout.TotalMilliseconds; }
111+
set { retryTimeout = TimeSpan.FromMilliseconds(value); }
112+
}
108113
public int RetryCount { get; set; }
109114
public int SendTimeout { get; set; }
110115
public int ReceiveTimeout { get; set; }
@@ -168,6 +173,7 @@ private void Init(RedisEndpoint config)
168173
ConnectTimeout = config.ConnectTimeout;
169174
SendTimeout = config.SendTimeout;
170175
ReceiveTimeout = config.ReceiveTimeout;
176+
RetryTimeout = config.RetryTimeout;
171177
Password = config.Password;
172178
NamespacePrefix = config.NamespacePrefix;
173179
Client = config.Client;
@@ -771,12 +777,12 @@ public void BgSave()
771777

772778
public void Shutdown()
773779
{
774-
SendCommand(Commands.Shutdown);
780+
SendWithoutRead(Commands.Shutdown);
775781
}
776782

777783
public void ShutdownNoSave()
778784
{
779-
SendCommand(Commands.Shutdown, Commands.NoSave);
785+
SendWithoutRead(Commands.Shutdown, Commands.NoSave);
780786
}
781787

782788
public void BgRewriteAof()
@@ -786,7 +792,7 @@ public void BgRewriteAof()
786792

787793
public void Quit()
788794
{
789-
SendCommand(Commands.Quit);
795+
SendWithoutRead(Commands.Quit);
790796
}
791797

792798
public void FlushDb()
@@ -924,8 +930,7 @@ internal void Multi()
924930
//make sure socket is connected. Otherwise, fetch of server info will interfere
925931
//with pipeline
926932
AssertConnectedSocket();
927-
if (!SendCommand(Commands.Multi))
928-
throw CreateConnectionError();
933+
SendWithoutRead(Commands.Multi);
929934
}
930935

931936
/// <summary>
@@ -934,9 +939,7 @@ internal void Multi()
934939
/// <returns>Number of results</returns>
935940
internal void Exec()
936941
{
937-
if (!SendCommand(Commands.Exec))
938-
throw CreateConnectionError();
939-
942+
SendWithoutRead(Commands.Exec);
940943
}
941944

942945
internal void Discard()

0 commit comments

Comments
 (0)