Skip to content

feat: add rate limiting retry middleware#533

Open
adityakrmishra wants to merge 1 commit intopalantir:developfrom
adityakrmishra:feature/rate-limit-retry
Open

feat: add rate limiting retry middleware#533
adityakrmishra wants to merge 1 commit intopalantir:developfrom
adityakrmishra:feature/rate-limit-retry

Conversation

@adityakrmishra
Copy link
Copy Markdown

Addresses #45

Hi team,

This PR introduces an opt-in middleware to automatically wait and retry requests when hitting GitHub's rate limits or secondary abuse limits (HTTP 403), as requested in #45.

To address the concerns about indefinite blocking and race conditions, this feature is completely opt-in and requires a strict maxWait threshold.

Implementation Details:

  • Opt-in only: Added WithRateLimitRetry(maxWait) as a ClientOption.
  • Configuration: Added RateLimitMaxWait to Config (configurable via YAML/JSON or GITHUB_RATE_LIMIT_MAX_WAIT env var).
  • Header Parsing: Respects both Retry-After (seconds) and X-RateLimit-Reset (epoch timestamp), prioritizing Retry-After as per GitHub API docs.
  • Safety First: - If the required wait exceeds maxWait, it returns the 403 response immediately.
    • Retries exactly once after sleeping.
    • Sleep is context-aware (select { case <-ctx.Done() ... }) to prevent goroutine leaks if the caller cancels the request.
    • Safely drains and closes the original 403 response body before retrying to prevent connection exhaustion.

Testing:
Added comprehensive table-driven tests in middleware_retry_test.go covering both header types, edge cases (wait > maxWait, no headers), and zero-config scenarios.

Let me know if you would like any changes to the default behavior or parsing logic!

@changelog-app
Copy link
Copy Markdown

changelog-app bot commented Apr 16, 2026

Generate changelog in changelog/@unreleased

Type (Select exactly one)

  • Feature (Adding new functionality)
  • Improvement (Improving existing functionality)
  • Fix (Fixing an issue with existing functionality)
  • Break (Creating a new major version by breaking public APIs)
  • Deprecation (Removing functionality in a non-breaking way)
  • Migration (Automatically moving data/functionality to a new system)

Description

feat: add rate limiting retry middleware

Check the box to generate changelog(s)

  • Generate changelog entry

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