|
2 | 2 | import json
|
3 | 3 | import urllib3
|
4 | 4 | import inspect
|
| 5 | +from urllib3.util.retry import Retry |
5 | 6 | from enum import Enum
|
6 | 7 | from typing import Optional, Any, Dict
|
7 | 8 | from datetime import datetime
|
@@ -47,17 +48,28 @@ def __init__(
|
47 | 48 | "User-Agent": f"Polygon.io PythonClient/{version}",
|
48 | 49 | }
|
49 | 50 |
|
| 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 | + |
50 | 61 | # https://urllib3.readthedocs.io/en/stable/reference/urllib3.poolmanager.html
|
51 | 62 | # https://urllib3.readthedocs.io/en/stable/reference/urllib3.connectionpool.html#urllib3.HTTPConnectionPool
|
52 | 63 | self.client = urllib3.PoolManager(
|
53 | 64 | num_pools=num_pools,
|
54 | 65 | headers=self.headers, # default headers sent with each request.
|
55 | 66 | ca_certs=certifi.where(),
|
56 | 67 | cert_reqs="CERT_REQUIRED",
|
| 68 | + retries=retry_strategy, # use the customized Retry instance |
57 | 69 | )
|
58 | 70 |
|
59 | 71 | self.timeout = urllib3.Timeout(connect=connect_timeout, read=read_timeout)
|
60 |
| - self.retries = retries |
| 72 | + |
61 | 73 | if verbose:
|
62 | 74 | logger.setLevel(logging.DEBUG)
|
63 | 75 | self.trace = trace
|
|
0 commit comments