-
Notifications
You must be signed in to change notification settings - Fork 311
/
Copy pathwebsocket.go
854 lines (734 loc) · 20.2 KB
/
websocket.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
package websocket
import (
"bufio"
"context"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"runtime"
"strconv"
"sync"
"time"
"golang.org/x/xerrors"
)
// Conn represents a WebSocket connection.
// All methods may be called concurrently except for Reader, Read
// and SetReadLimit.
//
// Please be sure to call Close on the connection when you
// are finished with it to release the associated resources.
type Conn struct {
subprotocol string
br *bufio.Reader
bw *bufio.Writer
closer io.Closer
client bool
// read limit for a message in bytes.
msgReadLimit int64
closeOnce sync.Once
closeErr error
closed chan struct{}
// writeMsgLock is acquired to write a data message.
writeMsgLock chan struct{}
// writeFrameLock is acquired to write a single frame.
// Effectively meaning whoever holds it gets to write to bw.
writeFrameLock chan struct{}
// Used to ensure the previous reader is read till EOF before allowing
// a new one.
previousReader *messageReader
// readFrameLock is acquired to read from bw.
readFrameLock chan struct{}
// readMsg is used by messageReader to receive frames from
// readLoop.
readMsg chan header
// readMsgDone is used to tell the readLoop to continue after
// messageReader has read a frame.
readMsgDone chan struct{}
setReadTimeout chan context.Context
setWriteTimeout chan context.Context
setConnContext chan context.Context
getConnContext chan context.Context
activePingsMu sync.Mutex
activePings map[string]chan<- struct{}
}
func (c *Conn) init() {
c.closed = make(chan struct{})
c.msgReadLimit = 32768
c.writeMsgLock = make(chan struct{}, 1)
c.writeFrameLock = make(chan struct{}, 1)
c.readFrameLock = make(chan struct{}, 1)
c.readMsg = make(chan header)
c.readMsgDone = make(chan struct{})
c.setReadTimeout = make(chan context.Context)
c.setWriteTimeout = make(chan context.Context)
c.setConnContext = make(chan context.Context)
c.getConnContext = make(chan context.Context)
c.activePings = make(map[string]chan<- struct{})
runtime.SetFinalizer(c, func(c *Conn) {
c.close(xerrors.New("connection garbage collected"))
})
go c.timeoutLoop()
go c.readLoop()
}
// Subprotocol returns the negotiated subprotocol.
// An empty string means the default protocol.
func (c *Conn) Subprotocol() string {
return c.subprotocol
}
func (c *Conn) close(err error) {
c.closeOnce.Do(func() {
runtime.SetFinalizer(c, nil)
c.closeErr = xerrors.Errorf("websocket closed: %w", err)
close(c.closed)
// Have to close after c.closed is closed to ensure any goroutine that wakes up
// from the connection being closed also sees that c.closed is closed and returns
// closeErr.
c.closer.Close()
// See comment in dial.go
if c.client {
// By acquiring the locks, we ensure no goroutine will touch the bufio reader or writer
// and we can safely return them.
// Whenever a caller holds this lock and calls close, it ensures to release the lock to prevent
// a deadlock.
// As of now, this is in writeFrame, readFramePayload and readHeader.
c.readFrameLock <- struct{}{}
returnBufioReader(c.br)
c.writeFrameLock <- struct{}{}
returnBufioWriter(c.bw)
}
})
}
func (c *Conn) timeoutLoop() {
readCtx := context.Background()
writeCtx := context.Background()
parentCtx := context.Background()
for {
select {
case <-c.closed:
return
case writeCtx = <-c.setWriteTimeout:
case readCtx = <-c.setReadTimeout:
case <-readCtx.Done():
c.close(xerrors.Errorf("data read timed out: %w", readCtx.Err()))
case <-writeCtx.Done():
c.close(xerrors.Errorf("data write timed out: %w", writeCtx.Err()))
case <-parentCtx.Done():
c.close(xerrors.Errorf("parent context cancelled: %w", parentCtx.Err()))
return
case parentCtx = <-c.setConnContext:
ctx, cancelCtx := context.WithCancel(parentCtx)
defer cancelCtx()
select {
case <-c.closed:
return
case c.getConnContext <- ctx:
}
}
}
}
// Context returns a context derived from parent that will be cancelled
// when the connection is closed or broken.
// If the parent context is cancelled, the connection will be closed.
//
// This is an experimental API.
// Please let me know how you feel about it in https://github.com/nhooyr/websocket/issues/79
func (c *Conn) Context(parent context.Context) context.Context {
select {
case <-c.closed:
ctx, cancel := context.WithCancel(parent)
cancel()
return ctx
case c.setConnContext <- parent:
}
select {
case <-c.closed:
ctx, cancel := context.WithCancel(parent)
cancel()
return ctx
case ctx := <-c.getConnContext:
return ctx
}
}
func (c *Conn) acquireLock(ctx context.Context, lock chan struct{}) error {
select {
case <-ctx.Done():
var err error
switch lock {
case c.writeFrameLock, c.writeMsgLock:
err = xerrors.Errorf("could not acquire write lock: %v", ctx.Err())
case c.readFrameLock:
err = xerrors.Errorf("could not acquire read lock: %v", ctx.Err())
default:
panic(fmt.Sprintf("websocket: failed to acquire unknown lock: %v", ctx.Err()))
}
c.close(err)
return ctx.Err()
case <-c.closed:
return c.closeErr
case lock <- struct{}{}:
return nil
}
}
func (c *Conn) releaseLock(lock chan struct{}) {
// Allow multiple releases.
select {
case <-lock:
default:
}
}
func (c *Conn) readLoop() {
for {
h, err := c.readTillMsg()
if err != nil {
return
}
select {
case <-c.closed:
return
case c.readMsg <- h:
}
select {
case <-c.closed:
return
case <-c.readMsgDone:
}
}
}
func (c *Conn) readTillMsg() (header, error) {
for {
h, err := c.readFrameHeader()
if err != nil {
return header{}, err
}
if h.rsv1 || h.rsv2 || h.rsv3 {
err := xerrors.Errorf("received header with rsv bits set: %v:%v:%v", h.rsv1, h.rsv2, h.rsv3)
c.Close(StatusProtocolError, err.Error())
return header{}, err
}
if h.opcode.controlOp() {
c.handleControl(h)
continue
}
switch h.opcode {
case opBinary, opText, opContinuation:
return h, nil
default:
err := xerrors.Errorf("received unknown opcode %v", h.opcode)
c.Close(StatusProtocolError, err.Error())
return header{}, err
}
}
}
func (c *Conn) readFrameHeader() (header, error) {
err := c.acquireLock(context.Background(), c.readFrameLock)
if err != nil {
return header{}, err
}
defer c.releaseLock(c.readFrameLock)
h, err := readHeader(c.br)
if err != nil {
err := xerrors.Errorf("failed to read header: %w", err)
c.releaseLock(c.readFrameLock)
c.close(err)
return header{}, err
}
return h, nil
}
func (c *Conn) handleControl(h header) {
if h.payloadLength > maxControlFramePayload {
c.Close(StatusProtocolError, "control frame too large")
return
}
if !h.fin {
c.Close(StatusProtocolError, "control frame cannot be fragmented")
return
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
b := make([]byte, h.payloadLength)
_, err := c.readFramePayload(ctx, b)
if err != nil {
return
}
if h.masked {
fastXOR(h.maskKey, 0, b)
}
switch h.opcode {
case opPing:
c.writePong(b)
case opPong:
c.activePingsMu.Lock()
pong, ok := c.activePings[string(b)]
c.activePingsMu.Unlock()
if ok {
close(pong)
}
case opClose:
ce, err := parseClosePayload(b)
if err != nil {
c.close(xerrors.Errorf("received invalid close payload: %w", err))
return
}
if ce.Code == StatusNoStatusRcvd {
c.writeClose(nil, ce)
} else {
c.Close(ce.Code, ce.Reason)
}
default:
panic(fmt.Sprintf("websocket: unexpected control opcode: %#v", h))
}
}
// Reader waits until there is a WebSocket data message to read
// from the connection.
// It returns the type of the message and a reader to read it.
// The passed context will also bound the reader.
// Ensure you read to EOF otherwise the connection will hang.
//
// Control (ping, pong, close) frames will be handled automatically
// in a separate goroutine so if you do not expect any data messages,
// you do not need to read from the connection. However, if the peer
// sends a data message, further pings, pongs and close frames will not
// be read if you do not read the message from the connection.
//
// Only one Reader may be open at a time.
func (c *Conn) Reader(ctx context.Context) (MessageType, io.Reader, error) {
typ, r, err := c.reader(ctx)
if err != nil {
return 0, nil, xerrors.Errorf("failed to get reader: %w", err)
}
return typ, &limitedReader{
c: c,
r: r,
left: c.msgReadLimit,
}, nil
}
func (c *Conn) reader(ctx context.Context) (MessageType, io.Reader, error) {
if c.previousReader != nil && c.previousReader.h != nil {
// The only way we know for sure the previous reader is not yet complete is
// if there is an active frame not yet fully read.
// Otherwise, a user may have read the last byte but not the EOF if the EOF
// is in the next frame so we check for that below.
return 0, nil, xerrors.Errorf("previous message not read to completion")
}
select {
case <-c.closed:
return 0, nil, c.closeErr
case <-ctx.Done():
return 0, nil, ctx.Err()
case h := <-c.readMsg:
if c.previousReader != nil && !c.previousReader.done {
if h.opcode != opContinuation {
err := xerrors.Errorf("received new data message without finishing the previous message")
c.Close(StatusProtocolError, err.Error())
return 0, nil, err
}
if !h.fin || h.payloadLength > 0 {
return 0, nil, xerrors.Errorf("previous message not read to completion")
}
c.previousReader.done = true
select {
case <-c.closed:
return 0, nil, c.closeErr
case c.readMsgDone <- struct{}{}:
}
return c.reader(ctx)
} else if h.opcode == opContinuation {
err := xerrors.Errorf("received continuation frame not after data or text frame")
c.Close(StatusProtocolError, err.Error())
return 0, nil, err
}
r := &messageReader{
ctx: ctx,
c: c,
h: &h,
}
c.previousReader = r
return MessageType(h.opcode), r, nil
}
}
// messageReader enables reading a data frame from the WebSocket connection.
type messageReader struct {
ctx context.Context
c *Conn
h *header
maskPos int
done bool
}
// Read reads as many bytes as possible into p.
func (r *messageReader) Read(p []byte) (int, error) {
n, err := r.read(p)
if err != nil {
// Have to return io.EOF directly for now, we cannot wrap as xerrors
// isn't used in stdlib.
if xerrors.Is(err, io.EOF) {
return n, io.EOF
}
return n, xerrors.Errorf("failed to read: %w", err)
}
return n, nil
}
func (r *messageReader) read(p []byte) (int, error) {
if r.done {
return 0, xerrors.Errorf("cannot use EOFed reader")
}
if r.h == nil {
select {
case <-r.c.closed:
return 0, r.c.closeErr
case <-r.ctx.Done():
r.c.close(xerrors.Errorf("failed to read: %w", r.ctx.Err()))
return 0, r.ctx.Err()
case h := <-r.c.readMsg:
if h.opcode != opContinuation {
err := xerrors.Errorf("received new data frame without finishing the previous frame")
r.c.Close(StatusProtocolError, err.Error())
return 0, err
}
r.h = &h
}
}
if int64(len(p)) > r.h.payloadLength {
p = p[:r.h.payloadLength]
}
n, err := r.c.readFramePayload(r.ctx, p)
r.h.payloadLength -= int64(n)
if r.h.masked {
r.maskPos = fastXOR(r.h.maskKey, r.maskPos, p)
}
if err != nil {
return n, err
}
if r.h.payloadLength == 0 {
select {
case <-r.c.closed:
return n, r.c.closeErr
case r.c.readMsgDone <- struct{}{}:
}
fin := r.h.fin
// Need to nil this as Reader uses it to check
// whether there is active data on the previous reader and
// now there isn't.
r.h = nil
if fin {
r.done = true
return n, io.EOF
}
r.maskPos = 0
}
return n, nil
}
func (c *Conn) readFramePayload(ctx context.Context, p []byte) (int, error) {
err := c.acquireLock(ctx, c.readFrameLock)
if err != nil {
return 0, err
}
defer c.releaseLock(c.readFrameLock)
select {
case <-c.closed:
return 0, c.closeErr
case c.setReadTimeout <- ctx:
}
n, err := io.ReadFull(c.br, p)
if err != nil {
select {
case <-c.closed:
return n, c.closeErr
case <-ctx.Done():
err = ctx.Err()
default:
}
err = xerrors.Errorf("failed to read from connection: %w", err)
c.releaseLock(c.readFrameLock)
c.close(err)
return n, err
}
select {
case <-c.closed:
return n, c.closeErr
case c.setReadTimeout <- context.Background():
}
return n, err
}
// SetReadLimit sets the max number of bytes to read for a single message.
// It applies to the Reader and Read methods.
//
// By default, the connection has a message read limit of 32768 bytes.
//
// When the limit is hit, the connection will be closed with StatusPolicyViolation.
func (c *Conn) SetReadLimit(n int64) {
c.msgReadLimit = n
}
// Read is a convenience method to read a single message from the connection.
//
// See the Reader method if you want to be able to reuse buffers or want to stream a message.
// The docs on Reader apply to this method as well.
//
// This is an experimental API, please let me know how you feel about it in
// https://github.com/nhooyr/websocket/issues/62
func (c *Conn) Read(ctx context.Context) (MessageType, []byte, error) {
typ, r, err := c.Reader(ctx)
if err != nil {
return 0, nil, err
}
b, err := ioutil.ReadAll(r)
return typ, b, err
}
// Writer returns a writer bounded by the context that will write
// a WebSocket message of type dataType to the connection.
//
// You must close the writer once you have written the entire message.
//
// Only one writer can be open at a time, multiple calls will block until the previous writer
// is closed.
func (c *Conn) Writer(ctx context.Context, typ MessageType) (io.WriteCloser, error) {
wc, err := c.writer(ctx, typ)
if err != nil {
return nil, xerrors.Errorf("failed to get writer: %w", err)
}
return wc, nil
}
func (c *Conn) writer(ctx context.Context, typ MessageType) (io.WriteCloser, error) {
err := c.acquireLock(ctx, c.writeMsgLock)
if err != nil {
return nil, err
}
return &messageWriter{
ctx: ctx,
opcode: opcode(typ),
c: c,
}, nil
}
// Write is a convenience method to write a message to the connection.
//
// See the Writer method if you want to stream a message. The docs on Writer
// regarding concurrency also apply to this method.
//
// This is an experimental API, please let me know how you feel about it in
// https://github.com/nhooyr/websocket/issues/62
func (c *Conn) Write(ctx context.Context, typ MessageType, p []byte) error {
err := c.write(ctx, typ, p)
if err != nil {
return xerrors.Errorf("failed to write msg: %w", err)
}
return nil
}
func (c *Conn) write(ctx context.Context, typ MessageType, p []byte) error {
err := c.acquireLock(ctx, c.writeMsgLock)
if err != nil {
return err
}
defer c.releaseLock(c.writeMsgLock)
err = c.writeFrame(ctx, true, opcode(typ), p)
return err
}
// messageWriter enables writing to a WebSocket connection.
type messageWriter struct {
ctx context.Context
opcode opcode
c *Conn
closed bool
}
// Write writes the given bytes to the WebSocket connection.
func (w *messageWriter) Write(p []byte) (int, error) {
n, err := w.write(p)
if err != nil {
return n, xerrors.Errorf("failed to write: %w", err)
}
return n, nil
}
func (w *messageWriter) write(p []byte) (int, error) {
if w.closed {
return 0, xerrors.Errorf("cannot use closed writer")
}
err := w.c.writeFrame(w.ctx, false, w.opcode, p)
if err != nil {
return 0, xerrors.Errorf("failed to write data frame: %w", err)
}
w.opcode = opContinuation
return len(p), nil
}
// Close flushes the frame to the connection.
// This must be called for every messageWriter.
func (w *messageWriter) Close() error {
err := w.close()
if err != nil {
return xerrors.Errorf("failed to close writer: %w", err)
}
return nil
}
func (w *messageWriter) close() error {
if w.closed {
return xerrors.Errorf("cannot use closed writer")
}
w.closed = true
err := w.c.writeFrame(w.ctx, true, w.opcode, nil)
if err != nil {
return xerrors.Errorf("failed to write fin frame: %w", err)
}
w.c.releaseLock(w.c.writeMsgLock)
return nil
}
func (c *Conn) writeControl(ctx context.Context, opcode opcode, p []byte) error {
err := c.writeFrame(ctx, true, opcode, p)
if err != nil {
return xerrors.Errorf("failed to write control frame: %w", err)
}
return nil
}
// writeFrame handles all writes to the connection.
// We never mask inside here because our mask key is always 0,0,0,0.
// See comment on secWebSocketKey for why.
func (c *Conn) writeFrame(ctx context.Context, fin bool, opcode opcode, p []byte) error {
h := header{
fin: fin,
opcode: opcode,
masked: c.client,
payloadLength: int64(len(p)),
}
b2 := marshalHeader(h)
err := c.acquireLock(ctx, c.writeFrameLock)
if err != nil {
return err
}
defer c.releaseLock(c.writeFrameLock)
select {
case <-c.closed:
return c.closeErr
case c.setWriteTimeout <- ctx:
}
writeErr := func(err error) error {
select {
case <-c.closed:
return c.closeErr
case <-ctx.Done():
err = ctx.Err()
default:
}
err = xerrors.Errorf("failed to write to connection: %w", err)
// We need to release the lock first before closing the connection to ensure
// the lock can be acquired inside close to ensure no one can access c.bw.
c.releaseLock(c.writeFrameLock)
c.close(err)
return err
}
_, err = c.bw.Write(b2)
if err != nil {
return writeErr(err)
}
_, err = c.bw.Write(p)
if err != nil {
return writeErr(err)
}
if fin {
err = c.bw.Flush()
if err != nil {
return writeErr(err)
}
}
// We already finished writing, no need to potentially brick the connection if
// the context expires.
select {
case <-c.closed:
return c.closeErr
case c.setWriteTimeout <- context.Background():
}
return nil
}
func (c *Conn) writePong(p []byte) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
err := c.writeControl(ctx, opPong, p)
return err
}
// Close closes the WebSocket connection with the given status code and reason.
//
// It will write a WebSocket close frame with a timeout of 5 seconds.
// The connection can only be closed once. Additional calls to Close
// are no-ops.
//
// The maximum length of reason must be 125 bytes otherwise an internal
// error will be sent to the peer. For this reason, you should avoid
// sending a dynamic reason.
//
// Close will unblock all goroutines interacting with the connection.
func (c *Conn) Close(code StatusCode, reason string) error {
err := c.exportedClose(code, reason)
if err != nil {
return xerrors.Errorf("failed to close connection: %w", err)
}
return nil
}
func (c *Conn) exportedClose(code StatusCode, reason string) error {
ce := CloseError{
Code: code,
Reason: reason,
}
// This function also will not wait for a close frame from the peer like the RFC
// wants because that makes no sense and I don't think anyone actually follows that.
// Definitely worth seeing what popular browsers do later.
p, err := ce.bytes()
if err != nil {
fmt.Fprintf(os.Stderr, "websocket: failed to marshal close frame: %v\n", err)
ce = CloseError{
Code: StatusInternalError,
}
p, _ = ce.bytes()
}
return c.writeClose(p, ce)
}
func (c *Conn) writeClose(p []byte, cerr CloseError) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
err := c.writeControl(ctx, opClose, p)
if err != nil {
return err
}
c.close(cerr)
if !xerrors.Is(c.closeErr, cerr) {
return c.closeErr
}
return nil
}
func init() {
rand.Seed(time.Now().UnixNano())
}
// Ping sends a ping to the peer and waits for a pong.
// Use this to measure latency or ensure the peer is responsive.
//
// This API is experimental.
// Please provide feedback in https://github.com/nhooyr/websocket/issues/1.
func (c *Conn) Ping(ctx context.Context) error {
err := c.ping(ctx)
if err != nil {
return xerrors.Errorf("failed to ping: %w", err)
}
return nil
}
func (c *Conn) ping(ctx context.Context) error {
id := rand.Uint64()
p := strconv.FormatUint(id, 10)
pong := make(chan struct{})
c.activePingsMu.Lock()
c.activePings[p] = pong
c.activePingsMu.Unlock()
defer func() {
c.activePingsMu.Lock()
delete(c.activePings, p)
c.activePingsMu.Unlock()
}()
err := c.writeControl(ctx, opPing, []byte(p))
if err != nil {
return err
}
select {
case <-c.closed:
return c.closeErr
case <-ctx.Done():
c.close(xerrors.Errorf("failed to ping: %w", ctx.Err()))
return ctx.Err()
case <-pong:
return nil
}
}