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

Fix ratelimit timezone #362

Merged
merged 2 commits into from
Nov 27, 2023
Merged

Conversation

jepler
Copy link
Member

@jepler jepler commented Nov 27, 2023

I discovered that sites that use pygithub to wait for the rate limit reset time were performing incorrect arithmetic between an UTC timestamp and a local timestamp. This led to the sleep taking hours more time than they should.

Some requests are made via pygithub and some via our own API + requests; the requests code was right while the API code was wrong. This made the cron task fail sometimes, depending whether the first rate-limited result was via API or via raw requests.

>>> 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)

)

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)
@jepler jepler requested a review from dhalbert November 27, 2023 02:08
@jepler
Copy link
Member Author

jepler commented Nov 27, 2023

we're on an old pygithub. the time zone problem of the rate limit is fixed in 2.0.0 (current is 2.1.x)

PyGithub/PyGithub#2565

Copy link
Contributor

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

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

🎆 🕐

@dhalbert dhalbert merged commit b08b400 into adafruit:main Nov 27, 2023
3 checks passed
@dhalbert dhalbert linked an issue Nov 28, 2023 that may be closed by this pull request
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.

update_cp_org_libraries.py runs veryyyy slow
2 participants