@@ -41,15 +41,15 @@ type Websocket struct {
4141 addr string
4242 closed bool
4343
44+ sendLimiter * rate.Limiter
45+ dialLimiter * rate.Limiter
46+
4447 // Constants. These must not be changed after the Websocket instance is used
4548 // once, as they are not thread-safe.
4649
4750 // Timeout for connecting and writing to the Websocket, uses default
4851 // WSTimeout (global).
4952 Timeout time.Duration
50-
51- SendLimiter * rate.Limiter
52- DialLimiter * rate.Limiter
5353}
5454
5555// New creates a default Websocket with the given address.
@@ -64,10 +64,10 @@ func NewCustom(conn Connection, addr string) *Websocket {
6464 addr : addr ,
6565 closed : true ,
6666
67- Timeout : WSTimeout ,
67+ sendLimiter : NewSendLimiter (),
68+ dialLimiter : NewDialLimiter (),
6869
69- SendLimiter : NewSendLimiter (),
70- DialLimiter : NewDialLimiter (),
70+ Timeout : WSTimeout ,
7171 }
7272}
7373
@@ -80,7 +80,7 @@ func (ws *Websocket) Dial(ctx context.Context) error {
8080 ctx = tctx
8181 }
8282
83- if err := ws .DialLimiter .Wait (ctx ); err != nil {
83+ if err := ws .dialLimiter .Wait (ctx ); err != nil {
8484 // Expired, fatal error
8585 return errors .Wrap (err , "failed to wait" )
8686 }
@@ -99,6 +99,9 @@ func (ws *Websocket) Dial(ctx context.Context) error {
9999
100100 ws .closed = false
101101
102+ // Reset the send limiter.
103+ ws .sendLimiter = NewSendLimiter ()
104+
102105 return nil
103106}
104107
@@ -125,7 +128,7 @@ func (ws *Websocket) Send(b []byte) error {
125128func (ws * Websocket ) SendCtx (ctx context.Context , b []byte ) error {
126129 WSDebug ("Waiting for the send rate limiter..." )
127130
128- if err := ws .SendLimiter .Wait (ctx ); err != nil {
131+ if err := ws .sendLimiter .Wait (ctx ); err != nil {
129132 WSDebug ("Send rate limiter timed out." )
130133 return errors .Wrap (err , "SendLimiter failed" )
131134 }
0 commit comments