minimal improvements for autorestart and slot handling

This commit is contained in:
Pilzinsel64
2025-02-04 14:35:22 +01:00
parent c0a422ca71
commit 22c962bec7

View File

@@ -18,6 +18,7 @@ public class ApiServer(string apiUrl) : IApiServer
protected DateTime lastRestartAttempt; protected DateTime lastRestartAttempt;
protected SemaphoreSlim? semaphore; protected SemaphoreSlim? semaphore;
protected bool doListen; protected bool doListen;
protected bool isAutoRestarting;
public event OnCheckAuthenticationEventHandler? OnCheckAuthentication; public event OnCheckAuthenticationEventHandler? OnCheckAuthentication;
public event OnCheckContextEventHandler? OnCheckContext; public event OnCheckContextEventHandler? OnCheckContext;
@@ -89,10 +90,13 @@ public class ApiServer(string apiUrl) : IApiServer
protected virtual bool AutoRestart(bool graceful) protected virtual bool AutoRestart(bool graceful)
{ {
if (isAutoRestarting)
return true;
if (restartAttempts > MaxAutoRestartsPerMinute) if (restartAttempts > MaxAutoRestartsPerMinute)
{ {
Log.Fatal("Reached maximum auto-restart attempts"); Log.Fatal("Reached maximum auto-restart attempts");
Stop(); Stop(false);
return false; return false;
} }
@@ -102,7 +106,9 @@ public class ApiServer(string apiUrl) : IApiServer
restartAttempts += 1; restartAttempts += 1;
isAutoRestarting = true;
Restart(graceful); Restart(graceful);
isAutoRestarting = false;
return true; return true;
} }
@@ -253,9 +259,11 @@ public class ApiServer(string apiUrl) : IApiServer
// Check context // Check context
Log.Info("Request retrived for " + context.Request.RawUrl); Log.Info("Request retrived for " + context.Request.RawUrl);
OnCheckContext?.Invoke(this, new(context)); OnCheckContext?.Invoke(this, new(context));
var success = false;
try try
{ {
CheckContext(context); CheckContext(context);
success = true;
} }
catch (Exception ex) catch (Exception ex)
{ {
@@ -268,12 +276,23 @@ public class ApiServer(string apiUrl) : IApiServer
OnCheckContextCompleted?.Invoke(this, new(context)); OnCheckContextCompleted?.Invoke(this, new(context));
} }
// Try closing the request on fail
if (!success)
{
try
{
context.Response.Close();
}
catch (Exception)
{
}
}
FreeSlot();
// Listen for new request // Listen for new request
if (!AllowMultipleRequests) if (!AllowMultipleRequests)
{
FreeSlot();
Receive(); Receive();
}
} }
protected virtual void CheckContext(HttpListenerContext context) protected virtual void CheckContext(HttpListenerContext context)