@@ -5,12 +5,10 @@ import (
55 "time"
66)
77
8- // A Ticker synchronizes rate calculation among multiple Limiters and
9- // provides the on-tick callback.
8+ // A Ticker synchronizes rate calculation among multiple Limiters.
109type Ticker struct {
1110 mu sync.Mutex
1211 ch chan struct {}
13- fn func ()
1412}
1513
1614var DefaultTicker * Ticker
@@ -20,28 +18,17 @@ var DefaultTicker *Ticker
2018// both read and write limits, the second will set the write limit.
2119//
2220// To stop the limiter and free it's resources, call Stop.
23- func (ot * Ticker ) NewLimiter (limits ... int64 ) * Limiter {
21+ func (ot * Ticker ) NewLimiter (limits ... int64 ) ( l * Limiter ) {
2422 return & Limiter {
2523 Ticker : ot ,
26- Reads : NewOperation (limits , 0 ),
27- Writes : NewOperation (limits , 1 ),
24+ Reads : NewOperation (ot , limits , 0 ),
25+ Writes : NewOperation (ot , limits , 1 ),
2826 }
2927}
3028
31- func (ot * Ticker ) SetOnTick (fn func ()) {
32- ot .mu .Lock ()
33- ot .fn = fn
34- ot .mu .Unlock ()
35- }
36-
37- func (ot * Ticker ) GetOnTick () (fn func ()) {
38- ot .mu .Lock ()
39- fn = ot .fn
40- ot .mu .Unlock ()
41- return
42- }
43-
44- func (ot * Ticker ) Ch () (ch <- chan struct {}) {
29+ // WaitCh returns a channel that will close when the current rate limit
30+ // time slice runs out.
31+ func (ot * Ticker ) WaitCh () (ch <- chan struct {}) {
4532 ot .mu .Lock ()
4633 ch = ot .ch
4734 ot .mu .Unlock ()
@@ -63,19 +50,9 @@ func (ot *Ticker) run() {
6350 }
6451}
6552
66- func (ot * Ticker ) runOnTick () {
67- for {
68- <- ot .Ch ()
69- if fn := ot .GetOnTick (); fn != nil {
70- fn ()
71- }
72- }
73- }
74-
7553func init () {
7654 DefaultTicker = & Ticker {
7755 ch : make (chan struct {}),
7856 }
7957 go DefaultTicker .run ()
80- go DefaultTicker .runOnTick ()
8158}
0 commit comments