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