Skip to content

Commit 51fd796

Browse files
ksaurabh02ksaurabhAparaviRobertCraigie
authored andcommitted
fix(sockets): handle non-portable socket flags (#935)
* Not all socket constants are available on every platform * Match the style of base code * Add the change of the socket constant to async client * Fix lint errors * Fix formatting --------- Co-authored-by: Kumar Saurabh <saurabh.kumar@aparavi.com> Co-authored-by: Robert Craigie <robert@craigie.dev>
1 parent ac6cfee commit 51fd796

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

src/anthropic/_base_client.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
TYPE_CHECKING,
1818
Any,
1919
Dict,
20+
List,
2021
Type,
22+
Tuple,
2123
Union,
2224
Generic,
2325
Mapping,
@@ -806,11 +808,20 @@ def __init__(self, **kwargs: Any) -> None:
806808
kwargs.setdefault("follow_redirects", True)
807809

808810
if "transport" not in kwargs:
809-
socket_options = [
810-
(socket.SOL_SOCKET, socket.SO_KEEPALIVE, True),
811-
(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 60),
812-
(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 5),
813-
]
811+
socket_options: List[Tuple[int, int, Union[int, bool]]] = [(socket.SOL_SOCKET, socket.SO_KEEPALIVE, True)]
812+
813+
TCP_KEEPINTVL = getattr(socket, "TCP_KEEPINTVL", None)
814+
815+
if TCP_KEEPINTVL is not None:
816+
socket_options.append((socket.IPPROTO_TCP, TCP_KEEPINTVL, 60))
817+
elif sys.platform == "darwin":
818+
TCP_KEEPALIVE = getattr(socket, "TCP_KEEPALIVE", 0x10)
819+
socket_options.append((socket.IPPROTO_TCP, TCP_KEEPALIVE, 60))
820+
821+
TCP_KEEPCNT = getattr(socket, "TCP_KEEPCNT", None)
822+
if TCP_KEEPCNT is not None:
823+
socket_options.append((socket.IPPROTO_TCP, TCP_KEEPCNT, 5))
824+
814825
TCP_KEEPIDLE = getattr(socket, "TCP_KEEPIDLE", None)
815826
if TCP_KEEPIDLE is not None:
816827
socket_options.append((socket.IPPROTO_TCP, TCP_KEEPIDLE, 60))
@@ -1335,11 +1346,20 @@ def __init__(self, **kwargs: Any) -> None:
13351346
kwargs.setdefault("follow_redirects", True)
13361347

13371348
if "transport" not in kwargs:
1338-
socket_options = [
1339-
(socket.SOL_SOCKET, socket.SO_KEEPALIVE, True),
1340-
(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 60),
1341-
(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 5),
1342-
]
1349+
socket_options: List[Tuple[int, int, Union[int, bool]]] = [(socket.SOL_SOCKET, socket.SO_KEEPALIVE, True)]
1350+
1351+
TCP_KEEPINTVL = getattr(socket, "TCP_KEEPINTVL", None)
1352+
1353+
if TCP_KEEPINTVL is not None:
1354+
socket_options.append((socket.IPPROTO_TCP, TCP_KEEPINTVL, 60))
1355+
elif sys.platform == "darwin":
1356+
TCP_KEEPALIVE = getattr(socket, "TCP_KEEPALIVE", 0x10)
1357+
socket_options.append((socket.IPPROTO_TCP, TCP_KEEPALIVE, 60))
1358+
1359+
TCP_KEEPCNT = getattr(socket, "TCP_KEEPCNT", None)
1360+
if TCP_KEEPCNT is not None:
1361+
socket_options.append((socket.IPPROTO_TCP, TCP_KEEPCNT, 5))
1362+
13431363
TCP_KEEPIDLE = getattr(socket, "TCP_KEEPIDLE", None)
13441364
if TCP_KEEPIDLE is not None:
13451365
socket_options.append((socket.IPPROTO_TCP, TCP_KEEPIDLE, 60))

0 commit comments

Comments
 (0)