Skip to content

Commit 3a0a250

Browse files
committed
refactor: improve connection management and simplify handleMessage function
1 parent 948015d commit 3a0a250

File tree

6 files changed

+364
-166
lines changed

6 files changed

+364
-166
lines changed

gateway/gateway.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -239,19 +239,22 @@ func (g *gatewayImpl) open(ctx context.Context) error {
239239
g.lastHeartbeatSent = time.Now().UTC()
240240
conn, rs, err := g.config.Dialer.DialContext(ctx, gatewayURL, nil)
241241
if err != nil {
242-
body := ""
243-
if rs != nil && rs.Body != nil {
242+
var body []byte
243+
if rs != nil {
244244
defer func() {
245245
_ = rs.Body.Close()
246246
}()
247-
rawBody, bErr := io.ReadAll(rs.Body)
248-
if bErr != nil {
249-
g.config.Logger.ErrorContext(ctx, "error while reading response body", slog.Any("err", bErr))
247+
body, err = io.ReadAll(rs.Body)
248+
if err != nil {
249+
g.config.Logger.ErrorContext(ctx, "error while reading response body", slog.Any("err", err))
250250
}
251-
body = string(rawBody)
252251
}
253252

254-
g.config.Logger.ErrorContext(ctx, "error connecting to the gateway", slog.Any("err", err), slog.String("url", gatewayURL), slog.String("body", body))
253+
g.config.Logger.ErrorContext(ctx, "error connecting to the gateway",
254+
slog.Any("err", err),
255+
slog.String("url", gatewayURL),
256+
slog.String("body", string(body)),
257+
)
255258
g.connMu.Unlock()
256259
return err
257260
}
@@ -303,7 +306,7 @@ func (g *gatewayImpl) Close(ctx context.Context) {
303306

304307
func (g *gatewayImpl) CloseWithCode(ctx context.Context, code int, message string) {
305308
if g.heartbeatCancel != nil {
306-
g.config.Logger.DebugContext(ctx, "closing heartbeat goroutines...")
309+
g.config.Logger.DebugContext(ctx, "closing heartbeat goroutine")
307310
g.heartbeatCancel()
308311
}
309312

@@ -382,8 +385,10 @@ func (g *gatewayImpl) Presence() *MessageDataPresenceUpdate {
382385
}
383386

384387
func (g *gatewayImpl) doReconnect(ctx context.Context) error {
385-
try := 0
386-
backoffIncrement := 0
388+
var (
389+
try int
390+
backoffIncrement int
391+
)
387392

388393
for {
389394
// Exponentially backoff up to a limit of 10s

gateway/gateway_messages.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ func (e *Message) UnmarshalJSON(data []byte) error {
6565
messageData = d
6666

6767
case OpcodeReconnect:
68+
messageData = MessageDataReconnect{}
6869

6970
case OpcodeRequestGuildMembers:
7071
var d MessageDataRequestGuildMembers
@@ -82,6 +83,7 @@ func (e *Message) UnmarshalJSON(data []byte) error {
8283
messageData = d
8384

8485
case OpcodeHeartbeatACK:
86+
messageData = MessageDataHeartbeatACK{}
8587

8688
case OpcodeRequestSoundboardSounds:
8789
var d MessageDataRequestSoundboardSounds
@@ -658,6 +660,10 @@ type MessageDataResume struct {
658660

659661
func (MessageDataResume) messageData() {}
660662

663+
type MessageDataReconnect struct{}
664+
665+
func (MessageDataReconnect) messageData() {}
666+
661667
// MessageDataRequestGuildMembers is used for fetching all the members of a guild_events. It is recommended you have a strict
662668
// member caching policy when using this.
663669
type MessageDataRequestGuildMembers struct {
@@ -681,6 +687,10 @@ type MessageDataHello struct {
681687

682688
func (MessageDataHello) messageData() {}
683689

690+
type MessageDataHeartbeatACK struct{}
691+
692+
func (MessageDataHeartbeatACK) messageData() {}
693+
684694
type MessageDataRequestSoundboardSounds struct {
685695
GuildIDs []snowflake.ID `json:"guild_ids"`
686696
}

voice/conn.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func (c *connImpl) HandleVoiceServerUpdate(update botgateway.EventVoiceServerUpd
194194
}()
195195
}
196196

197-
func (c *connImpl) handleMessage(gateway Gateway, op Opcode, data GatewayMessageData) {
197+
func (c *connImpl) handleMessage(gateway Gateway, op Opcode, sequenceNumber int, data GatewayMessageData) {
198198
switch d := data.(type) {
199199
case GatewayMessageDataReady:
200200
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
@@ -238,7 +238,7 @@ func (c *connImpl) handleMessage(gateway Gateway, op Opcode, data GatewayMessage
238238
}
239239
}
240240
if c.config.EventHandlerFunc != nil {
241-
c.config.EventHandlerFunc(c.gateway, op, data)
241+
c.config.EventHandlerFunc(gateway, op, sequenceNumber, data)
242242
}
243243
}
244244

0 commit comments

Comments
 (0)