try improve sensitive error handling
This commit is contained in:
@@ -60,26 +60,34 @@ public class ApiServer(string apiUrl) : IApiServer
|
||||
}
|
||||
|
||||
public virtual void Stop()
|
||||
{
|
||||
Stop(true);
|
||||
}
|
||||
|
||||
public virtual void Stop(bool graceful)
|
||||
{
|
||||
Log.Info("Stopping listener");
|
||||
doListen = false;
|
||||
httpListener.Stop();
|
||||
if (graceful)
|
||||
httpListener.Stop();
|
||||
else
|
||||
httpListener.Abort();
|
||||
Thread.Sleep(StopDelay);
|
||||
httpListener.Close();
|
||||
Log.Info("Stopped listener");
|
||||
}
|
||||
|
||||
public virtual void Restart()
|
||||
public virtual void Restart(bool graceful)
|
||||
{
|
||||
Log.Info("Restarting listener");
|
||||
Stop();
|
||||
Stop(graceful);
|
||||
httpListener = new();
|
||||
semaphore?.Release(int.MaxValue);
|
||||
Start();
|
||||
Log.Info("Restarted listener");
|
||||
}
|
||||
|
||||
protected virtual bool AutoRestart()
|
||||
protected virtual bool AutoRestart(bool graceful)
|
||||
{
|
||||
if (restartAttempts > MaxAutoRestartsPerMinute)
|
||||
{
|
||||
@@ -94,7 +102,7 @@ public class ApiServer(string apiUrl) : IApiServer
|
||||
|
||||
restartAttempts += 1;
|
||||
|
||||
Restart();
|
||||
Restart(graceful);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -192,6 +200,10 @@ public class ApiServer(string apiUrl) : IApiServer
|
||||
{
|
||||
WaitForSlot();
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
return;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Log.Fatal($"Too many concurent connections", ex);
|
||||
@@ -205,16 +217,17 @@ public class ApiServer(string apiUrl) : IApiServer
|
||||
{
|
||||
context = httpListener.EndGetContext(result);
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
return;
|
||||
}
|
||||
catch (HttpListenerException ex)
|
||||
{
|
||||
if (ex.ErrorCode == 995 || ex.ErrorCode == 64)
|
||||
{
|
||||
Log.Fatal($"Fatal http error retriving context with code {ex.ErrorCode}", ex);
|
||||
if (AutoRestart()) // Try restart the server and skip this context
|
||||
{
|
||||
FreeSlot();
|
||||
if (AutoRestart(false)) // Try restart the server and skip this context
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
Log.Error($"Http error retriving context with code {ex.ErrorCode}", ex);
|
||||
@@ -255,9 +268,6 @@ public class ApiServer(string apiUrl) : IApiServer
|
||||
OnCheckContextCompleted?.Invoke(this, new(context));
|
||||
}
|
||||
|
||||
// Release slot
|
||||
FreeSlot();
|
||||
|
||||
// Listen for new request
|
||||
if (!AllowMultipleRequests)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user