Skip to content

Commit cc03333

Browse files
committed
Introduced min, max and delta concepts for calculating interval
1 parent 8d63954 commit cc03333

File tree

3 files changed

+57
-21
lines changed

3 files changed

+57
-21
lines changed

src/SendGrid/Reliability/ReliabilitySettings.cs

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@ namespace SendGrid.Helpers.Reliability
77
/// </summary>
88
public class ReliabilitySettings
99
{
10-
private int retryCount;
10+
private int maximumNumberOfRetries;
1111

12-
private TimeSpan retryInterval;
12+
private TimeSpan minimumBackOff;
13+
private TimeSpan maximumBackOff;
14+
private TimeSpan deltaBackOff;
1315

1416
/// <summary>
1517
/// Initializes a new instance of the <see cref="ReliabilitySettings"/> class.
1618
/// </summary>
1719
public ReliabilitySettings()
1820
{
19-
this.retryCount = 0;
20-
this.retryInterval = TimeSpan.FromSeconds(1);
21+
this.maximumNumberOfRetries = 0;
22+
this.minimumBackOff = TimeSpan.FromSeconds(1);
23+
this.deltaBackOff = TimeSpan.FromSeconds(1);
24+
this.maximumBackOff = TimeSpan.FromSeconds(10);
2125
}
2226

2327
/// <summary>
@@ -27,7 +31,7 @@ public int MaximumNumberOfRetries
2731
{
2832
get
2933
{
30-
return this.retryCount;
34+
return this.maximumNumberOfRetries;
3135
}
3236

3337
set
@@ -42,28 +46,64 @@ public int MaximumNumberOfRetries
4246
throw new ArgumentException("The maximum number of retries that can be attempted is 5");
4347
}
4448

45-
this.retryCount = value;
49+
this.maximumNumberOfRetries = value;
4650
}
4751
}
4852

4953
/// <summary>
5054
/// Gets or sets the interval between HTTP retries. Defaults to 1 second
5155
/// </summary>
52-
public TimeSpan RetryInterval
56+
public TimeSpan MinimumBackOff
5357
{
5458
get
5559
{
56-
return this.retryInterval;
60+
return this.minimumBackOff;
5761
}
5862

5963
set
6064
{
6165
if (value.TotalSeconds > 30)
6266
{
63-
throw new ArgumentException("The maximum retry interval is 30 seconds");
67+
throw new ArgumentException("The maximum setting for minimum back off is 30 seconds");
6468
}
6569

66-
this.retryInterval = value;
70+
this.minimumBackOff = value;
71+
}
72+
}
73+
74+
public TimeSpan MaximumBackOff
75+
{
76+
get
77+
{
78+
return this.maximumBackOff;
79+
}
80+
81+
set
82+
{
83+
if (value.TotalSeconds > 30)
84+
{
85+
throw new ArgumentException("The maximum setting to back off for is 30 seconds");
86+
}
87+
88+
this.maximumBackOff = value;
89+
}
90+
}
91+
92+
public TimeSpan DeltaBackOff
93+
{
94+
get
95+
{
96+
return this.deltaBackOff;
97+
}
98+
99+
set
100+
{
101+
if (value.TotalSeconds > 30)
102+
{
103+
throw new ArgumentException("The maximum delta interval is 5 seconds");
104+
}
105+
106+
this.deltaBackOff = value;
67107
}
68108
}
69109
}

src/SendGrid/Reliability/RetryDelegatingHandler.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,18 +107,14 @@ private static void ThrowHttpRequestExceptionIfResponseCodeCanBeRetried(HttpResp
107107

108108
private TimeSpan GetNextWaitInterval(int numberOfAttempts)
109109
{
110-
var randomDelay = this.random.Next(0, 500);
110+
var delta = (int)((Math.Pow(2.0, numberOfAttempts) - 1.0) *
111+
this.random.Next(
112+
(int)(this.settings.DeltaBackOff.TotalMilliseconds * 0.8),
113+
(int)(this.settings.DeltaBackOff.TotalMilliseconds * 1.2)));
111114

112-
if (numberOfAttempts == 0)
113-
{
114-
return TimeSpan.FromMilliseconds(this.settings.RetryInterval.TotalMilliseconds + randomDelay);
115-
}
116-
117-
var exponentialIncrease = Math.Pow(2, numberOfAttempts) * 1000;
118-
119-
var actualIncrease = TimeSpan.FromMilliseconds(this.settings.RetryInterval.TotalMilliseconds + exponentialIncrease + randomDelay);
115+
var interval = (int)Math.Min(this.settings.MinimumBackOff.TotalMilliseconds + delta, this.settings.MaximumBackOff.TotalMilliseconds);
120116

121-
return actualIncrease;
117+
return TimeSpan.FromMilliseconds(interval);
122118
}
123119
}
124120
}

tests/SendGrid.Tests/Reliability/RetryDelegatingHandlerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public void ReliabilitySettingsShouldNotAllowRetryIntervalGreaterThan30Seconds()
135135
{
136136
var settings = new ReliabilitySettings();
137137

138-
Assert.Throws<ArgumentException>(() => settings.RetryInterval = TimeSpan.FromSeconds(31));
138+
Assert.Throws<ArgumentException>(() => settings.MinimumBackOff = TimeSpan.FromSeconds(31));
139139
}
140140
}
141141
}

0 commit comments

Comments
 (0)