|
31 | 31 | import hmac |
32 | 32 | import os |
33 | 33 | import platform |
| 34 | +import time |
34 | 35 | import requests |
35 | 36 | from urllib.parse import quote |
36 | 37 |
|
|
46 | 47 | MAX_X_CUSTOM_SOURCE_LENGTH = 256 |
47 | 48 |
|
48 | 49 |
|
| 50 | +# Transient-failure handling for signed OpenAPI calls. Historically this used a |
| 51 | +# single timeout-less ``requests.request`` — a stalled connection could hang |
| 52 | +# forever, and a transient overload (429/503) or connection error failed the |
| 53 | +# call outright with no retry. Both are now bounded and conservatively retried. |
| 54 | +# Tunable via env; AGENTKIT_HTTP_RETRIES=0 disables retries. |
| 55 | +_RETRYABLE_STATUS = frozenset({429, 503}) |
| 56 | + |
| 57 | + |
| 58 | +def _http_timeout() -> float: |
| 59 | + try: |
| 60 | + return max(1.0, float(os.getenv("AGENTKIT_HTTP_TIMEOUT", "30"))) |
| 61 | + except ValueError: |
| 62 | + return 30.0 |
| 63 | + |
| 64 | + |
| 65 | +def _http_retries() -> int: |
| 66 | + try: |
| 67 | + return max(0, int(os.getenv("AGENTKIT_HTTP_RETRIES", "2"))) |
| 68 | + except ValueError: |
| 69 | + return 2 |
| 70 | + |
| 71 | + |
| 72 | +def _backoff_seconds(attempt: int) -> float: |
| 73 | + return min(8.0, 0.5 * (2**attempt)) |
| 74 | + |
| 75 | + |
| 76 | +def _retry_after_seconds(resp: requests.Response) -> float | None: |
| 77 | + raw = resp.headers.get("Retry-After") |
| 78 | + if not raw: |
| 79 | + return None |
| 80 | + try: |
| 81 | + return max(0.0, float(raw)) |
| 82 | + except ValueError: |
| 83 | + return None |
| 84 | + |
| 85 | + |
| 86 | +def _signed_request(method, url, headers, params, data) -> requests.Response: |
| 87 | + """Issue a signed HTTP request with a bounded timeout and conservative |
| 88 | + transient-retry. |
| 89 | +
|
| 90 | + Only retries failures that almost certainly mean the request was not |
| 91 | + processed — connection errors (incl. connect timeouts) and HTTP 429/503 — |
| 92 | + so non-idempotent ``Create*`` actions are never double-executed. Read |
| 93 | + timeouts and other 5xx are surfaced, not retried. Honors ``Retry-After``. |
| 94 | + """ |
| 95 | + retries = _http_retries() |
| 96 | + timeout = _http_timeout() |
| 97 | + resp: requests.Response | None = None |
| 98 | + for attempt in range(retries + 1): |
| 99 | + try: |
| 100 | + resp = requests.request( |
| 101 | + method=method, |
| 102 | + url=url, |
| 103 | + headers=headers, |
| 104 | + params=params, |
| 105 | + data=data, |
| 106 | + timeout=timeout, |
| 107 | + ) |
| 108 | + except requests.ConnectionError: |
| 109 | + # Not delivered (connection failed/reset before completion), so a |
| 110 | + # retry cannot double-execute the request. |
| 111 | + if attempt < retries: |
| 112 | + time.sleep(_backoff_seconds(attempt)) |
| 113 | + continue |
| 114 | + raise |
| 115 | + if resp.status_code in _RETRYABLE_STATUS and attempt < retries: |
| 116 | + time.sleep(_retry_after_seconds(resp) or _backoff_seconds(attempt)) |
| 117 | + continue |
| 118 | + return resp |
| 119 | + assert resp is not None # loop always returns or raises before here |
| 120 | + return resp |
| 121 | + |
| 122 | + |
49 | 123 | def _get_os_tag() -> str: |
50 | 124 | system = platform.system().lower() |
51 | 125 | if "linux" in system: |
@@ -221,7 +295,7 @@ def request(method, date, query, header, ak, sk, action, body): |
221 | 295 | header = {**header, **sign_result} |
222 | 296 | # header = {**header, **{"X-Security-Token": SessionToken}} |
223 | 297 | # 第六步:将 Signature 签名写入 HTTP Header 中,并发送 HTTP 请求。 |
224 | | - r = requests.request( |
| 298 | + r = _signed_request( |
225 | 299 | method=method, |
226 | 300 | url=f"{Scheme}://{request_param['host']}{request_param['path']}", |
227 | 301 | headers=header, |
|
0 commit comments