Skip to content

Commit 134fd6d

Browse files
committed
Add Limit.String
1 parent 7569e0e commit 134fd6d

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

rate.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package redis_rate
22

33
import (
44
"context"
5+
"fmt"
56
"strconv"
67
"time"
78

@@ -23,6 +24,22 @@ type Limit struct {
2324
Burst int
2425
}
2526

27+
func (l *Limit) String() string {
28+
return fmt.Sprintf("%d req/%s (burst %d)", l.Rate, fmtDur(l.Period), l.Burst)
29+
}
30+
31+
func fmtDur(d time.Duration) string {
32+
switch d {
33+
case time.Second:
34+
return "s"
35+
case time.Minute:
36+
return "m"
37+
case time.Hour:
38+
return "h"
39+
}
40+
return d.String()
41+
}
42+
2643
func PerSecond(rate int) *Limit {
2744
return &Limit{
2845
Rate: rate,

rate_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func TestAllow(t *testing.T) {
2626

2727
l := rateLimiter()
2828
limit := redis_rate.PerSecond(10)
29+
assert.Equal(t, limit.String(), "10 req/s (burst 10)")
2930

3031
res, err := l.Allow(ctx, "test_id", limit)
3132
assert.Nil(t, err)

0 commit comments

Comments
 (0)