@@ -20,14 +20,18 @@ type rediser interface {
2020
2121type Limit struct {
2222 Rate int
23- Period time.Duration
2423 Burst int
24+ Period time.Duration
2525}
2626
27- func (l * Limit ) String () string {
27+ func (l Limit ) String () string {
2828 return fmt .Sprintf ("%d req/%s (burst %d)" , l .Rate , fmtDur (l .Period ), l .Burst )
2929}
3030
31+ func (l Limit ) IsZero () bool {
32+ return l == Limit {}
33+ }
34+
3135func fmtDur (d time.Duration ) string {
3236 switch d {
3337 case time .Second :
@@ -40,24 +44,24 @@ func fmtDur(d time.Duration) string {
4044 return d .String ()
4145}
4246
43- func PerSecond (rate int ) * Limit {
44- return & Limit {
47+ func PerSecond (rate int ) Limit {
48+ return Limit {
4549 Rate : rate ,
4650 Period : time .Second ,
4751 Burst : rate ,
4852 }
4953}
5054
51- func PerMinute (rate int ) * Limit {
52- return & Limit {
55+ func PerMinute (rate int ) Limit {
56+ return Limit {
5357 Rate : rate ,
5458 Period : time .Minute ,
5559 Burst : rate ,
5660 }
5761}
5862
59- func PerHour (rate int ) * Limit {
60- return & Limit {
63+ func PerHour (rate int ) Limit {
64+ return Limit {
6165 Rate : rate ,
6266 Period : time .Hour ,
6367 Burst : rate ,
@@ -79,15 +83,15 @@ func NewLimiter(rdb rediser) *Limiter {
7983}
8084
8185// Allow is a shortcut for AllowN(ctx, key, limit, 1).
82- func (l * Limiter ) Allow (ctx context.Context , key string , limit * Limit ) (* Result , error ) {
86+ func (l Limiter ) Allow (ctx context.Context , key string , limit Limit ) (* Result , error ) {
8387 return l .AllowN (ctx , key , limit , 1 )
8488}
8589
8690// AllowN reports whether n events may happen at time now.
87- func (l * Limiter ) AllowN (
91+ func (l Limiter ) AllowN (
8892 ctx context.Context ,
8993 key string ,
90- limit * Limit ,
94+ limit Limit ,
9195 n int ,
9296) (* Result , error ) {
9397 values := []interface {}{limit .Burst , limit .Rate , limit .Period .Seconds (), n }
@@ -120,10 +124,10 @@ func (l *Limiter) AllowN(
120124
121125// AllowAtMost reports whether at most n events may happen at time now.
122126// It returns number of allowed events that is less than or equal to n.
123- func (l * Limiter ) AllowAtMost (
127+ func (l Limiter ) AllowAtMost (
124128 ctx context.Context ,
125129 key string ,
126- limit * Limit ,
130+ limit Limit ,
127131 n int ,
128132) (* Result , error ) {
129133 values := []interface {}{limit .Burst , limit .Rate , limit .Period .Seconds (), n }
@@ -163,7 +167,7 @@ func dur(f float64) time.Duration {
163167
164168type Result struct {
165169 // Limit is the limit that was used to obtain this result.
166- Limit * Limit
170+ Limit Limit
167171
168172 // Allowed is the number of events that may happen at time now.
169173 Allowed int
0 commit comments