Skip to content

Conversation

@riturajFi
Copy link
Contributor

This PR implements proactive rate-limit handling for the GitHub API in DefaultGithubClient.

Previously, the application only reacted after hitting a 403 RateLimitExceeded error. With these changes, the client now:

✅ Reads X-RateLimit-Remaining and X-RateLimit-Reset headers from each GitHub API response.
✅ Tracks the remaining quota and reset time internally.
✅ Checks the quota before each request and pauses (with sleep) when the quota is near exhausted, avoiding unnecessary failures.

This improves stability and avoids API bans, especially under heavy load or with many tracked repositories.

Changes
Add RateLimitState struct inside DefaultGithubClient (with Arc<Mutex<...>>).
Update state after every response using the provided rate-limit headers.
Add rate_limit_guard() method that blocks requests if remaining quota is below a safety threshold.
Call rate_limit_guard() at the start of every execute_graphql() call.
Linked Issue
Closes #67

@riturajFi riturajFi changed the title Feature/rate limiting GitHub Feature/Proactive Rate Limitting in Github Client Jul 22, 2025
Copy link
Owner

@isSerge isSerge left a comment

Choose a reason for hiding this comment

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

This is a significant improvement, few minor comments below.

Also, check cargo clippy - there are some warnings, and run cargo fmt. I will update CI to run these for all branches, not just main

Thanks!

@riturajFi riturajFi requested a review from isSerge July 22, 2025 13:26
Comment on lines 198 to 200
GithubError::HeaderError(_) => {
tracing::error!("Header error while polling issues for repository");
}
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
GithubError::HeaderError(_) => {
tracing::error!("Header error while polling issues for repository");
}
GithubError::HeaderError(msg) => {
tracing::warn!(
"Could not parse rate limit headers for repo {} (chat {}): {}. \
Skipping this repo for this cycle.",
repo.name_with_owner,
chat_id,
msg
);
}

@riturajFi riturajFi requested a review from isSerge July 24, 2025 06:30
@riturajFi riturajFi requested a review from isSerge July 24, 2025 07:02
@riturajFi
Copy link
Contributor Author

@isSerge requesting review. resolved the cargo fmt issue

Copy link
Owner

@isSerge isSerge left a comment

Choose a reason for hiding this comment

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

Thanks for your hard work @riturajFi

@isSerge isSerge merged commit e6adc7e into isSerge:main Jul 24, 2025
4 checks passed
@riturajFi
Copy link
Contributor Author

Thanks for your guidance! More to come!!

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.

Proactive GitHub API Rate Limit Handling

2 participants