From ced9d382876ed020bebe4fdd3a2a738334e16393 Mon Sep 17 00:00:00 2001 From: Thomas Bechtold Date: Mon, 7 Mar 2016 12:59:18 +0100 Subject: [PATCH] Set TCP keepalive options The following configuration options are in the sample config but are not used: - tcp_keepalive - tcp_keepidle - tcp_keepalive_interval - tcp_keepalive_count Use the utility function from oslo_utils to set the socket options. Change-Id: I38f501202184792f12bf153a800e39a4b1a3fccb Closes-Bug: #1554022 --- manila/tests/test_wsgi.py | 27 +++++++++++++++++++++++++++ manila/wsgi.py | 14 ++++++++++---- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/manila/tests/test_wsgi.py b/manila/tests/test_wsgi.py index fdb31a6d..2029b772 100644 --- a/manila/tests/test_wsgi.py +++ b/manila/tests/test_wsgi.py @@ -24,6 +24,7 @@ import ddt import eventlet import mock from oslo_config import cfg +from oslo_utils import netutils import six from six.moves import urllib import testtools @@ -115,6 +116,32 @@ class TestWSGIServer(test.TestCase): server.stop() server.wait() + def test_start_with_default_tcp_options(self): + server = manila.wsgi.Server("test_tcp_options", + None, + host="127.0.0.1") + self.mock_object( + netutils, 'set_tcp_keepalive') + server.start() + netutils.set_tcp_keepalive.assert_called_once_with( + mock.ANY, tcp_keepalive=True, tcp_keepalive_count=None, + tcp_keepalive_interval=None, tcp_keepidle=600) + + def test_start_with_custom_tcp_options(self): + CONF.set_default("tcp_keepalive", False) + CONF.set_default("tcp_keepalive_count", 33) + CONF.set_default("tcp_keepalive_interval", 22) + CONF.set_default("tcp_keepidle", 11) + server = manila.wsgi.Server("test_tcp_options", + None, + host="127.0.0.1") + self.mock_object( + netutils, 'set_tcp_keepalive') + server.start() + netutils.set_tcp_keepalive.assert_called_once_with( + mock.ANY, tcp_keepalive=False, tcp_keepalive_count=33, + tcp_keepalive_interval=22, tcp_keepidle=11) + def test_app(self): self.mock_object( eventlet, 'spawn', mock.Mock(side_effect=eventlet.spawn)) diff --git a/manila/wsgi.py b/manila/wsgi.py index 1db0271f..d8f7d382 100644 --- a/manila/wsgi.py +++ b/manila/wsgi.py @@ -33,6 +33,7 @@ from oslo_config import cfg from oslo_log import log from oslo_service import service from oslo_utils import excutils +from oslo_utils import netutils from paste import deploy import routes.middleware import webob.dec @@ -199,6 +200,15 @@ class Server(service.ServiceBase): # to keep file descriptor usable. dup_socket = self._socket.dup() + + netutils.set_tcp_keepalive( + dup_socket, + tcp_keepalive=CONF.tcp_keepalive, + tcp_keepidle=CONF.tcp_keepidle, + tcp_keepalive_interval=CONF.tcp_keepalive_interval, + tcp_keepalive_count=CONF.tcp_keepalive_count + ) + if self._use_ssl: try: ssl_kwargs = { @@ -218,10 +228,6 @@ class Server(service.ServiceBase): dup_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - # sockets can hang around forever without keepalive - dup_socket.setsockopt(socket.SOL_SOCKET, - socket.SO_KEEPALIVE, 1) - except Exception: with excutils.save_and_reraise_exception(): LOG.error(