Skip to content

Commit

Permalink
allow to override keep_alive with socket_options
Browse files Browse the repository at this point in the history
  • Loading branch information
sentrivana committed Mar 19, 2024
1 parent 4449fa6 commit 19534de
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
15 changes: 12 additions & 3 deletions sentry_sdk/transport.py
Expand Up @@ -462,10 +462,19 @@ def _get_pool_options(self, ca_certs):
"ca_certs": ca_certs or certifi.where(),
}

socket_options = []

Check warning on line 465 in sentry_sdk/transport.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/transport.py#L465

Added line #L465 was not covered by tests

if self.options["socket_options"]:
options["socket_options"] = self.options["socket_options"]
elif self.options["keep_alive"]:
options["socket_options"] = KEEP_ALIVE_SOCKET_OPTIONS
socket_options = self.options["socket_options"]

Check warning on line 468 in sentry_sdk/transport.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/transport.py#L468

Added line #L468 was not covered by tests

if self.options["keep_alive"]:
used_options = {(o[0], o[1]) for o in socket_options}
for default_option in KEEP_ALIVE_SOCKET_OPTIONS:
if (default_option[0], default_option[1]) not in used_options:
socket_options.append(default_option)

Check warning on line 474 in sentry_sdk/transport.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/transport.py#L474

Added line #L474 was not covered by tests

if socket_options:
options["socket_options"] = socket_options

Check warning on line 477 in sentry_sdk/transport.py

View check run for this annotation

Codecov / codecov/patch

sentry_sdk/transport.py#L477

Added line #L477 was not covered by tests

return options

Expand Down
16 changes: 16 additions & 0 deletions tests/test_transport.py
Expand Up @@ -187,6 +187,22 @@ def test_socket_options_override_keep_alive(make_client):
assert options["socket_options"] == socket_options


def test_socket_options_merge_keep_alive_with_socket_options(make_client):
socket_options = [
(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 42),
(socket.SOL_TCP, socket.TCP_KEEPINTVL, 42),
]

client = make_client(socket_options=socket_options, keep_alive=True)

options = client.transport._get_pool_options([])
assert options["socket_options"] == [
(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 42),
(socket.SOL_TCP, socket.TCP_KEEPINTVL, 42),
(socket.SOL_TCP, socket.TCP_KEEPCNT, 6),
]


def test_keep_alive_off_by_default(make_client):
client = make_client()

Expand Down

0 comments on commit 19534de

Please sign in to comment.