-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from dodopizza/sleep_duration_provider
refactor: make sleep duration provider variation
- Loading branch information
Showing
13 changed files
with
144 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/Dodo.HttpClient.ResiliencePolicies/RetryPolicySettings/ISleepDurationProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Dodo.HttpClientResiliencePolicies.RetrySettings | ||
{ | ||
public interface ISleepDurationProvider | ||
{ | ||
IEnumerable<TimeSpan> SleepFunction { get; } | ||
} | ||
} |
42 changes: 0 additions & 42 deletions
42
src/Dodo.HttpClient.ResiliencePolicies/RetryPolicySettings/JitterRetryPolicySettings.cs
This file was deleted.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
src/Dodo.HttpClient.ResiliencePolicies/RetryPolicySettings/RetryPolicySettings.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using System.Net.Http; | ||
using Polly; | ||
|
||
namespace Dodo.HttpClientResiliencePolicies.RetrySettings | ||
{ | ||
public class RetryPolicySettings : IRetryPolicySettings | ||
{ | ||
public ISleepDurationProvider SleepDurationProvider { get; } | ||
public Action<DelegateResult<HttpResponseMessage>, TimeSpan> OnRetry { get; set; } | ||
|
||
public RetryPolicySettings() | ||
: this(RetrySettings.SleepDurationProvider.Jitter( | ||
Defaults.Retry.RetryCount, | ||
TimeSpan.FromMilliseconds(Defaults.Retry.MedianFirstRetryDelayInMilliseconds))) | ||
{ | ||
} | ||
|
||
public RetryPolicySettings( | ||
ISleepDurationProvider sleepDurationProvider) | ||
{ | ||
SleepDurationProvider = sleepDurationProvider; | ||
OnRetry = _doNothingOnRetry; | ||
} | ||
|
||
private static readonly Action<DelegateResult<HttpResponseMessage>, TimeSpan> _doNothingOnRetry = (_, __) => { }; | ||
} | ||
} |
33 changes: 0 additions & 33 deletions
33
src/Dodo.HttpClient.ResiliencePolicies/RetryPolicySettings/SimpleRetryPolicySettings.cs
This file was deleted.
Oops, something went wrong.
71 changes: 71 additions & 0 deletions
71
src/Dodo.HttpClient.ResiliencePolicies/RetryPolicySettings/SleepDurationProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Polly.Contrib.WaitAndRetry; | ||
|
||
namespace Dodo.HttpClientResiliencePolicies.RetrySettings | ||
{ | ||
public sealed class SleepDurationProvider : ISleepDurationProvider | ||
{ | ||
public IEnumerable<TimeSpan> SleepFunction { get; } | ||
|
||
private SleepDurationProvider(IEnumerable<TimeSpan> sleepFunction) | ||
{ | ||
if (sleepFunction == null) | ||
throw new ArgumentNullException(nameof(sleepFunction)); | ||
|
||
SleepFunction = sleepFunction; | ||
} | ||
|
||
public static ISleepDurationProvider Exponential(int retryCount) | ||
{ | ||
return Exponential(retryCount, TimeSpan.FromMilliseconds(Defaults.Retry.InitialDelayMilliseconds)); | ||
} | ||
|
||
public static ISleepDurationProvider Exponential(int retryCount, TimeSpan initialDelay) | ||
{ | ||
if (retryCount < 0) throw new ArgumentOutOfRangeException(nameof(retryCount), retryCount, "should be >= 0"); | ||
if (initialDelay < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(initialDelay), initialDelay, "should be >= 0ms"); | ||
|
||
return new SleepDurationProvider(Backoff.ExponentialBackoff(initialDelay, retryCount)); | ||
} | ||
|
||
public static ISleepDurationProvider Linear(int retryCount) | ||
{ | ||
return Linear(retryCount, TimeSpan.FromMilliseconds(Defaults.Retry.InitialDelayMilliseconds)); | ||
} | ||
|
||
public static ISleepDurationProvider Linear(int retryCount, TimeSpan initialDelay) | ||
{ | ||
if (retryCount < 0) throw new ArgumentOutOfRangeException(nameof(retryCount), retryCount, "should be >= 0"); | ||
if (initialDelay < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(initialDelay), initialDelay, "should be >= 0ms"); | ||
|
||
return new SleepDurationProvider(Backoff.LinearBackoff(initialDelay, retryCount)); | ||
} | ||
|
||
public static ISleepDurationProvider Jitter(int retryCount) | ||
{ | ||
return Jitter(retryCount, TimeSpan.FromMilliseconds(Defaults.Retry.MedianFirstRetryDelayInMilliseconds)); | ||
} | ||
|
||
public static ISleepDurationProvider Jitter(int retryCount, TimeSpan medianFirstRetryDelay) | ||
{ | ||
if (retryCount < 0) throw new ArgumentOutOfRangeException(nameof(retryCount), retryCount, "should be >= 0"); | ||
if (medianFirstRetryDelay < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(medianFirstRetryDelay), medianFirstRetryDelay, "should be >= 0ms"); | ||
|
||
return new SleepDurationProvider(Backoff.DecorrelatedJitterBackoffV2(medianFirstRetryDelay, retryCount)); | ||
} | ||
|
||
public static ISleepDurationProvider Constant(int retryCount) | ||
{ | ||
return Constant(retryCount, TimeSpan.FromMilliseconds(Defaults.Retry.InitialDelayMilliseconds)); | ||
} | ||
|
||
public static ISleepDurationProvider Constant(int retryCount, TimeSpan delay) | ||
{ | ||
if (retryCount < 0) throw new ArgumentOutOfRangeException(nameof(retryCount), retryCount, "should be >= 0"); | ||
if (delay < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(delay), delay, "should be >= 0ms"); | ||
|
||
return new SleepDurationProvider(Backoff.ConstantBackoff(delay, retryCount)); | ||
} | ||
} | ||
} |