Skip to content

Commit 2405c4c

Browse files
exponential backoff for urlopen
1 parent f47299a commit 2405c4c

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

recipe/fixup_sha.py

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,48 @@
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 exponential backoff to address rate limiting issues.
15+
"""
16+
timeout = 16
17+
while True:
18+
try:
19+
content = urlopen(
20+
f"https://api.github.com/repos/ray-project/ray/git/ref/tags/ray-{ver}"
21+
).read()
22+
return json.loads(content)["object"]["sha"]
23+
except HTTPError as e:
24+
if e.code == 403 and e.msg == "rate limit exceeded" and timeout <= 128:
25+
print(f"{e}, retrying after {timeout} s", file=sys.stderr)
26+
print("headers", e.hdrs, file=sys.stderr)
27+
print("env keys", list(os.environ))
28+
time.sleep(timeout)
29+
timeout *= 2
30+
continue
31+
raise
32+
733

834
versionpy = sys.argv[1]
935
ver = sys.argv[2]
1036

1137
assert os.path.exists(versionpy)
1238

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:
39+
with open(versionpy, "r") as fid:
1640
txt = fid.read()
41+
42+
sha = get_commit_sha(ver)
43+
1744
txt = txt.replace("{{RAY_COMMIT_SHA}}", sha)
1845
assert f'version = "{ver}"' in txt
1946
assert f'commit = "{sha}"' in txt
20-
with open(versionpy, 'w') as fid:
47+
with open(versionpy, "w") as fid:
2148
fid.write(txt)

0 commit comments

Comments
 (0)