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
This commit is contained in:
Thomas Bechtold 2016-03-07 12:59:18 +01:00
parent f81bc489b8
commit ced9d38287
2 changed files with 37 additions and 4 deletions

View File

@ -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))

View File

@ -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(