Skip to content

Commit a25250d

Browse files
committed
wsutil: Reset SendLimiter after Dial
1 parent 33eb8ef commit a25250d

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

utils/wsutil/conn.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,10 @@ func startReadLoop(conn *websocket.Conn, eventCh chan<- Event) {
182182
return
183183
}
184184

185-
// // Check if the error is a normal one:
186-
// if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
187-
// return
188-
// }
185+
// Check if the error is a normal one:
186+
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
187+
return
188+
}
189189

190190
// Unusual error; log and exit:
191191
eventCh <- Event{nil, errors.Wrap(err, "WS error")}

utils/wsutil/ws.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {
125128
func (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

Comments
 (0)