feat(AuthClient): Konfiguration der Wiederholungsrichtlinie im Falle eines Verbindungsverlustes hinzugefügt.

This commit is contained in:
Developer 02 2025-02-11 10:33:37 +01:00
parent 484cc86a29
commit 7f39cbe24a
3 changed files with 60 additions and 10 deletions

View File

@ -17,16 +17,20 @@ public class AuthClient : IAuthClient, IAsyncDisposable
public AuthClient(IOptions<ClientParams> paramsOptions, HubConnectionBuilder connectionBuilder, ILogger<AuthClient>? 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<string, string>(nameof(ReceiveKeyAsync), ReceiveKeyAsync);
_logger = logger;
_params = paramsOptions.Value;
_lazyInitiator = new(async () =>
{
try

View File

@ -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<IRetryPolicy?> _lazyRetryPolicy;
/// <summary>
/// Controls when the client attempts to reconnect and how many times it does so.
/// </summary>
public IRetryPolicy? RetryPolicy => _lazyRetryPolicy.Value;
/// <summary>
/// To simplify the assignment of <seealso cref="RetryPolicy"/>
/// </summary>
public Func<RetryContext, TimeSpan?>? NextRetryDelay { get; set; }
/// <summary>
/// To be able to serilize the simple <seealso cref="RetryPolicy"/>
/// </summary>
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;
});
}
}

View File

@ -0,0 +1,17 @@
using Microsoft.AspNetCore.SignalR.Client;
namespace DigitalData.Auth.Client;
public class RetryPolicy : IRetryPolicy
{
private readonly TimeSpan _retryDelay;
private readonly Func<RetryContext, TimeSpan?> _nextRetryDelay;
public RetryPolicy(Func<RetryContext, TimeSpan?> nextRetryDelay)
{
_nextRetryDelay = nextRetryDelay;
}
public TimeSpan? NextRetryDelay(RetryContext retryContext) => _nextRetryDelay(retryContext);
}