Skip to content

AutoLimiter: change a key's rate in place, and take with a context #179

Closed

Description

@blacksud0

The request

Two additions to AutoLimiter, both of which Limiter already has:

  1. 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.
  2. 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:

  • Remove stops the key's limiter, and a stopped limiter lets every waiting Take return as if it took a token (Stop lets every waiting Take return as if it took a token #178).
  • 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

  1. self-assigned this
    on Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions