GET 上游并解析 JSON(relay 回源拉 /v1/models 用)。连接抖动重试,服务端明确响应不重试。
(url, headers, attempts=3, timeout=30)
| 302 | |
| 303 | |
| 304 | def http_get_json(url, headers, attempts=3, timeout=30): |
| 305 | """GET 上游并解析 JSON(relay 回源拉 /v1/models 用)。连接抖动重试,服务端明确响应不重试。""" |
| 306 | headers = {"User-Agent": UPSTREAM_UA, **headers} |
| 307 | for i in range(attempts): |
| 308 | req = urllib.request.Request(url, headers=headers, method="GET") |
| 309 | try: |
| 310 | with urllib.request.urlopen(req, timeout=timeout) as r: |
| 311 | return json.loads(r.read()) |
| 312 | except urllib.error.HTTPError: |
| 313 | raise |
| 314 | except Exception as e: |
| 315 | if i < attempts - 1: |
| 316 | log(f" ~ 上游连接抖动,重试 {i + 1}/{attempts - 1}: {e}") |
| 317 | time.sleep(0.6 * (i + 1)) |
| 318 | continue |
| 319 | raise |
| 320 | |
| 321 | |
| 322 | def normalize_openai_base(base): |
no test coverage detected