From 0f48c6879ba080c6e36afe264981b9a7d82e385f Mon Sep 17 00:00:00 2001 From: Ske Date: Thu, 31 Oct 2019 17:21:12 +0100 Subject: [PATCH] Attempt to fix slow/timeout-y bot startup I've noticed the bot's CPU spiking for a few minutes as it's just connecting, and a lot of shard connections are timing out. I suspect it might be the overload of messages as the shards connect and Discord sends it the entire backlog at once. So, as an interim solution, I'm making it simply discard every message before the socket is fully connected. Hopefully this should help reduce the pressure. --- PluralKit.Bot/Bot.cs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/PluralKit.Bot/Bot.cs b/PluralKit.Bot/Bot.cs index c82d2133..fe34f790 100644 --- a/PluralKit.Bot/Bot.cs +++ b/PluralKit.Bot/Bot.cs @@ -86,7 +86,10 @@ namespace PluralKit.Bot { MessageCacheSize = 5, ExclusiveBulkDelete = true, - DefaultRetryMode = RetryMode.AlwaysRetry + DefaultRetryMode = RetryMode.AlwaysRetry, + // Commented this out since Debug actually sends, uh, quite a lot that's not necessary in production + // but leaving it here in case I (or someone else) get[s] confused about why logging isn't working again :p + // LogLevel = LogSeverity.Debug // We filter log levels in Serilog, so just pass everything through (Debug is lower than Verbose) })) .AddSingleton() .AddTransient() @@ -172,10 +175,10 @@ namespace PluralKit.Bot level = LogEventLevel.Error; else if (msg.Severity == LogSeverity.Info) level = LogEventLevel.Information; - else if (msg.Severity == LogSeverity.Verbose) + else if (msg.Severity == LogSeverity.Debug) // D.NET's lowest level is Debug and Verbose is greater, Serilog's is the other way around level = LogEventLevel.Verbose; - else if (msg.Severity == LogSeverity.Warning) - level = LogEventLevel.Warning; + else if (msg.Severity == LogSeverity.Verbose) + level = LogEventLevel.Debug; _logger.Write(level, msg.Exception, "Discord.Net {Source}: {Message}", msg.Source, msg.Message); return Task.CompletedTask; @@ -318,6 +321,9 @@ namespace PluralKit.Bot public async Task HandleMessage(SocketMessage arg) { + if (_client.GetShardFor((arg.Channel as IGuildChannel)?.Guild).ConnectionState != ConnectionState.Connected) + return; // Discard messages while the bot "catches up" to avoid unnecessary CPU pressure causing timeouts + RegisterMessageMetrics(arg); // Ignore system messages (member joined, message pinned, etc)