Skip to content

Commit

Permalink
fix: allow rate limit of 0
Browse files Browse the repository at this point in the history
We have a special case check for rate limits of 0 in some areas
of the code. Eventually I would like to remove those so I'm
going to set the rate limit of 0 as a valid rate limit which
allows no events.
  • Loading branch information
Chris Stockton committed Jan 27, 2025
1 parent 8ef3807 commit 193c777
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion internal/ratelimit/burst.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func NewBurstLimiter(r conf.Rate) *BurstLimiter {

e := r.Events
if e <= 0 {
e = 1
e = 0
}

// BurstLimiter will have an initial token bucket of size `e`. It will
Expand Down
33 changes: 29 additions & 4 deletions internal/ratelimit/burst_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ func TestBurstLimiter(t *testing.T) {
},
},

// zero value for Events = 1 event per second
// 1 event per second
{
cfg: conf.Rate{Events: 0, OverTime: time.Second},
cfg: conf.Rate{Events: 1, OverTime: time.Second},
now: now,
evts: []event{
{true, now, 0},
Expand All @@ -162,9 +162,9 @@ func TestBurstLimiter(t *testing.T) {
},
},

// zero value for both Events and OverTime = 1 event per hour.
// 1 events per second and OverTime = 1 event per hour.
{
cfg: conf.Rate{Events: 0, OverTime: 0},
cfg: conf.Rate{Events: 1, OverTime: 0},
now: now,
evts: []event{
{true, now, 0},
Expand All @@ -173,6 +173,31 @@ func TestBurstLimiter(t *testing.T) {
{true, now.Add(time.Hour * 2), 0},
},
},

// zero value for Events = 0 event per second
{
cfg: conf.Rate{Events: 0, OverTime: time.Second},
now: now,
evts: []event{
{false, now, 0},
{false, now.Add(-time.Second), 0},
{false, now.Add(time.Second), 0},
{false, now.Add(time.Second * 2), 0},
},
},

// zero value for both Events and OverTime = 1 event per hour.
{
cfg: conf.Rate{Events: 0, OverTime: 0},
now: now,
evts: []event{
{false, now, 0},
{false, now.Add(time.Hour - time.Second), 0},
{false, now.Add(-time.Hour), 0},
{false, now.Add(time.Hour), 0},
{false, now.Add(time.Hour * 2), 0},
},
},
}

for _, tc := range cases {
Expand Down

0 comments on commit 193c777

Please sign in to comment.