Skip to content

only send X-RateLimit headers when a rate limit applies - #262

Merged
thushan merged 3 commits into
mainfrom
fix/rate-limit-headers-when-off
Oct 30, 2026
Merged

thushan merged 3 commits into
mainfrom
fix/rate-limit-headers-when-off

Conversation

@thushan

@thushan thushan commented Oct 29, 2026 •

Copy link
Copy Markdown
Owner

Summary

RateLimitValidator.CreateMiddleware in internal/adapter/security/request_rate_limit.go set X-RateLimit-Limit, X-RateLimit-Remaining and X-RateLimit-Reset on every response, unconditionally. result.RateLimit is 0 when no tier applies (both per_ip_requests_per_minute and global_requests_per_minute at 0, or an unlimited health probe), so a 200 or a 413 carried X-RateLimit-Limit: 0 and X-RateLimit-Remaining: 0, which a client reads as fully throttled.

The three headers are now set only when result.RateLimit > 0. Retry-After on a rejection is unchanged.

Type of change

  • Bug fix

Upgrading

With rate limiting off, responses no longer carry the X-RateLimit-* headers. Anything that parsed them to detect disabled limits will see them absent. With a limit on, nothing changes. This is in the v0.0.30 upgrading checklist and has its own release note entry.

How was this tested?

New in internal/adapter/security/request_rate_limit_test.go: TestRateLimitValidator_Middleware_RateLimitHeaders, a table over per-IP on, per-IP off with global on (reports the global limit), both off, and an unlimited health probe.

Extended in internal/app/handlers/handler_security_status_codes_test.go: TestSecurityChainMiddleware_OversizedBody_Returns413 asserts the three headers are absent on a 413 with both tiers off.

Integration

Tested together with the rest of the October round-up (R1 to R5) on feature/october-round-up at bb64508, a local branch merging all five onto main fd5e341:

  • make ready and go test -race over proxy, security, app and config
  • /olla-validate --quick: 46 pass, 0 fail
  • /claude-code-e2e against a real Ollama, passthrough and translation legs: Tier A 16 pass / 0 fail each, no 429s, no retries
  • Live checks of each change: retry policy on both engines, chunked Anthropic body cap (peak RSS 81MB for 4 concurrent 90MB chunked requests), rate limit headers, unknown-key cap
  • Log scan: no panics, data races or concurrent map errors

Checklist

  • make ready passes at a4365905
  • Tests added or updated for the change
  • Documentation updated: docs/content/configuration/practices/rate-limits.md, v0.0.30 release note and upgrading checklist
  • No new dependencies added

Summary by CodeRabbit

  • Bug Fixes
    • Rate-limit headers are now omitted when rate limiting is disabled or no limit applies, including on unlimited health checks.
  • Documentation
    • Updated the rate-limit guidance and release notes to clarify when these response headers are sent.

@coderabbitai

coderabbitai Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Walkthrough

The middleware now sends X-RateLimit-* headers only when a positive rate limit applies. Tests and documentation cover disabled limits, enabled tiers, unlimited health probes, and oversized requests.

Changes

Rate-limit response headers

Layer / File(s) Summary
Conditional header emission and coverage
internal/adapter/security/request_rate_limit.go, internal/adapter/security/request_rate_limit_test.go, internal/app/handlers/handler_security_status_codes_test.go, docs/content/configuration/practices/rate-limits.md, docs/content/about/release-notes.md
The middleware omits the three X-RateLimit-* headers when result.RateLimit is not positive. Tests check disabled and enabled tiers, unlimited health probes, and a 413 response. The documentation describes when these headers are sent.

Priority: Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Possibly related PRs

  • thushan/olla#257: Adds translator endpoints that use the middleware changed here.
  • thushan/olla#256: Changes the rate-limit tiers and validation result value used by the middleware.
Merge Risk: Minimal · up to e87ad

The release-note link works, and rate-limited responses retain their headers. No identified issue blocks merging after normal checks.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: send X-RateLimit headers only when a rate limit applies.
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 2 functions across 3 files. (2 skipped: 2 unsupported.)

Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

# Conflicts:
#	docs/content/about/release-notes.md
@thushan
thushan merged commit 0a3adeb into main Oct 30, 2026
8 checks passed
@thushan
thushan deleted the fix/rate-limit-headers-when-off branch October 30, 2026 01:15
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.

1 participant