Skip to content

Commit 3d87756

Browse files
authored
TCP Keep-Alive Environment Variable Support (boto#3579)
1 parent aa92d99 commit 3d87756

4 files changed

Lines changed: 34 additions & 33 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type": "enhancement",
3+
"category": "config",
4+
"description": "Add support for TCP Keep-Alive configuration via BOTOCORE_TCP_KEEPALIVE environment variable"
5+
}

botocore/args.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -556,13 +556,15 @@ def _resolve_endpoint(
556556
def _compute_socket_options(self, scoped_config, client_config=None):
557557
# This disables Nagle's algorithm and is the default socket options
558558
# in urllib3.
559+
559560
socket_options = [(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)]
560561
client_keepalive = client_config and client_config.tcp_keepalive
561-
scoped_keepalive = scoped_config and self._ensure_boolean(
562-
scoped_config.get("tcp_keepalive", False)
563-
)
564-
# Enables TCP Keepalive if specified in client config object or shared config file.
565-
if client_keepalive or scoped_keepalive:
562+
if client_keepalive is None:
563+
client_keepalive = self._config_store.get_config_variable(
564+
'tcp_keepalive'
565+
)
566+
567+
if client_keepalive:
566568
socket_options.append((socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1))
567569
return socket_options
568570

botocore/configprovider.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@
204204
None,
205205
None,
206206
),
207+
'tcp_keepalive': (
208+
'tcp_keepalive',
209+
'BOTOCORE_TCP_KEEPALIVE',
210+
None,
211+
utils.ensure_boolean,
212+
),
207213
}
208214
# A mapping for the s3 specific configuration vars. These are the configuration
209215
# vars that typically go in the s3 section of the config file. This mapping

tests/unit/test_args.py

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -240,42 +240,20 @@ def test_region_does_not_resolve_if_not_s3_and_endpoint_url_provided(self):
240240
)
241241
self.assertEqual(client_args['client_config'].region_name, None)
242242

243-
def test_tcp_keepalive_enabled_scoped_config(self):
244-
scoped_config = {'tcp_keepalive': 'true'}
245-
with mock.patch('botocore.args.EndpointCreator') as m:
246-
self.call_get_client_args(scoped_config=scoped_config)
247-
self.assert_create_endpoint_call(
248-
m,
249-
socket_options=self.default_socket_options
250-
+ [(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)],
251-
)
252-
253243
def test_tcp_keepalive_not_specified(self):
254244
with mock.patch('botocore.args.EndpointCreator') as m:
255-
self.call_get_client_args(scoped_config={}, client_config=None)
245+
self.call_get_client_args(client_config=None)
256246
self.assert_create_endpoint_call(
257247
m, socket_options=self.default_socket_options
258248
)
259-
self.call_get_client_args(
260-
scoped_config=None, client_config=Config()
261-
)
249+
self.call_get_client_args(client_config=Config())
262250
self.assert_create_endpoint_call(
263251
m, socket_options=self.default_socket_options
264252
)
265253

266254
def test_tcp_keepalive_enabled_if_set_anywhere(self):
267255
with mock.patch('botocore.args.EndpointCreator') as m:
268256
self.call_get_client_args(
269-
scoped_config={'tcp_keepalive': 'true'},
270-
client_config=Config(tcp_keepalive=False),
271-
)
272-
self.assert_create_endpoint_call(
273-
m,
274-
socket_options=self.default_socket_options
275-
+ [(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)],
276-
)
277-
self.call_get_client_args(
278-
scoped_config={'tcp_keepalive': 'false'},
279257
client_config=Config(tcp_keepalive=True),
280258
)
281259
self.assert_create_endpoint_call(
@@ -284,18 +262,28 @@ def test_tcp_keepalive_enabled_if_set_anywhere(self):
284262
+ [(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)],
285263
)
286264

265+
def test_tcp_keepalive_enabled_environment_variable(self):
266+
with mock.patch.dict('os.environ', {'BOTOCORE_TCP_KEEPALIVE': 'true'}):
267+
with mock.patch('botocore.args.EndpointCreator') as m:
268+
self.call_get_client_args()
269+
self.assert_create_endpoint_call(
270+
m,
271+
socket_options=self.default_socket_options
272+
+ [(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)],
273+
)
274+
287275
def test_tcp_keepalive_explicitly_disabled(self):
288-
scoped_config = {'tcp_keepalive': 'false'}
276+
self.config_store.set_config_variable('tcp_keepalive', False)
289277
with mock.patch('botocore.args.EndpointCreator') as m:
290-
self.call_get_client_args(scoped_config=scoped_config)
278+
self.call_get_client_args()
291279
self.assert_create_endpoint_call(
292280
m, socket_options=self.default_socket_options
293281
)
294282

295283
def test_tcp_keepalive_enabled_case_insensitive(self):
296-
scoped_config = {'tcp_keepalive': 'True'}
284+
self.config_store.set_config_variable('tcp_keepalive', 'True')
297285
with mock.patch('botocore.args.EndpointCreator') as m:
298-
self.call_get_client_args(scoped_config=scoped_config)
286+
self.call_get_client_args()
299287
self.assert_create_endpoint_call(
300288
m,
301289
socket_options=self.default_socket_options

0 commit comments

Comments
 (0)