Skip to content

Instantly share code, notes, and snippets.

@mendhak
Last active April 18, 2021 12:14
Show Gist options
  • Save mendhak/eed2e27429feffe94990705a337ab5ba to your computer and use it in GitHub Desktop.
Save mendhak/eed2e27429feffe94990705a337ab5ba to your computer and use it in GitHub Desktop.
pygithub rate limit Python decorator
@rate_limited_retry()
def get_search_issues(gh, author, type):
return gh.search_issues('', author=author, type=type)
def rate_limited_retry():
def decorator(func):
def ret(*args, **kwargs):
for _ in range(3):
try:
return func(*args, **kwargs)
except RateLimitExceededException:
limits = gh.get_rate_limit()
print(f"Rate limit exceeded")
print("Search:", limits.search, "Core:", limits.core, "GraphQl:", limits.graphql)
if limits.search.remaining == 0:
limited = limits.search
elif limits.graphql.remaining == 0:
limited = limits.graphql
else:
limited = limits.core
reset = limited.reset.replace(tzinfo=timezone.utc)
now = datetime.now(timezone.utc)
seconds = (reset - now).total_seconds() + 30
print(f"Reset is in {seconds} seconds.")
if seconds > 0.0:
print(f"Waiting for {seconds} seconds...")
time.sleep(seconds)
print("Done waiting - resume!")
raise Exception("Failed too many times")
return ret
return decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment