Skip to content

Commit e0d3a27

Browse files
gh retry policy and exp backoff for urlopen
1 parent f47299a commit e0d3a27

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

recipe/fixup_sha.py

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,55 @@
11
# Call like python3 __file__ python/ray/_version.py 2.49.2
22

3-
import json;
3+
import json
44
import os
55
import sys
6-
from urllib.request import urlopen;
6+
import time
7+
from urllib.request import HTTPError, urlopen
8+
9+
10+
def get_commit_sha(ver: str) -> str:
11+
"""
12+
Return sha from version tag commit
13+
14+
Use retry information in headers and exponential backoff to address rate limiting issues.
15+
See: https://docs.github.com/en/rest/using-the-rest-api/best-practices-for-using-the-rest-api?apiVersion=2022-11-28#handle-rate-limit-errors-appropriately
16+
"""
17+
exp_backoff = 60
18+
retry_count = 0
19+
while True:
20+
try:
21+
content = urlopen(
22+
f"https://api.github.com/repos/ray-project/ray/git/ref/tags/ray-{ver}"
23+
).read()
24+
return json.loads(content)["object"]["sha"]
25+
except HTTPError as e:
26+
if retry_count < 10 and e.code == 403 and e.msg == "rate limit exceeded":
27+
retry_count += 1
28+
timeout = exp_backoff
29+
if "retry-after" in e.hdrs:
30+
timeout = int(e.hdrs["retry-after"])
31+
elif e.hdrs.get("x-ratelimit-remaining", None) == "0":
32+
timeout = max(int(e.hdrs["x-ratelimit-reset"]) - time.gmtime(), 0)
33+
else:
34+
exp_backoff *= 1.5
35+
print(f"{e}:, retrying after {timeout} s", file=sys.stderr)
36+
time.sleep(timeout)
37+
continue
38+
raise
39+
740

841
versionpy = sys.argv[1]
942
ver = sys.argv[2]
1043

1144
assert os.path.exists(versionpy)
1245

13-
content = urlopen(f'https://api.github.com/repos/ray-project/ray/git/ref/tags/ray-{ver}').read()
14-
sha = json.loads(content)['object']['sha']
15-
with open(versionpy, 'r') as fid:
46+
with open(versionpy, "r") as fid:
1647
txt = fid.read()
48+
49+
sha = get_commit_sha(ver)
50+
1751
txt = txt.replace("{{RAY_COMMIT_SHA}}", sha)
1852
assert f'version = "{ver}"' in txt
1953
assert f'commit = "{sha}"' in txt
20-
with open(versionpy, 'w') as fid:
54+
with open(versionpy, "w") as fid:
2155
fid.write(txt)

0 commit comments

Comments
 (0)