Skip to content

Commit 19534de

Browse files
committed
allow to override keep_alive with socket_options
1 parent 4449fa6 commit 19534de

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

sentry_sdk/transport.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -462,10 +462,19 @@ def _get_pool_options(self, ca_certs):
462462
"ca_certs": ca_certs or certifi.where(),
463463
}
464464

465+
socket_options = []
466+
465467
if self.options["socket_options"]:
466-
options["socket_options"] = self.options["socket_options"]
467-
elif self.options["keep_alive"]:
468-
options["socket_options"] = KEEP_ALIVE_SOCKET_OPTIONS
468+
socket_options = self.options["socket_options"]
469+
470+
if self.options["keep_alive"]:
471+
used_options = {(o[0], o[1]) for o in socket_options}
472+
for default_option in KEEP_ALIVE_SOCKET_OPTIONS:
473+
if (default_option[0], default_option[1]) not in used_options:
474+
socket_options.append(default_option)
475+
476+
if socket_options:
477+
options["socket_options"] = socket_options
469478

470479
return options
471480

tests/test_transport.py

+16
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,22 @@ def test_socket_options_override_keep_alive(make_client):
187187
assert options["socket_options"] == socket_options
188188

189189

190+
def test_socket_options_merge_keep_alive_with_socket_options(make_client):
191+
socket_options = [
192+
(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 42),
193+
(socket.SOL_TCP, socket.TCP_KEEPINTVL, 42),
194+
]
195+
196+
client = make_client(socket_options=socket_options, keep_alive=True)
197+
198+
options = client.transport._get_pool_options([])
199+
assert options["socket_options"] == [
200+
(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 42),
201+
(socket.SOL_TCP, socket.TCP_KEEPINTVL, 42),
202+
(socket.SOL_TCP, socket.TCP_KEEPCNT, 6),
203+
]
204+
205+
190206
def test_keep_alive_off_by_default(make_client):
191207
client = make_client()
192208

0 commit comments

Comments
 (0)