Fixed several socket issues. Added backoff for reconnection.

This commit is contained in:
Tom
2024-08-10 19:31:08 +00:00
parent aa9e3dbcd7
commit ab90d47b89
8 changed files with 207 additions and 106 deletions

View 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
View File

@ -0,0 +1,8 @@
namespace CommonSocketLibrary.Backoff
{
public interface IBackoff
{
TimeSpan GetNextDelay();
void Reset();
}
}