Skip to content

Commit 26048f4

Browse files
Update http status codes to retry request on (#527)
* Update http status codes to retry request on * Fix lint check
1 parent 9b4ecb0 commit 26048f4

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

polygon/rest/base.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
import urllib3
44
import inspect
5+
from urllib3.util.retry import Retry
56
from enum import Enum
67
from typing import Optional, Any, Dict
78
from datetime import datetime
@@ -47,17 +48,28 @@ def __init__(
4748
"User-Agent": f"Polygon.io PythonClient/{version}",
4849
}
4950

51+
# initialize self.retries with the parameter value before using it
52+
self.retries = retries
53+
54+
# https://urllib3.readthedocs.io/en/stable/reference/urllib3.util.html#urllib3.util.Retry.RETRY_AFTER_STATUS_CODES
55+
retry_strategy = Retry(
56+
total=self.retries,
57+
status_forcelist=[413, 429, 500, 502, 503, 504], # default 413, 429, 503
58+
backoff_factor=0.1, # [0.0s, 0.2s, 0.4s, 0.8s, 1.6s, ...]
59+
)
60+
5061
# https://urllib3.readthedocs.io/en/stable/reference/urllib3.poolmanager.html
5162
# https://urllib3.readthedocs.io/en/stable/reference/urllib3.connectionpool.html#urllib3.HTTPConnectionPool
5263
self.client = urllib3.PoolManager(
5364
num_pools=num_pools,
5465
headers=self.headers, # default headers sent with each request.
5566
ca_certs=certifi.where(),
5667
cert_reqs="CERT_REQUIRED",
68+
retries=retry_strategy, # use the customized Retry instance
5769
)
5870

5971
self.timeout = urllib3.Timeout(connect=connect_timeout, read=read_timeout)
60-
self.retries = retries
72+
6173
if verbose:
6274
logger.setLevel(logging.DEBUG)
6375
self.trace = trace

0 commit comments

Comments
 (0)