Skip to content

Commit 3dd6a7a

Browse files
martintmkmartincostello
authored andcommitted
Protect against retry delay overflows
1 parent 160b9ef commit 3dd6a7a

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/Polly.Core/Retry/RetryHelper.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ internal static class RetryHelper
99
public static bool IsValidDelay(TimeSpan delay) => delay >= TimeSpan.Zero;
1010

1111
public static TimeSpan GetRetryDelay(DelayBackoffType type, bool jitter, int attempt, TimeSpan baseDelay, ref double state, Func<double> randomizer)
12+
{
13+
try
14+
{
15+
return GetRetryDelayCore(type, jitter, attempt, baseDelay, ref state, randomizer);
16+
}
17+
catch (OverflowException)
18+
{
19+
return TimeSpan.MaxValue;
20+
}
21+
}
22+
23+
private static TimeSpan GetRetryDelayCore(RetryBackoffType type, bool jitter, int attempt, TimeSpan baseDelay, ref double state, Func<double> randomizer)
1224
{
1325
if (baseDelay == TimeSpan.Zero)
1426
{

test/Polly.Core.Tests/Retry/RetryHelperTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ public void Exponential_Ok()
9595
RetryHelper.GetRetryDelay(DelayBackoffType.Exponential, false, 2, TimeSpan.FromSeconds(1), ref state, _randomizer).Should().Be(TimeSpan.FromSeconds(4));
9696
}
9797

98+
[Fact]
99+
public void GetRetryDelay_Overflow_ReturnsMaxTimeSpan()
100+
{
101+
double state = 0;
102+
103+
RetryHelper.GetRetryDelay(RetryBackoffType.Exponential, false, 1000, TimeSpan.FromDays(1), ref state, _randomizer).Should().Be(TimeSpan.MaxValue);
104+
}
105+
98106
[InlineData(1)]
99107
[InlineData(2)]
100108
[InlineData(3)]

0 commit comments

Comments
 (0)