Skip to content

Commit e931ead

Browse files
topi314apricotbucket28
authored andcommitted
allow multiple gateway closes without blocking forever (disgoorg#339)
1 parent 9adff17 commit e931ead

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

gateway/gateway_impl.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ type gatewayImpl struct {
4242
closeHandlerFunc CloseHandlerFunc
4343
token string
4444

45-
conn *websocket.Conn
46-
connMu sync.Mutex
47-
heartbeatChan chan struct{}
48-
status Status
45+
conn *websocket.Conn
46+
connMu sync.Mutex
47+
heartbeatCancel context.CancelFunc
48+
status Status
4949

5050
heartbeatInterval time.Duration
5151
lastHeartbeatSent time.Time
@@ -150,9 +150,9 @@ func (g *gatewayImpl) Close(ctx context.Context) {
150150
}
151151

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

158158
g.connMu.Lock()
@@ -259,16 +259,16 @@ func (g *gatewayImpl) reconnect() {
259259
}
260260

261261
func (g *gatewayImpl) heartbeat() {
262-
if g.heartbeatChan == nil {
263-
g.heartbeatChan = make(chan struct{})
264-
}
262+
ctx, cancel := context.WithCancel(context.Background())
263+
g.heartbeatCancel = cancel
264+
265265
heartbeatTicker := time.NewTicker(g.heartbeatInterval)
266266
defer heartbeatTicker.Stop()
267267
defer g.config.Logger.Debug("exiting heartbeat goroutine")
268268

269269
for {
270270
select {
271-
case <-g.heartbeatChan:
271+
case <-ctx.Done():
272272
return
273273

274274
case <-heartbeatTicker.C:

0 commit comments

Comments
 (0)