File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -38,7 +38,9 @@ func (ot *Ticker) Stop() {
3838 <- ot .doneCh
3939}
4040
41- // NewLimiter returns a new Limiter using this Ticker.
41+ // NewLimiter returns a new Limiter using this Ticker. If this Ticker is stopped,
42+ // returns nil.
43+ //
4244// If you provide limits, the first will set
4345// both read and write limits, the second will set the write limit.
4446// Limits are applied in 100ms slices with fractional carry-over between
@@ -47,11 +49,16 @@ func (ot *Ticker) Stop() {
4749//
4850// To stop the limiter and free it's resources, call Stop.
4951func (ot * Ticker ) NewLimiter (limits ... int64 ) (l * Limiter ) {
50- return & Limiter {
51- Ticker : ot ,
52- Reads : NewOperation (ot , limits , 0 ),
53- Writes : NewOperation (ot , limits , 1 ),
52+ ot .mu .Lock ()
53+ if ot .stopCh != nil {
54+ l = & Limiter {
55+ Ticker : ot ,
56+ Reads : NewOperation (ot , limits , 0 ),
57+ Writes : NewOperation (ot , limits , 1 ),
58+ }
5459 }
60+ ot .mu .Unlock ()
61+ return
5562}
5663
5764// WaitCh returns a channel that will close when the current rate limit
You can’t perform that action at this time.
0 commit comments