Skip to content

Commit

Permalink
fix: change the burst limit to the number of configured events
Browse files Browse the repository at this point in the history
For example, the configuration value "100/24h" will permit 100
events immediately. It will still continue to refill the bucket
at a rate of 1 token approx every 15 minutes.

This change comes with the caveat that this rate limit is easily
bypassed by restarting the service. You may however achieve
smaller bursts (mitigating circumvention via restarts) by
lowering the overall time interval at the same ratio. For example
you could specify 8/2h to limit the burst to only 8 while still
producing roughly 1 token every 15 minutes.
  • Loading branch information
Chris Stockton committed Sep 25, 2024
1 parent 92f869c commit 691c78d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
5 changes: 4 additions & 1 deletion internal/api/ratelimits.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ import (
func newRateLimiter(r conf.Rate) *rate.Limiter {
// The rate limiter deals in events per second.
eps := r.EventsPerSecond()
const burst = 1
burst := int(r.Events)
if burst <= 0 {
burst = 1
}

// NewLimiter will have an initial token bucket of size `burst`. It will
// be refilled at a rate of `eps` indefinitely. Note that the expression
Expand Down
31 changes: 20 additions & 11 deletions internal/api/ratelimits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,25 @@ func Example_newRateLimiter() {
cfg := conf.Rate{Events: 100, OverTime: time.Hour * 24}
rl := newRateLimiter(cfg)
cur := now
for i := 0; i < 61; i++ {
fmt.Printf("%-5v @ %v\n", rl.AllowN(cur, 1), cur)
cur = cur.Add(time.Minute)
for limited, i := 0, 0; i < 160; i++ {
allowed := rl.AllowN(cur, 1)
if allowed {
limited++
}

switch {
case i == 100:
fmt.Printf("true @ %v for last %v events...\n", cur, limited)
case i > 100:
fmt.Printf("%-5v @ %v\n", allowed, cur)
cur = cur.Add(time.Minute)
}
}
}

// Output:
// true @ 2024-09-24 10:00:00 +0000 UTC
// true @ 2024-09-24 10:00:00 +0000 UTC for last 100 events...
// false @ 2024-09-24 10:00:00 +0000 UTC
// false @ 2024-09-24 10:01:00 +0000 UTC
// false @ 2024-09-24 10:02:00 +0000 UTC
// false @ 2024-09-24 10:03:00 +0000 UTC
Expand Down Expand Up @@ -55,8 +66,8 @@ func Example_newRateLimiter() {
// false @ 2024-09-24 10:26:00 +0000 UTC
// false @ 2024-09-24 10:27:00 +0000 UTC
// false @ 2024-09-24 10:28:00 +0000 UTC
// false @ 2024-09-24 10:29:00 +0000 UTC
// true @ 2024-09-24 10:30:00 +0000 UTC
// true @ 2024-09-24 10:29:00 +0000 UTC
// false @ 2024-09-24 10:30:00 +0000 UTC
// false @ 2024-09-24 10:31:00 +0000 UTC
// false @ 2024-09-24 10:32:00 +0000 UTC
// false @ 2024-09-24 10:33:00 +0000 UTC
Expand All @@ -70,8 +81,8 @@ func Example_newRateLimiter() {
// false @ 2024-09-24 10:41:00 +0000 UTC
// false @ 2024-09-24 10:42:00 +0000 UTC
// false @ 2024-09-24 10:43:00 +0000 UTC
// false @ 2024-09-24 10:44:00 +0000 UTC
// true @ 2024-09-24 10:45:00 +0000 UTC
// true @ 2024-09-24 10:44:00 +0000 UTC
// false @ 2024-09-24 10:45:00 +0000 UTC
// false @ 2024-09-24 10:46:00 +0000 UTC
// false @ 2024-09-24 10:47:00 +0000 UTC
// false @ 2024-09-24 10:48:00 +0000 UTC
Expand All @@ -84,9 +95,7 @@ func Example_newRateLimiter() {
// false @ 2024-09-24 10:55:00 +0000 UTC
// false @ 2024-09-24 10:56:00 +0000 UTC
// false @ 2024-09-24 10:57:00 +0000 UTC
// false @ 2024-09-24 10:58:00 +0000 UTC
// false @ 2024-09-24 10:59:00 +0000 UTC
// true @ 2024-09-24 11:00:00 +0000 UTC
// true @ 2024-09-24 10:58:00 +0000 UTC

}

Expand Down

0 comments on commit 691c78d

Please sign in to comment.