32 lines
776 B
C#
32 lines
776 B
C#
namespace CommonSocketLibrary.Backoff
|
|
{
|
|
public class ExponentialBackoff : IBackoff
|
|
{
|
|
private int _initial;
|
|
private int _current;
|
|
private int _maximum;
|
|
|
|
|
|
public ExponentialBackoff(int initial, int maximum)
|
|
{
|
|
if (maximum < initial)
|
|
throw new InvalidOperationException("Initial backoff cannot be larger than maximum backoff.");
|
|
|
|
_initial = initial;
|
|
_maximum = maximum;
|
|
Reset();
|
|
}
|
|
|
|
|
|
public TimeSpan GetNextDelay()
|
|
{
|
|
_current = Math.Min(_current * 2, _maximum);
|
|
return TimeSpan.FromMilliseconds(_current);
|
|
}
|
|
|
|
public void Reset()
|
|
{
|
|
_current = _initial / 2;
|
|
}
|
|
}
|
|
} |