Repository navigation
feat: add AutoLimiter.SetRate and TakeContext - #180
Merged
Mzack9999 merged 1 commit intoNov 19, 2026
Merged
Conversation
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
Mzack9999
approved these changes
Nov 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #178
Closes #179
Problem
AutoLimitercan't change a key's rate. You have toRemovethe key andAddit again, andRemovestops the old bucket. When its token channel closes, everyTakewaiting on it returnsnilas 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.Takecalls it withcontext.Background().Limiter.TakeContextreturnsErrLimiterStoppedwhen 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 afterStop.createOrDefaultandrecreateLimiteruseLoadOrStore. Without it, a firstTakeracingSetRatecould replace the new bucket with one at the default rate.Issue #179 suggested separate
SetLimitandSetDuration. I went with oneSetRatebecause 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,AddAndTakeandLimiter.TakeContextcan now returnErrLimiterStoppedwhere they returnednil.Limiter.TakeandMultiLimiterdon'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
On
mainall 10 returnniltogether at the old bucket's next tick, about 280 ms in. Withl.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,SetRateon a missing key,SetRateracing a firstTake, concurrentSetRatecalls,TakeContextcancellation, waiters afterRemove, andTakeContextafterStopon both bucket types. The waiter, race and stop tests fail onmain.go test -race ./...andgolangci-lint runpass on macOS and Linux.