Total reconnect death

This commit is contained in:
barelyprofessional
2026-06-26 20:27:39 -05:00
parent 2b61433dc6
commit 872c8317ca
3 changed files with 19 additions and 21 deletions
+18 -19
View File
@@ -51,11 +51,11 @@ public class ChatBot
_kfDeadBotDetection = KfDeadBotDetectionTask(); _kfDeadBotDetection = KfDeadBotDetectionTask();
var settings = SettingsProvider.GetMultipleValuesAsync([ var settings = SettingsProvider.GetMultipleValuesAsync([
BuiltIn.Keys.KiwiFarmsWsEndpoint, BuiltIn.Keys.KiwiFarmsDomain, BuiltIn.Keys.KiwiFarmsWsEndpoint, BuiltIn.Keys.KiwiFarmsDomain,
BuiltIn.Keys.KiwiFarmsProxy, BuiltIn.Keys.KiwiFarmsWsReconnectTimeout]).Result; BuiltIn.Keys.KiwiFarmsProxy]).Result;
_kfTokenService = new KfTokenService(settings[BuiltIn.Keys.KiwiFarmsDomain].Value!, _kfTokenService = new KfTokenService(settings[BuiltIn.Keys.KiwiFarmsDomain].Value!,
settings[BuiltIn.Keys.KiwiFarmsProxy].Value, _cancellationToken); settings[BuiltIn.Keys.KiwiFarmsProxy].Value, _cancellationToken);
if (_kfTokenService.GetCookies().Count == 0) if (_kfTokenService.GetCookies().Count == 0)
{ {
try try
@@ -68,14 +68,13 @@ public class ChatBot
_logger.Error(e); _logger.Error(e);
} }
} }
KfClient = new ChatClient(new ChatClientConfigModel KfClient = new ChatClient(new ChatClientConfigModel
{ {
WsUri = new Uri(settings[BuiltIn.Keys.KiwiFarmsWsEndpoint].Value ?? throw new InvalidOperationException($"{BuiltIn.Keys.KiwiFarmsWsEndpoint} cannot be null")), WsUri = new Uri(settings[BuiltIn.Keys.KiwiFarmsWsEndpoint].Value ?? throw new InvalidOperationException($"{BuiltIn.Keys.KiwiFarmsWsEndpoint} cannot be null")),
Cookies = _kfTokenService.GetCookies(), Cookies = _kfTokenService.GetCookies(),
CookieDomain = settings[BuiltIn.Keys.KiwiFarmsDomain].Value ?? throw new InvalidOperationException($"{BuiltIn.Keys.KiwiFarmsDomain} cannot be null"), CookieDomain = settings[BuiltIn.Keys.KiwiFarmsDomain].Value ?? throw new InvalidOperationException($"{BuiltIn.Keys.KiwiFarmsDomain} cannot be null"),
Proxy = settings[BuiltIn.Keys.KiwiFarmsProxy].Value, Proxy = settings[BuiltIn.Keys.KiwiFarmsProxy].Value
ReconnectTimeout = settings[BuiltIn.Keys.KiwiFarmsWsReconnectTimeout].ToType<int>()
}); });
_logger.Debug("Creating bot command instance"); _logger.Debug("Creating bot command instance");
@@ -135,8 +134,8 @@ public class ChatBot
_logger.Error("Caught an exception while trying to refresh the XF token"); _logger.Error("Caught an exception while trying to refresh the XF token");
_logger.Error(e); _logger.Error(e);
} }
_logger.Info("Retrieved fresh token. Reconnecting."); _logger.Info("Retrieved fresh token. Recreating WS client.");
KfClient.ReconnectAsync().Wait(_cancellationToken); RecreateKfClientAsync().Wait(_cancellationToken);
_logger.Info("Client should be reconnecting now"); _logger.Info("Client should be reconnecting now");
} }
@@ -162,9 +161,8 @@ public class ChatBot
var inactivityTimeout = (await SettingsProvider.GetValueAsync(BuiltIn.Keys.KiwiFarmsInactivityTimeout)).ToType<int>(); var inactivityTimeout = (await SettingsProvider.GetValueAsync(BuiltIn.Keys.KiwiFarmsInactivityTimeout)).ToType<int>();
if (inactivityTime.TotalSeconds > inactivityTimeout && lastReconnect.TotalMinutes > 1) if (inactivityTime.TotalSeconds > inactivityTimeout && lastReconnect.TotalMinutes > 1)
{ {
_lastReconnectAttempt = DateTime.UtcNow;
_logger.Error("Forcing reconnect as bot is completely dead"); _logger.Error("Forcing reconnect as bot is completely dead");
await KfClient.ReconnectAsync(); await RecreateKfClientAsync();
} }
} }
} }
@@ -188,7 +186,7 @@ public class ChatBot
_logger.Error($"deadTime -> {deadTime:g}"); _logger.Error($"deadTime -> {deadTime:g}");
if (shouldExit) Environment.Exit(1); if (shouldExit) Environment.Exit(1);
_logger.Error("Since we didn't exit, let's try forcing a reconnect"); _logger.Error("Since we didn't exit, let's try forcing a reconnect");
await KfClient.ReconnectAsync(); await RecreateKfClientAsync();
} }
} }
} }
@@ -845,6 +843,14 @@ public class ChatBot
await db.SaveChangesAsync(_cancellationToken); await db.SaveChangesAsync(_cancellationToken);
} }
private async Task RecreateKfClientAsync()
{
_lastReconnectAttempt = DateTime.UtcNow;
_logger.Info("Disposing and recreating the WS client");
KfClient.DisposeWsClient();
await KfClient.StartWsClient();
}
private void OnKfWsDisconnected(object sender, DisconnectionInfo disconnectionInfo) private void OnKfWsDisconnected(object sender, DisconnectionInfo disconnectionInfo)
{ {
_logger.Error($"Sneedchat disconnected due to {disconnectionInfo.Type}"); _logger.Error($"Sneedchat disconnected due to {disconnectionInfo.Type}");
@@ -855,16 +861,9 @@ public class ChatBot
{ {
_logger.Info("Chat 203'd, getting a new token"); _logger.Info("Chat 203'd, getting a new token");
RefreshXfToken().Wait(_cancellationToken); RefreshXfToken().Wait(_cancellationToken);
_logger.Info("Reconnecting");
KfClient.ReconnectAsync().Wait(_cancellationToken);
}
if (disconnectionInfo.Exception is TaskCanceledException)
{
_logger.Error("WebSocket client is broken as the cancellation token it held onto is FUCKING DEAD. Going to dispose and restart the WS client");
KfClient.DisposeWsClient();
KfClient.StartWsClient().Wait(_cancellationToken);
} }
_logger.Info("Recreating WS client after disconnect");
RecreateKfClientAsync().Wait(_cancellationToken);
} }
private void OnKfWsReconnected(object sender, ReconnectionInfo reconnectionInfo) private void OnKfWsReconnected(object sender, ReconnectionInfo reconnectionInfo)
+1 -1
View File
@@ -101,7 +101,7 @@ public class ChatClient
var client = new WebsocketClient(_config.WsUri, factory) var client = new WebsocketClient(_config.WsUri, factory)
{ {
ReconnectTimeout = TimeSpan.FromSeconds(_config.ReconnectTimeout) IsReconnectionEnabled = false
}; };
_wsClient = client; _wsClient = client;
@@ -5,7 +5,6 @@ public class ChatClientConfigModel
public required Dictionary<string, string> Cookies { get; set; } = new(); public required Dictionary<string, string> Cookies { get; set; } = new();
// Currently wss://kiwifarms.net/chat.ws // Currently wss://kiwifarms.net/chat.ws
public required Uri WsUri { get; set; } public required Uri WsUri { get; set; }
public int ReconnectTimeout { get; set; } = 30;
public string CookieDomain { get; set; } = "kiwifarms.net"; public string CookieDomain { get; set; } = "kiwifarms.net";
public string? Proxy { get; set; } public string? Proxy { get; set; }
} }