Skip to content

Commit e1fc686

Browse files
fix(http_request): enable TCP keepalive on shared sync session pool (#187)
- Enable TCP keepalive socket options on the shared sync session's connection pool so silently stale pooled connections are detected by the kernel instead of stalling until the 300s read timeout (fixes #186) - Add public dashscope.close_shared_sync_session() to let long-running applications reset the SDK-managed connection pool - Close pooled connections at interpreter exit - Add unit tests for keepalive options and session lifecycle Fixes #186
1 parent f64d35f commit e1fc686

4 files changed

Lines changed: 222 additions & 65 deletions

File tree

‎dashscope/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
)
3232
from dashscope.audio.tts.speech_synthesizer import SpeechSynthesizer
3333
from dashscope.api_entities.aio_session import close_shared_aio_session
34+
from dashscope.api_entities.http_request import close_shared_sync_session
3435
from dashscope.common.api_key import save_api_key
3536
from dashscope.common.env import (
3637
api_key,
@@ -89,6 +90,7 @@
8990
"api_key_file_path",
9091
"save_api_key",
9192
"close_shared_aio_session",
93+
"close_shared_sync_session",
9294
"AioGeneration",
9395
"Conversation",
9496
"Generation",

‎dashscope/__init__.pyi‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ base_websocket_api_url: str
2222

2323
def save_api_key(api_key: str, file_path: Optional[str] = ...) -> None: ...
2424
def close_shared_aio_session() -> None: ...
25+
def close_shared_sync_session() -> None: ...
2526

2627
# ---------------------------------------------------------------------------
2728
# Response types

‎dashscope/api_entities/http_request.py‎

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# -*- coding: utf-8 -*-
22
# Copyright (c) Alibaba, Inc. and its affiliates.
3+
import atexit
34
import datetime
45
import json
6+
import socket
57
import threading
68
from http import HTTPStatus
7-
from typing import Callable, Optional, Dict, Union
9+
from typing import Callable, List, Optional, Dict, Tuple, Union
810

911
import aiohttp
1012
import requests
13+
from requests.adapters import HTTPAdapter
1114

1215
from dashscope.api_entities.aio_session import (
1316
get_shared_aio_session,
@@ -34,21 +37,113 @@
3437
_shared_sync_session: Optional[requests.Session] = None
3538
_shared_sync_session_lock = threading.Lock()
3639

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+
37113

38114
def _get_shared_sync_session() -> requests.Session:
39115
"""Return a shared requests.Session for connection pooling.
40116
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.
43119
"""
44120
global _shared_sync_session
45121
if _shared_sync_session is None:
46122
with _shared_sync_session_lock:
47123
if _shared_sync_session is None:
48-
_shared_sync_session = requests.Session()
124+
_shared_sync_session = _create_shared_sync_session()
49125
return _shared_sync_session
50126

51127

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+
52147
def _send_with_retry(
53148
send: Callable[[], requests.Response],
54149
) -> requests.Response:

0 commit comments

Comments
 (0)