From 4baf209d1ad28feeaca49b331ab175c06690300e Mon Sep 17 00:00:00 2001 From: Candygoblen123 Date: Wed, 25 Mar 2026 17:44:34 -0400 Subject: [PATCH] dont use multiple threads to respond to messages --- Sources/DiscordKit/GatewayClient.swift | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Sources/DiscordKit/GatewayClient.swift b/Sources/DiscordKit/GatewayClient.swift index ee1c5b3..4e4d89a 100644 --- a/Sources/DiscordKit/GatewayClient.swift +++ b/Sources/DiscordKit/GatewayClient.swift @@ -44,6 +44,11 @@ actor GatewayClient { while !Task.isCancelled { try await sendHeartbeat() try await Task.sleep(for: .milliseconds(helloMessage.heartbeat_interval)) + + if await !hbAck { + // haven't heard back + await ws.cancel() + } } } catch { print("Heartbeat task canceled") @@ -127,7 +132,8 @@ actor GatewayClient { private func attemptResume() async throws { guard - ws.closeCode.rawValue != 4004 && ws.closeCode.rawValue < 4010, + ws.closeCode.rawValue != 4004 && ws.closeCode.rawValue != 1000 && + ws.closeCode.rawValue != 1001 && ws.closeCode.rawValue < 4010, let resumeURL = resumeURL, let sessionID = sessionID, let sequenceNum = sequenceNum @@ -164,10 +170,6 @@ actor GatewayClient { hbAck = false let hbMessage = "{\"op\":1,\"d\":\(sequenceNum == nil ? "null" : String(sequenceNum!))}" try await ws.send(.string(hbMessage)) - try await Task.sleep(for: .seconds(5)) - if !hbAck { - ws.cancel(with: .normalClosure, reason: nil) - } } var events: AsyncStream {