From f44d6756cf85be83a54931cfcaae3615cc96d5d3 Mon Sep 17 00:00:00 2001 From: Jacob Strieb Date: Mon, 5 Oct 2020 12:35:55 -0400 Subject: [PATCH] Add timeout to limit indefinite 202s Fix #2 Fix #3 --- github_stats.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/github_stats.py b/github_stats.py index f52364ff51e..d20e0bbb0fd 100644 --- a/github_stats.py +++ b/github_stats.py @@ -42,6 +42,7 @@ async def query(self, generated_query: str) -> Dict: json={"query": generated_query}) return await r.json() except: + print("aiohttp failed for GraphQL query") # Fall back on non-async requests async with self.semaphore: r = requests.post("https://api.github.com/graphql", @@ -57,7 +58,7 @@ async def query_rest(self, path: str, params: Optional[Dict] = None) -> Dict: :return: deserialized REST JSON output """ - while True: + for _ in range(60): headers = { "Authorization": f"token {self.access_token}", } @@ -80,6 +81,7 @@ async def query_rest(self, path: str, params: Optional[Dict] = None) -> Dict: if result is not None: return result except: + print("aiohttp failed for rest query") # Fall back on non-async requests async with self.semaphore: r = requests.get(f"https://api.github.com/{path}", @@ -89,8 +91,11 @@ async def query_rest(self, path: str, params: Optional[Dict] = None) -> Dict: print(f"A path returned 202. Retrying...") await asyncio.sleep(1) continue - - return r.json() + elif r.status_code == 200: + return r.json() + # print(f"There were too many 202s. Data for {path} will be incomplete.") + print("There were too many 202s. Data for this repository will be incomplete.") + return dict() @staticmethod def repos_overview(contrib_cursor: Optional[str] = None,