Skip to content

RateLimit admission control dependency#356

Merged
chrisguidry merged 2 commits intomainfrom
rate-limit-161
Feb 27, 2026
Merged

RateLimit admission control dependency#356
chrisguidry merged 2 commits intomainfrom
rate-limit-161

Conversation

@chrisguidry
Copy link
Owner

Adds RateLimit(limit, per=timedelta) — caps how many times a task (or a
per-parameter scope) can execute within a sliding window. Uses a single
Redis sorted set per scope with a Lua script that atomically prunes old
entries, counts remaining, and either records the execution or computes
retry_after from the oldest entry.

By default, excess tasks are rescheduled to exactly when a slot opens.
drop=True quietly drops them instead (like Cooldown).

Works both as a default parameter and as Annotated metadata for
per-parameter scoping — same patterns as Cooldown and Debounce.

Closes #161.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 4.6 noreply@anthropic.com

Adds `RateLimit(limit, per=timedelta)` — caps how many times a task (or a
per-parameter scope) can execute within a sliding window. Uses a single
Redis sorted set per scope with a Lua script that atomically prunes old
entries, counts remaining, and either records the execution or computes
`retry_after` from the oldest entry.

By default, excess tasks are rescheduled to exactly when a slot opens.
`drop=True` quietly drops them instead (like Cooldown).

Works both as a default parameter and as `Annotated` metadata for
per-parameter scoping — same patterns as Cooldown and Debounce.

Closes #161.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@read-the-docs-community
Copy link

read-the-docs-community bot commented Feb 27, 2026

Documentation build overview

📚 docket | 🛠️ Build #31595321 | 📁 Comparing e331c29 against latest (58a9bf5)


🔍 Preview build

Show files changed (2 files in total): 📝 2 modified | ➕ 0 added | ➖ 0 deleted
File Status
api-reference/index.html 📝 modified
task-behaviors/index.html 📝 modified

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4e7e7b824e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

script = redis.register_script(_RATELIMIT_LUA)
result: list[int] = await script(
keys=[ratelimit_key],
args=[execution.key, now_ms, window_ms, self.limit, ttl_ms],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Count each execution, not each task key

Using execution.key as the sorted-set member means repeated runs of the same key overwrite a single entry instead of increasing the window count, so the limiter can be bypassed whenever keys are reused (for example, Perpetual.on_complete reschedules with the same key in src/docket/dependencies/_perpetual.py). In those cases ZCARD stays at 1 and limits like RateLimit(10, ...) never trigger as intended.

Useful? React with 👍 / 👎.

@codecov-commenter
Copy link

codecov-commenter commented Feb 27, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (58a9bf5) to head (e331c29).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff            @@
##              main      #356   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          106       108    +2     
  Lines         3106      3159   +53     
  Branches        26        26           
=========================================
+ Hits          3106      3159   +53     
Flag Coverage Δ
cli-python-3.10 100.00% <ø> (ø)
cli-python-3.11 100.00% <ø> (ø)
cli-python-3.12 100.00% <ø> (ø)
cli-python-3.13 100.00% <ø> (ø)
cli-python-3.14 100.00% <ø> (ø)
python-3.10 100.00% <100.00%> (ø)
python-3.11 98.03% <100.00%> (+0.03%) ⬆️
python-3.12 100.00% <100.00%> (ø)
python-3.13 100.00% <100.00%> (ø)
python-3.14 100.00% <100.00%> (ø)
windows-python-3.10 100.00% <100.00%> (ø)
windows-python-3.11 97.95% <100.00%> (+0.03%) ⬆️
windows-python-3.12 100.00% <100.00%> (ø)
windows-python-3.13 100.00% <100.00%> (ø)
windows-python-3.14 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/docket/dependencies/__init__.py 100.00% <ø> (ø)
src/docket/dependencies/_ratelimit.py 100.00% <100.00%> (ø)
tests/test_ratelimit.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Using `execution.key` as the sorted set member meant Perpetual tasks
(which reuse the same key via `replace()`) only ever had one entry —
ZADD overwrites the score instead of adding a new member, so ZCARD
stays at 1 and the rate limit never fires.

The member is now `{execution.key}:{now_ms}`, so each execution
attempt gets its own entry. Also cleans up phantom slots in
`__aexit__` when a later dependency (like ConcurrencyLimit) blocks —
the task never ran but would otherwise consume a rate-limit slot.

Caught during PR #356 review.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@chrisguidry chrisguidry merged commit d2fa250 into main Feb 27, 2026
50 checks passed
@chrisguidry chrisguidry deleted the rate-limit-161 branch February 27, 2026 23:02
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.

Rate-limiting tasks

2 participants