def create_custom_session(session_options, pool_size): LOG.debug("Using custom connection pool size: %s", pool_size) session = requests.Session() session.adapters['http://'] = ks_session.TCPKeepAliveAdapter( pool_maxsize=pool_size) session.adapters['https://'] = ks_session.TCPKeepAliveAdapter( pool_maxsize=pool_size) return ks_session.Session(session=session, **session_options)
def _construct_session_with_betamax(fixture, session_obj=None): # NOTE(morganfainberg): This function should contain the logic of # keystoneauth1.session._construct_session as it replaces the # _construct_session function to apply betamax magic to the requests # session object. if not session_obj: session_obj = requests.Session() # Use TCPKeepAliveAdapter to fix bug 1323862 for scheme in list(session_obj.adapters.keys()): session_obj.mount(scheme, session.TCPKeepAliveAdapter()) fixture.recorder = betamax.Betamax( session_obj, cassette_library_dir=fixture.cassette_library_dir) record = 'none' serializer = 'json' if fixture.record: record = 'all' if fixture.serializer: serializer = fixture.serializer.name fixture.recorder.use_cassette(fixture.cassette_name, serialize_with=serializer, record=record) fixture.recorder.start() fixture.addCleanup(fixture.recorder.stop) return session_obj
def _construct_session_with_betamax(fixture, session_obj=None): # NOTE(morganfainberg): This function should contain the logic of # keystoneauth1.session._construct_session as it replaces the # _construct_session function to apply betamax magic to the requests # session object. if not session_obj: session_obj = requests.Session() # Use TCPKeepAliveAdapter to fix bug 1323862 for scheme in list(session_obj.adapters.keys()): session_obj.mount(scheme, session.TCPKeepAliveAdapter()) with betamax.Betamax.configure() as config: config.before_record(callback=fixture.pre_record_hook) fixture.recorder = betamax.Betamax( session_obj, cassette_library_dir=fixture.cassette_library_dir) record = 'none' serializer = None if fixture.record in ['once', 'all', 'new_episodes']: record = fixture.record serializer = fixture.serializer_name fixture.recorder.use_cassette(fixture.cassette_name, serialize_with=serializer, record=record, **fixture.use_cassette_kwargs) fixture.recorder.start() fixture.addCleanup(fixture.recorder.stop) return session_obj
def setup_openstacksdk(): auth_plugin = utils.get_auth_plugin('neutron') session = utils.get_keystone_session('neutron', auth_plugin) # NOTE(mdulko): To get rid of warnings about connection pool being full # we need to "tweak" the keystoneauth's adapters increasing # the maximum pool size. for scheme in list(session.session.adapters): session.session.mount(scheme, k_session.TCPKeepAliveAdapter(pool_maxsize=1000)) # TODO(mdulko): To use Neutron's ability to do compare-and-swap updates we # need to manually add support for inserting If-Match header # into requests. At the moment we only need it for ports. # Remove when lower-constraints openstacksdk supports this. os_port.Port.if_match = os_resource.Header('If-Match') # TODO(maysams): We need to manually insert allowed_cidrs option # as it's only supported from 0.41.0 version. Remove it once # lower-constraints supports it. os_listener.Listener.allowed_cidrs = os_resource.Body('allowed_cidrs', type=list) conn = connection.Connection(session=session, region_name=getattr(config.CONF.neutron, 'region_name', None)) conn.network.create_ports = partial(_create_ports, conn.network) conn.network.add_trunk_subports = partial(_add_trunk_subports, conn.network) conn.network.delete_trunk_subports = partial(_delete_trunk_subports, conn.network) _clients[_OPENSTACKSDK] = conn
def test_init_poolmanager_with_requests_lesser_than_2_4_1(self): self.patch(client_session, 'REQUESTS_VERSION', (2, 4, 0)) given_adapter = client_session.TCPKeepAliveAdapter() # when pool manager is initialized given_adapter.init_poolmanager(1, 2, 3) # then no socket_options are given self.init_poolmanager.assert_called_once_with(1, 2, 3)
def get_gnocchiclient(conf): requests_session = requests.session() for scheme in requests_session.adapters.keys(): requests_session.mount(scheme, ka_session.TCPKeepAliveAdapter( pool_block=True)) session = keystone_client.get_session(requests_session=requests_session) return client.Client('1', session, interface=conf.service_credentials.interface, region_name=conf.service_credentials.region_name, endpoint_override=conf.dispatcher_gnocchi.url)
def test_init_poolmanager_with_given_optionsl(self): self.patch(client_session, 'REQUESTS_VERSION', (2, 4, 1)) given_adapter = client_session.TCPKeepAliveAdapter() given_options = object() # when pool manager is initialized given_adapter.init_poolmanager(1, 2, 3, socket_options=given_options) # then socket_options are given self.init_poolmanager.assert_called_once_with( 1, 2, 3, socket_options=given_options)
def test_init_poolmanager_with_basic_options(self): self.patch(client_session, 'REQUESTS_VERSION', (2, 4, 1)) socket = self.patch_socket_with_options( ['IPPROTO_TCP', 'TCP_NODELAY', 'SOL_SOCKET', 'SO_KEEPALIVE']) given_adapter = client_session.TCPKeepAliveAdapter() # when pool manager is initialized given_adapter.init_poolmanager(1, 2, 3) # then no socket_options are given self.init_poolmanager.assert_called_once_with( 1, 2, 3, socket_options=[ (socket.IPPROTO_TCP, socket.TCP_NODELAY, 1), (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)])
def test_init_poolmanager_with_tcp_keepcnt_on_windows(self): self.patch(client_session, 'REQUESTS_VERSION', (2, 4, 1)) self.patch(client_session.utils, 'is_windows_linux_subsystem', True) socket = self.patch_socket_with_options( ['IPPROTO_TCP', 'TCP_NODELAY', 'SOL_SOCKET', 'SO_KEEPALIVE', 'TCP_KEEPCNT']) given_adapter = client_session.TCPKeepAliveAdapter() # when pool manager is initialized given_adapter.init_poolmanager(1, 2, 3) # then socket_options are given self.init_poolmanager.assert_called_once_with( 1, 2, 3, socket_options=[ (socket.IPPROTO_TCP, socket.TCP_NODELAY, 1), (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1)])
def get(self, url): """Store and reuse HTTP adapters per Service URL.""" if url not in self._adapters: self._adapters[url] = session.TCPKeepAliveAdapter() return self._adapters[url]