Skip to content

Commit 9b1dddf

Browse files
authored
allow multiple gateway closes without blocking forever (#339)
1 parent 223c084 commit 9b1dddf

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
@@ -132,9 +132,9 @@ func (g *gatewayImpl) Close(ctx context.Context) {
132132
}
133133

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

140140
g.connMu.Lock()
@@ -234,16 +234,16 @@ func (g *gatewayImpl) reconnect() {
234234
}
235235

236236
func (g *gatewayImpl) heartbeat() {
237-
if g.heartbeatChan == nil {
238-
g.heartbeatChan = make(chan struct{})
239-
}
237+
ctx, cancel := context.WithCancel(context.Background())
238+
g.heartbeatCancel = cancel
239+
240240
heartbeatTicker := time.NewTicker(g.heartbeatInterval)
241241
defer heartbeatTicker.Stop()
242242
defer g.config.Logger.Debug("exiting heartbeat goroutine")
243243

244244
for {
245245
select {
246-
case <-g.heartbeatChan:
246+
case <-ctx.Done():
247247
return
248248

249249
case <-heartbeatTicker.C:

0 commit comments

Comments
 (0)