Skip to content

Commit

Permalink
allow multiple gateway closes without blocking forever (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Mar 29, 2024
1 parent 223c084 commit 9b1dddf
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions gateway/gateway_impl.go
Expand Up @@ -42,10 +42,10 @@ type gatewayImpl struct {
closeHandlerFunc CloseHandlerFunc
token string

conn *websocket.Conn
connMu sync.Mutex
heartbeatChan chan struct{}
status Status
conn *websocket.Conn
connMu sync.Mutex
heartbeatCancel context.CancelFunc
status Status

heartbeatInterval time.Duration
lastHeartbeatSent time.Time
Expand Down Expand Up @@ -132,9 +132,9 @@ func (g *gatewayImpl) Close(ctx context.Context) {
}

func (g *gatewayImpl) CloseWithCode(ctx context.Context, code int, message string) {
if g.heartbeatChan != nil {
if g.heartbeatCancel != nil {
g.config.Logger.Debug("closing heartbeat goroutines...")
g.heartbeatChan <- struct{}{}
g.heartbeatCancel()
}

g.connMu.Lock()
Expand Down Expand Up @@ -234,16 +234,16 @@ func (g *gatewayImpl) reconnect() {
}

func (g *gatewayImpl) heartbeat() {
if g.heartbeatChan == nil {
g.heartbeatChan = make(chan struct{})
}
ctx, cancel := context.WithCancel(context.Background())
g.heartbeatCancel = cancel

heartbeatTicker := time.NewTicker(g.heartbeatInterval)
defer heartbeatTicker.Stop()
defer g.config.Logger.Debug("exiting heartbeat goroutine")

for {
select {
case <-g.heartbeatChan:
case <-ctx.Done():
return

case <-heartbeatTicker.C:
Expand Down

0 comments on commit 9b1dddf

Please sign in to comment.