|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | # Copyright (c) Alibaba, Inc. and its affiliates. |
| 3 | +import atexit |
3 | 4 | import datetime |
4 | 5 | import json |
| 6 | +import socket |
5 | 7 | import threading |
6 | 8 | from http import HTTPStatus |
7 | | -from typing import Callable, Optional, Dict, Union |
| 9 | +from typing import Callable, List, Optional, Dict, Tuple, Union |
8 | 10 |
|
9 | 11 | import aiohttp |
10 | 12 | import requests |
| 13 | +from requests.adapters import HTTPAdapter |
11 | 14 |
|
12 | 15 | from dashscope.api_entities.aio_session import ( |
13 | 16 | get_shared_aio_session, |
|
34 | 37 | _shared_sync_session: Optional[requests.Session] = None |
35 | 38 | _shared_sync_session_lock = threading.Lock() |
36 | 39 |
|
| 40 | +# TCP keepalive tuning for the shared synchronous session's connection |
| 41 | +# pool. A pooled keep-alive connection that has been silently dropped by |
| 42 | +# a NAT gateway or load balancer (no FIN/RST received) is detected by the |
| 43 | +# kernel once it stays idle for roughly |
| 44 | +# _TCP_KEEPIDLE_SECONDS + _TCP_KEEPCNT * _TCP_KEEPINTVL_SECONDS, so the |
| 45 | +# next reuse fails fast with ConnectionError (which is retried once) |
| 46 | +# instead of stalling until the full read timeout expires. |
| 47 | +_TCP_KEEPIDLE_SECONDS = 60 |
| 48 | +_TCP_KEEPINTVL_SECONDS = 30 |
| 49 | +_TCP_KEEPCNT = 3 |
| 50 | + |
| 51 | + |
| 52 | +def _tcp_keepalive_socket_options() -> List[Tuple[int, int, int]]: |
| 53 | + """Socket options enabling TCP keepalive on pooled connections. |
| 54 | +
|
| 55 | + urllib3 replaces its default socket options (TCP_NODELAY) as soon as |
| 56 | + custom options are supplied, so TCP_NODELAY is repeated here. The |
| 57 | + keepalive timer options are best-effort: TCP_KEEPIDLE is Linux, |
| 58 | + TCP_KEEPALIVE is the macOS equivalent, and on other platforms only |
| 59 | + SO_KEEPALIVE is applied with the system default timers. |
| 60 | + """ |
| 61 | + options = [ |
| 62 | + (socket.IPPROTO_TCP, socket.TCP_NODELAY, 1), |
| 63 | + (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1), |
| 64 | + ] |
| 65 | + keepidle = getattr(socket, "TCP_KEEPIDLE", None) |
| 66 | + if keepidle is None: |
| 67 | + keepidle = getattr(socket, "TCP_KEEPALIVE", None) # macOS |
| 68 | + if keepidle is not None: |
| 69 | + options.append( |
| 70 | + (socket.IPPROTO_TCP, keepidle, _TCP_KEEPIDLE_SECONDS), |
| 71 | + ) |
| 72 | + keepintvl = getattr(socket, "TCP_KEEPINTVL", None) |
| 73 | + if keepintvl is not None: |
| 74 | + options.append( |
| 75 | + (socket.IPPROTO_TCP, keepintvl, _TCP_KEEPINTVL_SECONDS), |
| 76 | + ) |
| 77 | + keepcnt = getattr(socket, "TCP_KEEPCNT", None) |
| 78 | + if keepcnt is not None: |
| 79 | + options.append((socket.IPPROTO_TCP, keepcnt, _TCP_KEEPCNT)) |
| 80 | + return options |
| 81 | + |
| 82 | + |
| 83 | +class _KeepAliveHTTPAdapter(HTTPAdapter): |
| 84 | + """HTTPAdapter whose pooled connections enable TCP keepalive.""" |
| 85 | + |
| 86 | + def init_poolmanager(self, connections, maxsize, block=False, **kwargs): |
| 87 | + kwargs.setdefault( |
| 88 | + "socket_options", |
| 89 | + _tcp_keepalive_socket_options(), |
| 90 | + ) |
| 91 | + super().init_poolmanager( |
| 92 | + connections, |
| 93 | + maxsize, |
| 94 | + block=block, |
| 95 | + **kwargs, |
| 96 | + ) |
| 97 | + |
| 98 | + def proxy_manager_for(self, proxy, **kwargs): |
| 99 | + kwargs.setdefault( |
| 100 | + "socket_options", |
| 101 | + _tcp_keepalive_socket_options(), |
| 102 | + ) |
| 103 | + return super().proxy_manager_for(proxy, **kwargs) |
| 104 | + |
| 105 | + |
| 106 | +def _create_shared_sync_session() -> requests.Session: |
| 107 | + session = requests.Session() |
| 108 | + adapter = _KeepAliveHTTPAdapter() |
| 109 | + session.mount("http://", adapter) |
| 110 | + session.mount("https://", adapter) |
| 111 | + return session |
| 112 | + |
37 | 113 |
|
38 | 114 | def _get_shared_sync_session() -> requests.Session: |
39 | 115 | """Return a shared requests.Session for connection pooling. |
40 | 116 |
|
41 | | - The session is created lazily and reused for the process lifetime; |
42 | | - it is never closed explicitly. |
| 117 | + The session is created lazily and reused for the process lifetime. |
| 118 | + Call close_shared_sync_session() to release its pooled connections. |
43 | 119 | """ |
44 | 120 | global _shared_sync_session |
45 | 121 | if _shared_sync_session is None: |
46 | 122 | with _shared_sync_session_lock: |
47 | 123 | if _shared_sync_session is None: |
48 | | - _shared_sync_session = requests.Session() |
| 124 | + _shared_sync_session = _create_shared_sync_session() |
49 | 125 | return _shared_sync_session |
50 | 126 |
|
51 | 127 |
|
| 128 | +def close_shared_sync_session() -> None: |
| 129 | + """Close the shared synchronous session and its pooled connections. |
| 130 | +
|
| 131 | + Long-running applications may call this to bound the lifetime of the |
| 132 | + SDK-managed connection pool (e.g. periodically, or after a transport |
| 133 | + failure). A fresh session is created lazily on the next request. |
| 134 | + """ |
| 135 | + global _shared_sync_session |
| 136 | + with _shared_sync_session_lock: |
| 137 | + session = _shared_sync_session |
| 138 | + _shared_sync_session = None |
| 139 | + if session is not None: |
| 140 | + session.close() |
| 141 | + |
| 142 | + |
| 143 | +# Release pooled connections at interpreter exit. |
| 144 | +atexit.register(close_shared_sync_session) |
| 145 | + |
| 146 | + |
52 | 147 | def _send_with_retry( |
53 | 148 | send: Callable[[], requests.Response], |
54 | 149 | ) -> requests.Response: |
|
0 commit comments