Skip to content

Commit d82f64f

Browse files
committed
require live Ticker
1 parent 7cd0a8b commit d82f64f

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

ticker.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff 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.
4951
func (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

0 commit comments

Comments
 (0)