Skip to content

Commit 579ce18

Browse files
committed
Cleanup ping listener map
1 parent 8af7761 commit 579ce18

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

websocket.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ type Conn struct {
4848
setConnContext chan context.Context
4949
getConnContext chan context.Context
5050

51-
pingListener map[string]chan<- struct{}
51+
pingListenerMu sync.Mutex
52+
pingListener map[string]chan<- struct{}
5253
}
5354

5455
// Context returns a context derived from parent that will be cancelled
@@ -254,7 +255,9 @@ func (c *Conn) handleControl(h header) {
254255
case opPing:
255256
c.writePong(b)
256257
case opPong:
258+
c.pingListenerMu.Lock()
257259
listener, ok := c.pingListener[string(b)]
260+
c.pingListenerMu.Unlock()
258261
if ok {
259262
close(listener)
260263
}
@@ -717,7 +720,16 @@ func (c *Conn) ping(ctx context.Context) error {
717720
p := strconv.FormatUint(id, 10)
718721

719722
pong := make(chan struct{})
723+
724+
c.pingListenerMu.Lock()
720725
c.pingListener[p] = pong
726+
c.pingListenerMu.Unlock()
727+
728+
defer func() {
729+
c.pingListenerMu.Lock()
730+
delete(c.pingListener, p)
731+
c.pingListenerMu.Unlock()
732+
}()
721733

722734
err := c.writeMessage(ctx, opPing, []byte(p))
723735
if err != nil {

0 commit comments

Comments
 (0)