Skip to content

Commit 64ed77b

Browse files
committed
Added an auto shutdown on 3 failed connection retrys when accessing the SSE. Resets after 10sec, retrys occure in 5sec interval.
1 parent 6afa49b commit 64ed77b

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

WolfLeash/Components/Classes/WolfApi.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@ namespace WolfLeash.Components.Classes;
44

55
public class Api : WolfApi
66
{
7-
public Api(ILogger<WolfApi> logger, IConfiguration configuration) : base(logger, configuration) { }
7+
private readonly ILogger<WolfApi> _logger;
8+
private readonly IHostApplicationLifetime _lifeCycleService;
9+
private int _connectionErrorCount = 0;
10+
private DateTime _lastConnectionErrorTime = DateTime.MinValue;
11+
12+
public Api(ILogger<WolfApi> logger, IConfiguration configuration, IHostApplicationLifetime lifetime) :
13+
base(logger, configuration)
14+
{
15+
_lifeCycleService = lifetime;
16+
_logger = logger;
17+
}
818

919
private static Task Raise<TSource, TEventArgs>(Func<TSource, TEventArgs, Task>? handlers, TSource source, TEventArgs args)
1020
{
@@ -17,7 +27,21 @@ private static Task Raise<TSource, TEventArgs>(Func<TSource, TEventArgs, Task>?
1727

1828
return Task.CompletedTask;
1929
}
20-
30+
31+
protected override Task OnSseConnectionLostEvent(bool isFatal)
32+
{
33+
if (DateTime.Now - _lastConnectionErrorTime > TimeSpan.FromSeconds(10))
34+
{
35+
_connectionErrorCount = 0;
36+
_lastConnectionErrorTime = DateTime.Now;
37+
}
38+
if (_connectionErrorCount++ <= 3) return base.OnSseConnectionLostEvent(isFatal);
39+
_logger.LogError("Can't connect to Socket, shutting down.");
40+
_lifeCycleService.StopApplication();
41+
42+
return base.OnSseConnectionLostEvent(isFatal);
43+
}
44+
2145
protected override async Task OnPairSignalEvent(string data)
2246
{
2347
await Raise(PairRequestEvent, this, data);

0 commit comments

Comments
 (0)