diff options
Diffstat (limited to 'pkg/discord/gateway')
| -rw-r--r-- | pkg/discord/gateway/gateway.go | 4 | ||||
| -rw-r--r-- | pkg/discord/gateway/heartbeat.go | 12 |
2 files changed, 9 insertions, 7 deletions
diff --git a/pkg/discord/gateway/gateway.go b/pkg/discord/gateway/gateway.go index 18cf708..d1311c2 100644 --- a/pkg/discord/gateway/gateway.go +++ b/pkg/discord/gateway/gateway.go @@ -7,6 +7,7 @@ import ( "jinx/pkg/discord/events" "jinx/pkg/libs/cancellablewebsocket" "net/http" + "time" "github.com/gorilla/websocket" "github.com/rs/zerolog" @@ -62,7 +63,8 @@ func (g *GatewayImpl) Start(ctx context.Context, url string, token string) error return err } - g.heartbeat.Start(ctx, hello.HeartbeatInterval) + heartbeatInterval := time.Duration(hello.HeartbeatInterval) * time.Millisecond + g.heartbeat.Start(ctx, heartbeatInterval) if err = g.identify(token); err != nil { return err diff --git a/pkg/discord/gateway/heartbeat.go b/pkg/discord/gateway/heartbeat.go index 6cefb21..a8f793d 100644 --- a/pkg/discord/gateway/heartbeat.go +++ b/pkg/discord/gateway/heartbeat.go @@ -9,7 +9,7 @@ import ( ) type Heartbeat interface { - Start(ctx context.Context, interval uint64) + Start(ctx context.Context, interval time.Duration) ForceHeartbeat() Ack() @@ -31,7 +31,7 @@ func NewHeartbeat(logger *zerolog.Logger, gateway Gateway) *HeartbeatImpl { } } -func (h *HeartbeatImpl) Start(ctx context.Context, interval uint64) { +func (h *HeartbeatImpl) Start(ctx context.Context, interval time.Duration) { h.ctx = ctx go h.heartbeatRoutine(interval) } @@ -45,14 +45,14 @@ func (h *HeartbeatImpl) Ack() { h.logger.Debug().Msg("received heartbeat ack") } -func (h *HeartbeatImpl) heartbeatRoutine(interval uint64) { - h.logger.Debug().Msgf("beating heart every %dms", interval) +func (h *HeartbeatImpl) heartbeatRoutine(interval time.Duration) { + h.logger.Debug().Msgf("beating heart every %dms", interval.Milliseconds()) // REF: heartbeat_interval * jitter jitter := rand.Intn(int(interval)) - time.Sleep(time.Duration(jitter) * time.Millisecond) + time.Sleep(time.Duration(jitter)) - ticker := time.NewTicker(time.Duration(interval) * time.Millisecond) + ticker := time.NewTicker(interval) defer ticker.Stop() for { |
