Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tcp_keepalive to Config object #2766

Merged
merged 1 commit into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions botocore/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ def compute_client_args(
'protocol': protocol,
'config_kwargs': config_kwargs,
's3_config': s3_config,
'socket_options': self._compute_socket_options(scoped_config),
'socket_options': self._compute_socket_options(
scoped_config, client_config
),
}

def compute_s3_config(self, client_config):
Expand Down Expand Up @@ -387,16 +389,17 @@ def _resolve_endpoint(
service_name, region_name, endpoint_url, is_secure
)

def _compute_socket_options(self, scoped_config):
def _compute_socket_options(self, scoped_config, client_config=None):
# This disables Nagle's algorithm and is the default socket options
# in urllib3.
socket_options = [(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)]
if scoped_config:
# Enables TCP Keepalive if specified in shared config file.
if self._ensure_boolean(scoped_config.get('tcp_keepalive', False)):
socket_options.append(
(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)
)
client_keepalive = client_config and client_config.tcp_keepalive
scoped_keepalive = scoped_config and self._ensure_boolean(
scoped_config.get("tcp_keepalive", False)
)
# Enables TCP Keepalive if specified in client config object or shared config file.
if client_keepalive or scoped_keepalive:
socket_options.append((socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1))
return socket_options

def _compute_retry_config(self, config_kwargs):
Expand Down
7 changes: 7 additions & 0 deletions botocore/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ class Config:
endpoint resolution.

Defaults to None.

:type tcp_keepalive: bool
:param tcp_keepalive: Enables the TCP Keep-Alive socket option used when
creating new connections if set to True.

Defaults to False.
"""

OPTION_DEFAULTS = OrderedDict(
Expand All @@ -205,6 +211,7 @@ class Config:
('use_dualstack_endpoint', None),
('use_fips_endpoint', None),
('defaults_mode', None),
('tcp_keepalive', None),
]
)

Expand Down
32 changes: 29 additions & 3 deletions tests/unit/test_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def test_region_does_not_resolve_if_not_s3_and_endpoint_url_provided(self):
)
self.assertEqual(client_args['client_config'].region_name, None)

def test_tcp_keepalive_enabled(self):
def test_tcp_keepalive_enabled_scoped_config(self):
scoped_config = {'tcp_keepalive': 'true'}
with mock.patch('botocore.args.EndpointCreator') as m:
self.call_get_client_args(scoped_config=scoped_config)
Expand All @@ -200,13 +200,39 @@ def test_tcp_keepalive_enabled(self):
)

def test_tcp_keepalive_not_specified(self):
scoped_config = {}
with mock.patch('botocore.args.EndpointCreator') as m:
self.call_get_client_args(scoped_config=scoped_config)
self.call_get_client_args(scoped_config={}, client_config=None)
self.assert_create_endpoint_call(
m, socket_options=self.default_socket_options
)
self.call_get_client_args(
scoped_config=None, client_config=Config()
)
self.assert_create_endpoint_call(
m, socket_options=self.default_socket_options
)

def test_tcp_keepalive_enabled_if_set_anywhere(self):
with mock.patch('botocore.args.EndpointCreator') as m:
self.call_get_client_args(
scoped_config={'tcp_keepalive': 'true'},
client_config=Config(tcp_keepalive=False),
)
self.assert_create_endpoint_call(
m,
socket_options=self.default_socket_options
+ [(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)],
)
self.call_get_client_args(
scoped_config={'tcp_keepalive': 'false'},
client_config=Config(tcp_keepalive=True),
)
self.assert_create_endpoint_call(
m,
socket_options=self.default_socket_options
+ [(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)],
)

def test_tcp_keepalive_explicitly_disabled(self):
scoped_config = {'tcp_keepalive': 'false'}
with mock.patch('botocore.args.EndpointCreator') as m:
Expand Down