@@ -42,10 +42,10 @@ type gatewayImpl struct {
42
42
closeHandlerFunc CloseHandlerFunc
43
43
token string
44
44
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
49
49
50
50
heartbeatInterval time.Duration
51
51
lastHeartbeatSent time.Time
@@ -150,9 +150,9 @@ func (g *gatewayImpl) Close(ctx context.Context) {
150
150
}
151
151
152
152
func (g * gatewayImpl ) CloseWithCode (ctx context.Context , code int , message string ) {
153
- if g .heartbeatChan != nil {
153
+ if g .heartbeatCancel != nil {
154
154
g .config .Logger .Debug ("closing heartbeat goroutines..." )
155
- g .heartbeatChan <- struct {}{}
155
+ g .heartbeatCancel ()
156
156
}
157
157
158
158
g .connMu .Lock ()
@@ -259,16 +259,16 @@ func (g *gatewayImpl) reconnect() {
259
259
}
260
260
261
261
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
+
265
265
heartbeatTicker := time .NewTicker (g .heartbeatInterval )
266
266
defer heartbeatTicker .Stop ()
267
267
defer g .config .Logger .Debug ("exiting heartbeat goroutine" )
268
268
269
269
for {
270
270
select {
271
- case <- g . heartbeatChan :
271
+ case <- ctx . Done () :
272
272
return
273
273
274
274
case <- heartbeatTicker .C :
0 commit comments