Skip to content

Commit b9c7973

Browse files
committed
swap bool value, was confusing
1 parent 26eb71a commit b9c7973

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ See the post for information about the requirements and design of the actual alg
1313
The rate limiter satisfies this interface:
1414

1515
```go
16-
Increment(ctx context.Context, key string, incr int) (*Status, bool, error)
16+
Increment(ctx context.Context, key string, incr int) (status *Status, allowed bool, err error)
1717
```
1818

1919
- Status includes information you'd want to set in [`RateLimit` headers](https://datatracker.ietf.org/doc/draft-ietf-httpapi-ratelimit-headers/).
20-
- Bool is whether the limit was exceeded or not, true means reject the request.
20+
- Bool is whether action should be allowed or not, false means reject the action.
2121
- Errors occur for cache issues, such as Redis connectivity or malformed data.
2222

2323
The implementation is store agnostic, however due to the way it works, Redis is the recommended approach due to the usage of [hash sets](https://redis.io/docs/latest/develop/data-types/hashes/).
2424

2525
The `incr` argument allows you to assign different weights to the action being rate limited. For example, a simple request may use a value of 1 and an expensive request may use a value of 10.
2626

2727
```go
28-
status, exceeded, err := m.rl.Increment(ctx, key, cost)
28+
status, allowed, err := m.rl.Increment(ctx, key, cost)
2929
if err != nil {
3030
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
3131
return
@@ -39,7 +39,7 @@ w.Header().Set(RateLimitLimit, strconv.FormatUint(uint64(limit), 10))
3939
w.Header().Set(RateLimitRemaining, strconv.FormatUint(uint64(remaining), 10))
4040
w.Header().Set(RateLimitReset, resetTime)
4141

42-
if exceeded {
42+
if !allowed {
4343
// you shall not pass.
4444
w.Header().Set(RetryAfter, resetTime)
4545
http.Error(w, http.StatusText(http.StatusTooManyRequests), http.StatusTooManyRequests)

swirl.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func New(store Store, limit int, period, expiry time.Duration) *Limiter {
3636
}
3737
}
3838

39-
func (l *Limiter) Increment(ctx context.Context, key string, incr int) (*Status, bool, error) {
39+
func (l *Limiter) Increment(ctx context.Context, key string, incr int) (status *Status, allowed bool, err error) {
4040
now := time.Now()
4141
timestamp := fmt.Sprint(now.Truncate(l.counterWindow).Unix())
4242

@@ -53,7 +53,7 @@ func (l *Limiter) Increment(ctx context.Context, key string, incr int) (*Status,
5353
Limit: l.limit,
5454
Period: l.limitPeriod,
5555
Reset: now.Add(l.limitPeriod),
56-
}, true, nil
56+
}, false, nil
5757
}
5858

5959
// create or move whole limit period window expiry
@@ -76,7 +76,10 @@ func (l *Limiter) Increment(ctx context.Context, key string, incr int) (*Status,
7676
total := 0
7777
for k, v := range vals {
7878
if k > threshold {
79-
i, _ := strconv.Atoi(v)
79+
i, err := strconv.Atoi(v)
80+
if err != nil {
81+
return nil, false, err
82+
}
8083
total += i
8184
} else {
8285
// Clear the old hash keys
@@ -86,21 +89,21 @@ func (l *Limiter) Increment(ctx context.Context, key string, incr int) (*Status,
8689
}
8790
}
8891

89-
// exceeded
92+
// exceeded rate limit
9093
if total >= int(l.limit) {
9194
return &Status{
9295
Remaining: 0,
9396
Limit: l.limit,
9497
Period: l.limitPeriod,
9598
Reset: now.Add(l.limitPeriod),
96-
}, true, nil
99+
}, false, nil
97100
}
98101

99-
// not exceeded
102+
// allowed
100103
return &Status{
101104
Remaining: int(l.limit) - total,
102105
Limit: l.limit,
103106
Period: l.limitPeriod,
104107
Reset: now.Add(l.limitPeriod),
105-
}, false, nil
108+
}, true, nil
106109
}

swirl_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,20 @@ func TestRateLimit(t *testing.T) {
4040
}
4141
}
4242

43-
check(t, Status{Remaining: 9, Limit: 10}, false, now.Add(period))(ratelimiter.Increment(ctx, "k", 1))
44-
check(t, Status{Remaining: 8, Limit: 10}, false, now.Add(period))(ratelimiter.Increment(ctx, "k", 1))
45-
check(t, Status{Remaining: 7, Limit: 10}, false, now.Add(period))(ratelimiter.Increment(ctx, "k", 1))
46-
check(t, Status{Remaining: 6, Limit: 10}, false, now.Add(period))(ratelimiter.Increment(ctx, "k", 1))
47-
check(t, Status{Remaining: 5, Limit: 10}, false, now.Add(period))(ratelimiter.Increment(ctx, "k", 1))
48-
check(t, Status{Remaining: 4, Limit: 10}, false, now.Add(period))(ratelimiter.Increment(ctx, "k", 1))
49-
check(t, Status{Remaining: 3, Limit: 10}, false, now.Add(period))(ratelimiter.Increment(ctx, "k", 1))
50-
check(t, Status{Remaining: 2, Limit: 10}, false, now.Add(period))(ratelimiter.Increment(ctx, "k", 1))
51-
check(t, Status{Remaining: 1, Limit: 10}, false, now.Add(period))(ratelimiter.Increment(ctx, "k", 1))
52-
check(t, Status{Remaining: 0, Limit: 10}, true, now.Add(period))(ratelimiter.Increment(ctx, "k", 1))
43+
check(t, Status{Remaining: 9, Limit: 10}, true, now.Add(period))(ratelimiter.Increment(ctx, "k", 1))
44+
check(t, Status{Remaining: 8, Limit: 10}, true, now.Add(period))(ratelimiter.Increment(ctx, "k", 1))
45+
check(t, Status{Remaining: 7, Limit: 10}, true, now.Add(period))(ratelimiter.Increment(ctx, "k", 1))
46+
check(t, Status{Remaining: 6, Limit: 10}, true, now.Add(period))(ratelimiter.Increment(ctx, "k", 1))
47+
check(t, Status{Remaining: 5, Limit: 10}, true, now.Add(period))(ratelimiter.Increment(ctx, "k", 1))
48+
check(t, Status{Remaining: 4, Limit: 10}, true, now.Add(period))(ratelimiter.Increment(ctx, "k", 1))
49+
check(t, Status{Remaining: 3, Limit: 10}, true, now.Add(period))(ratelimiter.Increment(ctx, "k", 1))
50+
check(t, Status{Remaining: 2, Limit: 10}, true, now.Add(period))(ratelimiter.Increment(ctx, "k", 1))
51+
check(t, Status{Remaining: 1, Limit: 10}, true, now.Add(period))(ratelimiter.Increment(ctx, "k", 1))
52+
check(t, Status{Remaining: 0, Limit: 10}, false, now.Add(period))(ratelimiter.Increment(ctx, "k", 1))
5353

5454
// Wait for after the length of period (~3s) to reset the rate limit
5555
delay := period + time.Millisecond*10
5656
time.Sleep(delay)
5757

58-
check(t, Status{Remaining: 10, Limit: 10}, false, time.Now().Add(period))(ratelimiter.Increment(ctx, "k", 1))
58+
check(t, Status{Remaining: 10, Limit: 10}, true, time.Now().Add(period))(ratelimiter.Increment(ctx, "k", 1))
5959
}

0 commit comments

Comments
 (0)