-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement reconnect strategies (again)
- Loading branch information
1 parent
41adf69
commit 11c61d7
Showing
12 changed files
with
171 additions
and
121 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
24 changes: 24 additions & 0 deletions
24
src/Lavalink4NET.Tests/ExponentialBackoffReconnectStrategyTests.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,24 @@ | ||
namespace Lavalink4NET.Tests; | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Lavalink4NET.Socket; | ||
using Microsoft.Extensions.Options; | ||
using Xunit; | ||
|
||
public sealed class ExponentialBackoffReconnectStrategyTests | ||
{ | ||
[Fact] | ||
public async Task DefaultReconnectStrategyTestAsync() | ||
{ | ||
var strategy = new ExponentialBackoffReconnectStrategy( | ||
Options.Create(new ExponentialBackoffReconnectStrategyOptions())); | ||
|
||
Assert.Equal(TimeSpan.FromSeconds(2), await strategy.GetNextDelayAsync(DateTimeOffset.UtcNow, 1)); | ||
Assert.Equal(TimeSpan.FromSeconds(4), await strategy.GetNextDelayAsync(DateTimeOffset.UtcNow, 2)); | ||
Assert.Equal(TimeSpan.FromSeconds(8), await strategy.GetNextDelayAsync(DateTimeOffset.UtcNow, 3)); | ||
Assert.Equal(TimeSpan.FromSeconds(16), await strategy.GetNextDelayAsync(DateTimeOffset.UtcNow, 4)); | ||
Assert.Equal(TimeSpan.FromSeconds(60), await strategy.GetNextDelayAsync(DateTimeOffset.UtcNow, 50)); | ||
Assert.Equal(TimeSpan.FromSeconds(60), await strategy.GetNextDelayAsync(DateTimeOffset.UtcNow, 50000)); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
34 changes: 34 additions & 0 deletions
34
src/Lavalink4NET/Socket/ExponentialBackoffReconnectStrategy.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,34 @@ | ||
namespace Lavalink4NET.Socket; | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Options; | ||
|
||
public sealed class ExponentialBackoffReconnectStrategy : IReconnectStrategy | ||
{ | ||
private readonly ExponentialBackoffReconnectStrategyOptions _options; | ||
|
||
public ExponentialBackoffReconnectStrategy(IOptions<ExponentialBackoffReconnectStrategyOptions> options) | ||
{ | ||
ArgumentNullException.ThrowIfNull(options); | ||
|
||
_options = options.Value; | ||
} | ||
|
||
public ValueTask<TimeSpan?> GetNextDelayAsync(DateTimeOffset interruptedAt, int attempt, CancellationToken cancellationToken = default) | ||
{ | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
|
||
if (attempt is < 1) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(attempt)); | ||
} | ||
|
||
var factor = Math.Pow(_options.BackoffMultiplier, attempt - 1); | ||
var delayTicks = _options.InitialDelay.TotalSeconds * factor; | ||
var backoff = TimeSpan.FromSeconds(Math.Clamp(delayTicks, _options.MinimumDelay.TotalSeconds, _options.MaximumDelay.TotalSeconds)); | ||
|
||
return new ValueTask<TimeSpan?>(backoff); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Lavalink4NET/Socket/ExponentialBackoffReconnectStrategyOptions.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,14 @@ | ||
namespace Lavalink4NET.Socket; | ||
|
||
using System; | ||
|
||
public sealed class ExponentialBackoffReconnectStrategyOptions | ||
{ | ||
public TimeSpan MaximumDelay { get; set; } = TimeSpan.FromMinutes(1); | ||
|
||
public TimeSpan MinimumDelay { get; set; } = TimeSpan.FromSeconds(2); | ||
|
||
public TimeSpan InitialDelay { get; set; } = TimeSpan.FromSeconds(2); | ||
|
||
public double BackoffMultiplier { get; set; } = 2.0; | ||
} |
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 @@ | ||
namespace Lavalink4NET.Socket; | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
public interface IReconnectStrategy | ||
{ | ||
ValueTask<TimeSpan?> GetNextDelayAsync(DateTimeOffset interruptedAt, int attempt, CancellationToken cancellationToken = default); | ||
} |
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
Oops, something went wrong.