You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
AutoLimiter: change a key's rate in place, and take with a context #179
Two additions to AutoLimiter, both of which Limiter already has:
A way to change one key's rate without removing the key, for example SetLimit(key string, max uint) and SetDuration(key string, d time.Duration), which call the same methods on the key's Limiter.
TakeContext(ctx context.Context, key string) error, which calls Limiter.TakeContext.
Why
AutoLimiter.Add returns ErrAutoKeyAlreadyExists for a key it holds, and get is unexported, so the only way to change a key's rate today is Remove then Add.
That has three costs:
The new limiter starts with a full bucket, so each change of rate grants a fresh burst on top of what the old bucket already granted in that interval.
A Take that arrives between the Remove and the Add builds the key from the default options, and the Add then fails with ErrAutoKeyAlreadyExists.
AutoLimiter.Take reads no context, so a caller whose request was cancelled while it waited cannot leave the queue, and the token it eventually receives is spent on nothing.
Seen on v0.0.94, and auto_ratelimit.go on main is the same today (2026-11-16).
Activity