1414 DEFAULT_MAX_RETRIES ,
1515 DEFAULT_MIN_DELAY_BETWEEN_RETRIES ,
1616 DEFAULT_REQUEST_TIMEOUT ,
17+ DEFAULT_REQUEST_TIMEOUT_MAX ,
1718 Timeout ,
1819)
1920from apify_client ._docs import docs_group
@@ -54,18 +55,19 @@ def _compute_timeout(
5455 client_timeout : timedelta ,
5556 attempt : int ,
5657 timeout : Timeout ,
58+ timeout_max : timedelta ,
5759) -> float :
58- """Compute timeout for a request attempt with exponential increase, bounded by client timeout .
60+ """Compute timeout for a request attempt with exponential increase, bounded by timeout_max .
5961
6062 For `'no_timeout'`, returns 0 (impit interprets this as no timeout). For `None`, uses the client-level timeout.
61- For `timedelta` values, doubles the timeout with each attempt but caps at the client-level timeout.
63+ For `timedelta` values, doubles the timeout with each attempt but caps at the maximum timeout.
6264 """
6365 if timeout == 'no_timeout' :
6466 return 0
6567
6668 timeout_secs = to_seconds (timeout or client_timeout )
67- client_timeout_secs = to_seconds (client_timeout )
68- return min (client_timeout_secs , timeout_secs * 2 ** (attempt - 1 ))
69+ timeout_max_secs = to_seconds (timeout_max )
70+ return min (timeout_max_secs , timeout_secs * 2 ** (attempt - 1 ))
6971
7072
7173@docs_group ('HTTP clients' )
@@ -82,6 +84,7 @@ def __init__(
8284 * ,
8385 token : str | None = None ,
8486 timeout : timedelta = DEFAULT_REQUEST_TIMEOUT ,
87+ timeout_max : timedelta = DEFAULT_REQUEST_TIMEOUT_MAX ,
8588 max_retries : int = DEFAULT_MAX_RETRIES ,
8689 min_delay_between_retries : timedelta = DEFAULT_MIN_DELAY_BETWEEN_RETRIES ,
8790 statistics : ClientStatistics | None = None ,
@@ -91,7 +94,8 @@ def __init__(
9194
9295 Args:
9396 token: Apify API token for authentication.
94- timeout: Default timeout for HTTP requests.
97+ timeout: Default initial timeout for HTTP requests.
98+ timeout_max: Maximum timeout cap for exponential timeout growth across retries.
9599 max_retries: Maximum number of retry attempts for failed requests.
96100 min_delay_between_retries: Minimum delay between retries (increases exponentially with each attempt).
97101 statistics: Statistics tracker for API calls. Created automatically if not provided.
@@ -100,6 +104,7 @@ def __init__(
100104 super ().__init__ (
101105 token = token ,
102106 timeout = timeout ,
107+ timeout_max = timeout_max ,
103108 max_retries = max_retries ,
104109 min_delay_between_retries = min_delay_between_retries ,
105110 statistics = statistics ,
@@ -109,7 +114,7 @@ def __init__(
109114 self ._impit_client = impit .Client (
110115 headers = self ._headers ,
111116 follow_redirects = True ,
112- timeout = to_seconds (self ._timeout ),
117+ timeout = to_seconds (self ._timeout_max ),
113118 )
114119
115120 def call (
@@ -212,7 +217,7 @@ def _make_request(
212217 url = url_with_params ,
213218 headers = headers ,
214219 content = content ,
215- timeout = _compute_timeout (self ._timeout , attempt , timeout ),
220+ timeout = _compute_timeout (self ._timeout , attempt , timeout , self . _timeout_max ),
216221 stream = stream or False ,
217222 )
218223
@@ -309,6 +314,7 @@ def __init__(
309314 * ,
310315 token : str | None = None ,
311316 timeout : timedelta = DEFAULT_REQUEST_TIMEOUT ,
317+ timeout_max : timedelta = DEFAULT_REQUEST_TIMEOUT_MAX ,
312318 max_retries : int = DEFAULT_MAX_RETRIES ,
313319 min_delay_between_retries : timedelta = DEFAULT_MIN_DELAY_BETWEEN_RETRIES ,
314320 statistics : ClientStatistics | None = None ,
@@ -318,7 +324,8 @@ def __init__(
318324
319325 Args:
320326 token: Apify API token for authentication.
321- timeout: Default timeout for HTTP requests.
327+ timeout: Default initial timeout for HTTP requests.
328+ timeout_max: Maximum timeout cap for exponential timeout growth across retries.
322329 max_retries: Maximum number of retry attempts for failed requests.
323330 min_delay_between_retries: Minimum delay between retries (increases exponentially with each attempt).
324331 statistics: Statistics tracker for API calls. Created automatically if not provided.
@@ -327,6 +334,7 @@ def __init__(
327334 super ().__init__ (
328335 token = token ,
329336 timeout = timeout ,
337+ timeout_max = timeout_max ,
330338 max_retries = max_retries ,
331339 min_delay_between_retries = min_delay_between_retries ,
332340 statistics = statistics ,
@@ -336,7 +344,7 @@ def __init__(
336344 self ._impit_async_client = impit .AsyncClient (
337345 headers = self ._headers ,
338346 follow_redirects = True ,
339- timeout = to_seconds (self ._timeout ),
347+ timeout = to_seconds (self ._timeout_max ),
340348 )
341349
342350 async def call (
@@ -439,7 +447,7 @@ async def _make_request(
439447 url = url_with_params ,
440448 headers = headers ,
441449 content = content ,
442- timeout = _compute_timeout (self ._timeout , attempt , timeout ),
450+ timeout = _compute_timeout (self ._timeout , attempt , timeout , self . _timeout_max ),
443451 stream = stream or False ,
444452 )
445453
0 commit comments