Skip to content

Commit

Permalink
Fix rate limiting from pygithub.RateLimitExceededException (adafruit#263
Browse files Browse the repository at this point in the history
)

The "core rate limit reset" time is in UTC, while github actions
may be running in another timezone, such as PST. The below example
was run in my local timezone, CST:

```python
>>> import os, datetime
>>> import github as pygithub
>>> GH_INTERFACE = pygithub.Github(os.environ.get("ADABOT_GITHUB_ACCESS_TOKEN"))
>>> GH_INTERFACE.get_rate_limit().core.reset - datetime.datetime.now()
datetime.timedelta(seconds=24750, microseconds=356986)
>>> GH_INTERFACE.get_rate_limit().core.reset - datetime.datetime.utcnow()
datetime.timedelta(seconds=3147, microseconds=87271)
```

Use utcnow() instead of now() so that the sleep is for the correct
duration.

(utcnow is deprecated, but the correct alternative, `now(tz=datetime.timezone.utc)`
does not work: `GH_INTERFACE.get_rate_limit().core.reset` is a naive
timestamp and can't be compared with a tz-aware object)
  • Loading branch information
jepler committed Nov 27, 2023
1 parent 7a12af0 commit 8ba75e0
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion adabot/circuitpython_libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def run_library_checks(validators, kw_args, error_depth):
break
except pygithub.RateLimitExceededException:
core_rate_limit_reset = GH_INTERFACE.get_rate_limit().core.reset
sleep_time = core_rate_limit_reset - datetime.datetime.now()
sleep_time = core_rate_limit_reset - datetime.datetime.utcnow()
logging.warning("Rate Limit will reset at: %s", core_rate_limit_reset)
time.sleep(sleep_time.seconds)
continue
Expand Down
2 changes: 1 addition & 1 deletion adabot/lib/bundle_announcer.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def get_bundle_updates(full_repo_name: str) -> Tuple[Set[RepoResult], Set[RepoRe

except pygithub.RateLimitExceededException:
core_rate_limit_reset = GH_INTERFACE.get_rate_limit().core.reset
sleep_time = core_rate_limit_reset - datetime.datetime.now()
sleep_time = core_rate_limit_reset - datetime.datetime.utcnow()
logging.warning("Rate Limit will reset at: %s", core_rate_limit_reset)
time.sleep(sleep_time.seconds)
continue
Expand Down
4 changes: 2 additions & 2 deletions adabot/lib/circuitpython_library_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ def validate_readthedocs(self, repo):
break
except pygithub.RateLimitExceededException:
core_rate_limit_reset = GH_INTERFACE.get_rate_limit().core.reset
sleep_time = core_rate_limit_reset - datetime.datetime.now()
sleep_time = core_rate_limit_reset - datetime.datetime.utcnow()
logging.warning("Rate Limit will reset at: %s", core_rate_limit_reset)
time.sleep(sleep_time.seconds)
continue
Expand Down Expand Up @@ -1275,7 +1275,7 @@ def validate_actions_state(self, repo):
return []
except pygithub.RateLimitExceededException:
core_rate_limit_reset = GH_INTERFACE.get_rate_limit().core.reset
sleep_time = core_rate_limit_reset - datetime.datetime.now()
sleep_time = core_rate_limit_reset - datetime.datetime.utcnow()
logging.warning("Rate Limit will reset at: %s", core_rate_limit_reset)
time.sleep(sleep_time.seconds)

Expand Down

0 comments on commit 8ba75e0

Please sign in to comment.