Skip to content

feat: add AutoLimiter.SetRate and TakeContext - #180

Merged
Mzack9999 merged 1 commit into
projectdiscovery:mainfrom
blacksud0:fix/autolimiter-rate-in-place
Nov 19, 2026
Merged

Mzack9999 merged 1 commit into
projectdiscovery:mainfrom
blacksud0:fix/autolimiter-rate-in-place

Conversation

@blacksud0

@blacksud0 blacksud0 commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Closes #178
Closes #179

Problem

AutoLimiter can't change a key's rate. You have to Remove the key and Add it again, and Remove stops the old bucket. When its token channel closes, every Take waiting on it returns nil as if it got a token. So a crawler that slows a host down after a 429 and speeds it back up later sends its whole queue at once on every re-rate.

Change

  • AutoLimiter.SetRate(key, maxCount, duration) changes the rate on the key's existing bucket. Waiting takes stay queued and get tokens at the new rate. A missing key is created.
  • AutoLimiter.TakeContext(ctx, key), as asked in AutoLimiter: change a key's rate in place, and take with a context #179. Take calls it with context.Background().
  • Limiter.TakeContext returns ErrLimiterStopped when the token channel is closed, as proposed in Stop lets every waiting Take return as if it took a token #178. The leaky bucket does the same after Stop.
  • createOrDefault and recreateLimiter use LoadOrStore. Without it, a first Take racing SetRate could replace the new bucket with one at the default rate.

Issue #179 suggested separate SetLimit and SetDuration. I went with one SetRate because it sets both under one lock, and creating a missing key needs both values anyway. I can split it if you'd rather have the pair.

Behaviour change

AutoLimiter.Take, AddAndTake and Limiter.TakeContext can now return ErrLimiterStopped where they returned nil. Limiter.Take and MultiLimiter don't change. A stopped bucket still releases its waiters on its next tick, but they now get an error instead of a token they never had.

Repro

l := ratelimit.NewAutoLimiter(ctx, ratelimit.WithMaxCount(1), ratelimit.WithDuration(time.Hour))
_ = l.Add("host", ratelimit.WithMaxCount(1), ratelimit.WithDuration(300*time.Millisecond))
_ = l.Take("host") // spend the token
// start 10 goroutines calling l.Take("host"), then:
l.Remove("host")
_ = l.Add("host", ratelimit.WithMaxCount(1), ratelimit.WithDuration(100*time.Millisecond))

On main all 10 return nil together at the old bucket's next tick, about 280 ms in. With l.SetRate("host", 1, 100*time.Millisecond) in place of the last two lines, they leave one per 100 ms.

Tests

New tests cover waiters kept through SetRate, SetRate on a missing key, SetRate racing a first Take, concurrent SetRate calls, TakeContext cancellation, waiters after Remove, and TakeContext after Stop on both bucket types. The waiter, race and stop tests fail on main.

go test -race ./... and golangci-lint run pass on macOS and Linux.

AutoLimiter.SetRate changes a key's limit and interval on the bucket
the key already has, creating the key when absent, so takes already
waiting stay on that bucket and receive tokens at the new rate.
AutoLimiter.TakeContext takes with a context, and Take calls it.

Limiter.TakeContext tells a closed token channel from a token and
returns ErrLimiterStopped, as a stopped leaky bucket now does too,
instead of nil. AutoLimiter.Take and AddAndTake pass the error on.

createOrDefault and recreateLimiter store a new bucket with
LoadOrStore, so a first Take racing another first Take or a SetRate
leaves the key one bucket.

Closes projectdiscovery#178
Closes projectdiscovery#179
@blacksud0 blacksud0 changed the title feat: change the rate of an AutoLimiter key in place, report a stopped limiter feat: add AutoLimiter.SetRate and TakeContext Nov 18, 2026
@Mzack9999
Mzack9999 merged commit e76ecbc into projectdiscovery:main Nov 19, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AutoLimiter: change a key's rate in place, and take with a context Stop lets every waiting Take return as if it took a token

2 participants