fix(bot): ratelimit only initial websocket connection

This commit is contained in:
alyssa 2025-01-02 18:50:02 +00:00
parent efdbf708a0
commit 40b6ab1d87

View file

@ -43,6 +43,12 @@ public class Shard
private GatewayStatusUpdate? _presence { get; init; } private GatewayStatusUpdate? _presence { get; init; }
// opening hundreds of websocket connections simultaneously (at cold start) breaks *something*,
// so we use the identify ratelimiter to delay them
// we can't do this on later reconnections though, since it will time the session out and fail
// to resume
private bool _didInitialIdentify { get; set; } = false;
public Shard(GatewaySettings settings, ShardInfo info, IGatewayRatelimiter ratelimiter, string url, ILogger logger, GatewayStatusUpdate? presence = null) public Shard(GatewaySettings settings, ShardInfo info, IGatewayRatelimiter ratelimiter, string url, ILogger logger, GatewayStatusUpdate? presence = null)
{ {
_jsonSerializerOptions = new JsonSerializerOptions().ConfigureForMyriad(); _jsonSerializerOptions = new JsonSerializerOptions().ConfigureForMyriad();
@ -132,6 +138,7 @@ public class Shard
{ {
while (true) while (true)
{ {
if (!_didInitialIdentify)
await _ratelimiter.Identify(_info.ShardId); await _ratelimiter.Identify(_info.ShardId);
_logger.Information("Shard {ShardId}: Connecting to WebSocket", _info.ShardId); _logger.Information("Shard {ShardId}: Connecting to WebSocket", _info.ShardId);
@ -152,7 +159,12 @@ public class Shard
=> _conn.Disconnect(closeStatus, null); => _conn.Disconnect(closeStatus, null);
private async Task SendIdentify() private async Task SendIdentify()
=> await _conn.Send(new GatewayPacket {
if (_didInitialIdentify)
await _ratelimiter.Identify(_info.ShardId);
_didInitialIdentify = true;
await _conn.Send(new GatewayPacket
{ {
Opcode = GatewayOpcode.Identify, Opcode = GatewayOpcode.Identify,
Payload = new GatewayIdentify Payload = new GatewayIdentify
@ -171,6 +183,7 @@ public class Shard
Presence = _presence, Presence = _presence,
} }
}); });
}
private async Task SendResume((string SessionId, int? LastSeq) arg) private async Task SendResume((string SessionId, int? LastSeq) arg)
=> await _conn.Send(new GatewayPacket => await _conn.Send(new GatewayPacket