Skip to content

Commit

Permalink
Handle the GitHub rate limit in a better way
Browse files Browse the repository at this point in the history
Let's use the `X-RateLimit-Reset` header provided by GitHub and
sleep until the suggested time to complete the report instead of
bailing out in the middle.
  • Loading branch information
psss committed Oct 4, 2024
1 parent 017887f commit 78d49f8
Showing 1 changed file with 12 additions and 6 deletions.
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

0 comments on commit 78d49f8

Please sign in to comment.