albertvillanova HF staff commited on
Commit
dfd4a53
1 Parent(s): 719c272

Implement retry mechanism when HTTP timeout error

Browse files
Files changed (1) hide show
  1. src/hub.py +20 -2
src/hub.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import io
2
  import json
3
 
@@ -12,14 +13,31 @@ class Client:
12
  def __init__(self):
13
  self.client = httpx.AsyncClient(follow_redirects=True)
14
 
 
 
 
 
 
15
  async def get(self, url, headers=None, params=None):
16
  try:
17
- r = await self.client.get(url, headers=headers, params=params)
18
- r.raise_for_status()
 
19
  except httpx.HTTPError:
20
  return
21
  return r
22
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  client = Client()
25
  fs = HfFileSystem()
 
1
+ import asyncio
2
  import io
3
  import json
4
 
 
13
  def __init__(self):
14
  self.client = httpx.AsyncClient(follow_redirects=True)
15
 
16
+ async def _get(self, url, headers=None, params=None):
17
+ r = await self.client.get(url, headers=headers, params=params)
18
+ r.raise_for_status()
19
+ return r
20
+
21
  async def get(self, url, headers=None, params=None):
22
  try:
23
+ r = await self._get(url, headers=headers, params=params)
24
+ except httpx.ReadTimeout:
25
+ return await self.retry(self._get, url, headers=headers, params=params)
26
  except httpx.HTTPError:
27
  return
28
  return r
29
 
30
+ async def retry(self, func, url, max_retries=4, max_wait_time=8, wait_time=1, **kwargs):
31
+ for _ in range(max_retries):
32
+ try:
33
+ await asyncio.sleep(wait_time)
34
+ return await func(url, **kwargs)
35
+ except httpx.ReadTimeout:
36
+ wait_time = wait_time * 2
37
+ if wait_time > max_wait_time:
38
+ print("HTTP Timeout: max retries exceeded with url:", url)
39
+ return
40
+
41
 
42
  client = Client()
43
  fs = HfFileSystem()