@@ -5,13 +5,12 @@ package websocket
5
5
import (
6
6
"bufio"
7
7
"context"
8
- cryptorand "crypto/rand"
8
+ "crypto/rand"
9
9
"errors"
10
10
"fmt"
11
11
"io"
12
12
"io/ioutil"
13
13
"log"
14
- "math/rand"
15
14
"runtime"
16
15
"strconv"
17
16
"sync"
@@ -82,6 +81,7 @@ type Conn struct {
82
81
setReadTimeout chan context.Context
83
82
setWriteTimeout chan context.Context
84
83
84
+ pingCounter * atomicInt64
85
85
activePingsMu sync.Mutex
86
86
activePings map [string ]chan <- struct {}
87
87
}
@@ -100,6 +100,7 @@ func (c *Conn) init() {
100
100
c .setReadTimeout = make (chan context.Context )
101
101
c .setWriteTimeout = make (chan context.Context )
102
102
103
+ c .pingCounter = & atomicInt64 {}
103
104
c .activePings = make (map [string ]chan <- struct {})
104
105
105
106
c .writeHeaderBuf = makeWriteHeaderBuf ()
@@ -669,7 +670,7 @@ func (c *Conn) writeFrame(ctx context.Context, fin bool, opcode opcode, p []byte
669
670
c .writeHeader .payloadLength = int64 (len (p ))
670
671
671
672
if c .client {
672
- _ , err := io .ReadFull (cryptorand .Reader , c .writeHeader .maskKey [:])
673
+ _ , err := io .ReadFull (rand .Reader , c .writeHeader .maskKey [:])
673
674
if err != nil {
674
675
return 0 , fmt .Errorf ("failed to generate masking key: %w" , err )
675
676
}
@@ -839,10 +840,6 @@ func (c *Conn) writeClose(p []byte, cerr error) error {
839
840
return nil
840
841
}
841
842
842
- func init () {
843
- rand .Seed (time .Now ().UnixNano ())
844
- }
845
-
846
843
// Ping sends a ping to the peer and waits for a pong.
847
844
// Use this to measure latency or ensure the peer is responsive.
848
845
// Ping must be called concurrently with Reader as it does
@@ -851,10 +848,9 @@ func init() {
851
848
//
852
849
// TCP Keepalives should suffice for most use cases.
853
850
func (c * Conn ) Ping (ctx context.Context ) error {
854
- id := rand .Uint64 ()
855
- p := strconv .FormatUint (id , 10 )
851
+ p := c .pingCounter .Increment (1 )
856
852
857
- err := c .ping (ctx , p )
853
+ err := c .ping (ctx , strconv . FormatInt ( p , 10 ) )
858
854
if err != nil {
859
855
return fmt .Errorf ("failed to ping: %w" , err )
860
856
}
0 commit comments