Merge "Use TCP Keep-Alive on the socket level"

This commit is contained in:
Jenkins 2015-01-23 23:05:53 +00:00 committed by Gerrit Code Review
commit 0ae527665a
2 changed files with 14 additions and 2 deletions

View File

@ -28,6 +28,7 @@ import hashlib
import logging
import os
import re
import socket
import time
from keystoneclient import adapter
@ -49,6 +50,17 @@ from novaclient.openstack.common import cliutils
from novaclient import service_catalog
class TCPKeepAliveAdapter(adapters.HTTPAdapter):
"""The custom adapter used to set TCP Keep-Alive on all connections."""
def init_poolmanager(self, *args, **kwargs):
if requests.__version__ >= '2.4.1':
kwargs.setdefault('socket_options', [
(socket.IPROTO_TCP, socket.TCP_NODELAY, 1),
(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1),
])
super(TCPKeepAliveAdapter, self).init_poolmanager(*args, **kwargs)
class _ClientConnectionPool(object):
def __init__(self):
@ -59,7 +71,7 @@ class _ClientConnectionPool(object):
Store and reuse HTTP adapters per Service URL.
"""
if url not in self._adapters:
self._adapters[url] = adapters.HTTPAdapter()
self._adapters[url] = TCPKeepAliveAdapter()
return self._adapters[url]

View File

@ -29,7 +29,7 @@ import novaclient.v1_1.client
class ClientConnectionPoolTest(utils.TestCase):
@mock.patch("novaclient.client.adapters.HTTPAdapter")
@mock.patch("novaclient.client.TCPKeepAliveAdapter")
def test_get(self, mock_http_adapter):
mock_http_adapter.side_effect = lambda: mock.Mock()
pool = novaclient.client._ClientConnectionPool()