Fixed several socket issues. Added backoff for reconnection.
This commit is contained in:
32
Backoff/ExponentialBackoff.cs
Normal file
32
Backoff/ExponentialBackoff.cs
Normal file
@ -0,0 +1,32 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
8
Backoff/IBackoff.cs
Normal file
8
Backoff/IBackoff.cs
Normal file
@ -0,0 +1,8 @@
|
||||
namespace CommonSocketLibrary.Backoff
|
||||
{
|
||||
public interface IBackoff
|
||||
{
|
||||
TimeSpan GetNextDelay();
|
||||
void Reset();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user