diff --git a/DigitalData.Auth.Client/AuthClient.cs b/DigitalData.Auth.Client/AuthClient.cs index ffa3d65..5e2f9e3 100644 --- a/DigitalData.Auth.Client/AuthClient.cs +++ b/DigitalData.Auth.Client/AuthClient.cs @@ -17,15 +17,19 @@ public class AuthClient : IAuthClient, IAsyncDisposable public AuthClient(IOptions paramsOptions, HubConnectionBuilder connectionBuilder, ILogger? logger = null) { - _connection = connectionBuilder - .WithUrl(paramsOptions.Value.Url) - .Build(); + _params = paramsOptions.Value; + + var cnnBuilder = connectionBuilder.WithUrl(_params.Url); + + // set RetryPolicy if it exists + if (_params.RetryPolicy is not null) + cnnBuilder = cnnBuilder.WithAutomaticReconnect(_params.RetryPolicy); + + _connection = cnnBuilder.Build(); _connection.On(nameof(ReceiveKeyAsync), ReceiveKeyAsync); - _logger = logger; - - _params = paramsOptions.Value; + _logger = logger; _lazyInitiator = new(async () => { diff --git a/DigitalData.Auth.Client/ClientParams.cs b/DigitalData.Auth.Client/ClientParams.cs index de69ec1..fa78d1a 100644 --- a/DigitalData.Auth.Client/ClientParams.cs +++ b/DigitalData.Auth.Client/ClientParams.cs @@ -1,10 +1,39 @@ -namespace DigitalData.Auth.Client; +using Microsoft.AspNetCore.SignalR.Client; + +namespace DigitalData.Auth.Client; public class ClientParams { -#pragma warning disable CS8618 // throw exception in DI extension if it not set - public string Url { get; set; } -#pragma warning restore CS8618 + public string Url { get; set; } = string.Empty; + + private readonly Lazy _lazyRetryPolicy; + + /// + /// Controls when the client attempts to reconnect and how many times it does so. + /// + public IRetryPolicy? RetryPolicy => _lazyRetryPolicy.Value; + + /// + /// To simplify the assignment of + /// + public Func? NextRetryDelay { get; set; } + + /// + /// To be able to serilize the simple + /// + public TimeSpan? RetryDelay { get; set; } public readonly ClientEvents Events = new(); + + public ClientParams() + { + _lazyRetryPolicy = new(() => + { + if (RetryDelay is not null) + return new RetryPolicy(ctx => RetryDelay); + else if(NextRetryDelay is not null) + return new RetryPolicy(NextRetryDelay); + return null; + }); + } } \ No newline at end of file diff --git a/DigitalData.Auth.Client/RetryPolicy.cs b/DigitalData.Auth.Client/RetryPolicy.cs new file mode 100644 index 0000000..ad56e33 --- /dev/null +++ b/DigitalData.Auth.Client/RetryPolicy.cs @@ -0,0 +1,17 @@ +using Microsoft.AspNetCore.SignalR.Client; + +namespace DigitalData.Auth.Client; + +public class RetryPolicy : IRetryPolicy +{ + private readonly TimeSpan _retryDelay; + + private readonly Func _nextRetryDelay; + + public RetryPolicy(Func nextRetryDelay) + { + _nextRetryDelay = nextRetryDelay; + } + + public TimeSpan? NextRetryDelay(RetryContext retryContext) => _nextRetryDelay(retryContext); +}