Skip to content

Commit

Permalink
Delay port binding to reduce wait at process start
Browse files Browse the repository at this point in the history
With the new default behavior of eventlet 0.20 to set SO_REUSEPORT on listening
sockets, it becomes easy to have a zero-downtime reload.

By binding the listening socket as late as we can, we can reduce the
time an incoming connection will stay in queue, waiting for the process
to be ready to handle the connection.

This commit moves the socket binding after the load of the application.

On some benchmarks, it reduced the wait-time for incoming connections from
about 1.1s to about 350ms.

Change-Id: I045f4c9aa2c07cdc5ed95afd028540f1b3874637
  • Loading branch information
rledisez committed Jun 5, 2017
1 parent d51ecb4 commit 5d7b0d1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
13 changes: 7 additions & 6 deletions swift/common/wsgi.py
Expand Up @@ -892,12 +892,6 @@ def run_wsgi(conf_path, app_section, *args, **kwargs):
else:
strategy = WorkersStrategy(conf, logger)

error_msg = strategy.bind_ports()
if error_msg:
logger.error(error_msg)
print(error_msg)
return 1

# Ensure the configuration and application can be loaded before proceeding.
global_conf = {'log_name': log_name}
if 'global_conf_callback' in kwargs:
Expand All @@ -911,6 +905,13 @@ def run_wsgi(conf_path, app_section, *args, **kwargs):
# redirect errors to logger and close stdio
capture_stdio(logger)

# Start listening on bind_addr/port
error_msg = strategy.bind_ports()
if error_msg:
logger.error(error_msg)
print(error_msg)
return 1

no_fork_sock = strategy.no_fork_sock()
if no_fork_sock:
run_server(conf, logger, no_fork_sock, global_conf=global_conf)
Expand Down
4 changes: 3 additions & 1 deletion test/unit/common/test_wsgi.py
Expand Up @@ -730,7 +730,9 @@ def test_run_server_strategy_plumbing(self, mock_per_port, mock_workers,
logger,
'log_name',
]
with mock.patch.object(wsgi, '_initrp', return_value=stub__initrp):
with mock.patch.object(wsgi, '_initrp', return_value=stub__initrp), \
mock.patch.object(wsgi, 'loadapp'), \
mock.patch.object(wsgi, 'capture_stdio'):
for server_type in ('account-server', 'container-server',
'object-server'):
mock_per_port.reset_mock()
Expand Down

0 comments on commit 5d7b0d1

Please sign in to comment.