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

retry after exception when getting GitHub token #132

Merged
merged 2 commits into from
Jan 9, 2023
Merged
Changes from 1 commit
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
28 changes: 19 additions & 9 deletions connections/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# license: GPLv2
#
import datetime
import time


from tools import config, logging
from github import Github, GithubIntegration
Expand All @@ -28,15 +30,23 @@ def get_token():
with open(private_key_path, 'r') as private_key_file:
private_key = private_key_file.read()

# If the config keys are not set, get_access_token will raise a NotImplementedError
# Returning NoneType token will stop the connection in get_instance
try:
github_integration = GithubIntegration(app_id, private_key)
# Note that installation access tokens last only for 1 hour, you will need to regenerate them after they expire.
_token = github_integration.get_access_token(installation_id)
except NotImplementedError as err:
logging.error(err)
_token = None
tries = 3
for i in range(tries):
# If the config keys are not set, get_access_token will raise a NotImplementedError
# Returning NoneType token will stop the connection in get_instance
try:
github_integration = GithubIntegration(app_id, private_key)
# Note that installation access tokens last only for 1 hour,
# you will need to regenerate them after they expire.
_token = github_integration.get_access_token(installation_id)
except NotImplementedError as err:
if i < tries - 1:
time.sleep(0.8)
Copy link
Contributor

Choose a reason for hiding this comment

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

This always waits 0.8 seconds. Maybe the waiting time could be $n * (i+1)$ seconds? $n$ being the default/minimum waiting time (thus waiting time would increase linearly: $n, 2n, 3n, \dots$). Alternatively, the waiting time could be $n^{(i+1)}$ seconds (thus waiting time would increase exponentially: $n, n^2, n^3, \dots$).

Copy link
Contributor

Choose a reason for hiding this comment

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

To be on the safe side, maybe wait for $1 + i * n$ seconds.

continue
else:
logging.error(err)
_token = None
break
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the break needed?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes it's needed. Probably easier to read if the break is moved to after line 41.


return _token

Expand Down