Skip to content

Commit 3a1664f

Browse files
committed
Handle secondary rate limit
1 parent 0d40cb5 commit 3a1664f

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

devstats/query.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,19 @@ def send_query(query, query_type, headers, cursor=None):
8686
query = query[:cursor_ind] + f'after:"{cursor}", ' + query[cursor_ind:]
8787
# Build request payload
8888
payload = {"query": "".join(query.split("\n"))}
89-
response = requests.post(endpoint, json=payload, headers=headers)
89+
90+
retry = True
91+
while retry:
92+
response = requests.post(endpoint, json=payload, headers=headers)
93+
data = json.loads(response.content)
94+
if "exceeded a secondary rate limit" in data.get("message", ""):
95+
print("Secondary rate limit exceeded; sleeping 2mins")
96+
time.sleep(2 * 60)
97+
else:
98+
retry = False
99+
90100
rate_limit = {h: response.headers[h] for h in ("x-ratelimit-remaining",)}
91-
return {**json.loads(response.content), **rate_limit}
101+
return {**data, **rate_limit}
92102

93103

94104
def get_all_responses(query, query_type, headers):

0 commit comments

Comments
 (0)