99
1010def get_commit_sha (ver : str ) -> str :
1111 """
12- Return sha from version tag commit
12+ Return sha from version tag commit.
1313
1414 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
15+ See: https://docs.github.com/en/rest/using-the-rest-api/rate-limits -for-the-rest-api?apiVersion=2022-11-28#exceeding-the- rate-limit
1616 """
1717 exp_backoff = 60
1818 retry_count = 0
@@ -23,18 +23,19 @@ def get_commit_sha(ver: str) -> str:
2323 ).read ()
2424 return json .loads (content )["object" ]["sha" ]
2525 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
26+ if retry_count < 10 :
27+ if (e .code == 403 and e .msg == "rate limit exceeded" ) or e .code == 429 :
28+ retry_count += 1
29+ timeout = exp_backoff
30+ if "retry-after" in e .hdrs :
31+ timeout = int (e .hdrs ["retry-after" ])
32+ elif e .hdrs .get ("x-ratelimit-remaining" , None ) == "0" :
33+ timeout = max (int (e .hdrs ["x-ratelimit-reset" ]) - time .time (), 0 )
34+ else :
35+ exp_backoff *= 1.5
36+ print (f"{ e } :, retrying after { timeout } s" , file = sys .stderr )
37+ time .sleep (timeout )
38+ continue
3839 raise
3940
4041
0 commit comments