@@ -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
@@ -132,9 +132,9 @@ func (g *gatewayImpl) Close(ctx context.Context) {
132
132
}
133
133
134
134
func (g * gatewayImpl ) CloseWithCode (ctx context.Context , code int , message string ) {
135
- if g .heartbeatChan != nil {
135
+ if g .heartbeatCancel != nil {
136
136
g .config .Logger .Debug ("closing heartbeat goroutines..." )
137
- g .heartbeatChan <- struct {}{}
137
+ g .heartbeatCancel ()
138
138
}
139
139
140
140
g .connMu .Lock ()
@@ -234,16 +234,16 @@ func (g *gatewayImpl) reconnect() {
234
234
}
235
235
236
236
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
+
240
240
heartbeatTicker := time .NewTicker (g .heartbeatInterval )
241
241
defer heartbeatTicker .Stop ()
242
242
defer g .config .Logger .Debug ("exiting heartbeat goroutine" )
243
243
244
244
for {
245
245
select {
246
- case <- g . heartbeatChan :
246
+ case <- ctx . Done () :
247
247
return
248
248
249
249
case <- heartbeatTicker .C :
0 commit comments