Merge pull request #10 from StevanFreeborn/stevanfreeborn/fix/8-add-retry-logic

fix: make initial connection more resilient with retry logic
This commit is contained in:
Stevan Freeborn
2025-09-11 14:47:18 -05:00
committed by GitHub
2 changed files with 30 additions and 1 deletions
@@ -1,3 +1,4 @@
using Activity = StevesBot.Worker.Discord.Gateway.Events.Data.Activity;
namespace StevesBot.Worker.Discord.Gateway;
@@ -674,7 +675,34 @@ internal sealed class DiscordGatewayClient : IDiscordGatewayClient
throw new DiscordGatewayClientException("WebSocket is already open. Cannot connect.");
}
var connected = false;
var attempts = 0;
var delay = TimeSpan.FromSeconds(1);
while (connected is false)
{
try
{
await _webSocket.ConnectAsync(uri, cancellationToken);
connected = true;
}
catch (Exception e) when (e is WebSocketException)
{
var currentDelay = (int)(delay.TotalMilliseconds * Math.Pow(2, attempts));
_logger.LogWarning(
e,
"Failed to connect to {Uri} on {Attempt} attempt. Waiting for {Delay}ms before trying again",
uri,
attempts,
currentDelay
);
await Task.Delay(currentDelay, cancellationToken);
attempts++;
}
}
}
private static Uri BuildUri(string url)
+1
View File
@@ -1,3 +1,4 @@
global using System.Net;
global using System.Net.WebSockets;
global using System.Reflection;
global using System.Text;