Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle the GitHub rate limit in a better way #374

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions did/plugins/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import json
import re
import time

import requests

Expand Down Expand Up @@ -77,16 +78,21 @@ def search(self, query):
"Defined token is not valid. "
"Either update it or remove it.")

# Handle the exceeded rate limit
if response.status_code in [403, 429]:
if response.headers.get("X-RateLimit-Remaining") == "0":
reset_time = int(response.headers["X-RateLimit-Reset"])
sleep_time = int(max(reset_time - time.time(), 0)) + 1
log.warning("GitHub rate limit exceeded, use token to speed up.")
log.warning(f"Sleeping now for {listed(sleep_time, 'second')}.")
time.sleep(sleep_time)
continue
raise ReportError(f"GitHub query failed: {response.text}")

# Parse fetched json data
try:
data = json.loads(response.text)["items"]
result.extend(data)
except KeyError:
if json.loads(response.text)["message"].startswith(
"API rate limit exceeded"):
raise ReportError(
"GitHub API rate limit exceeded. "
"Consider creating an access token.")
except requests.exceptions.JSONDecodeError as error:
log.debug(error)
raise ReportError(f"GitHub JSON failed: {response.text}.")
Expand Down
Loading