[Python] Add configuration.{connection_pool_maxsize, assert_hostname} (#6508)

* Backport kubernetes client features:

- assert_hostname
- connection_pool_maxsize
- cleanups

* Update petstore sample
This commit is contained in:
Mehdy Bohlool
2017-09-18 10:25:33 -07:00
committed by wing328
parent d928617b69
commit f3c41e866c
292 changed files with 29674 additions and 14 deletions

View File

@@ -283,7 +283,7 @@ class ApiClient(object):
_request_timeout=None):
"""
Makes the HTTP request (synchronous) and return the deserialized data.
To make an async request, define a function for callback.
To make an async request, set the async parameter.
:param resource_path: Path to method endpoint.
:param method: Method to call.
@@ -307,10 +307,10 @@ class ApiClient(object):
:param _request_timeout: timeout setting for this request. If one number provided, it will be total request
timeout. It can also be a pair (tuple) of (connection, read) timeouts.
:return:
If provide parameter callback,
If async parameter is True,
the request will be called asynchronously.
The method will return the request thread.
If parameter callback is None,
If parameter async is False or missing,
then the method will return the response directly.
"""
if not async:

View File

@@ -15,8 +15,9 @@ from __future__ import absolute_import
import urllib3
import sys
import logging
import multiprocessing
import sys
from six import iteritems
from six.moves import http_client as httplib
@@ -75,6 +76,16 @@ class Configuration(object):
self.cert_file = None
# client key file
self.key_file = None
# Set this to True/False to enable/disable SSL hostname verification.
self.assert_hostname = None
# urllib3 connection pool's maximum number of connections saved
# per pool. urllib3 uses 1 connection as default value, but this is
# not the best value when you are making a lot of possibly parallel
# requests to the same host, which is often the case here.
# cpu_count * 5 is used as default value to increase performance.
self.connection_pool_maxsize = multiprocessing.cpu_count() * 5
# Proxy URL
self.proxy = None

View File

@@ -56,7 +56,7 @@ class RESTResponse(io.IOBase):
class RESTClientObject(object):
def __init__(self, configuration, pools_size=4, maxsize=4):
def __init__(self, configuration, pools_size=4, maxsize=None):
# urllib3.PoolManager will pass all kw parameters to connectionpool
# https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/poolmanager.py#L75
# https://github.com/shazow/urllib3/blob/f9409436f83aeb79fbaf090181cd81b784f1b8ce/urllib3/connectionpool.py#L680
@@ -76,6 +76,16 @@ class RESTClientObject(object):
# if not set certificate file, use Mozilla's root certificates.
ca_certs = certifi.where()
addition_pool_args = {}
if configuration.assert_hostname is not None:
addition_pool_args['assert_hostname'] = config.assert_hostname
if maxsize is None:
if configuration.connection_pool_maxsize is not None:
maxsize = configuration.connection_pool_maxsize
else:
maxsize = 4
# https pool manager
if configuration.proxy:
self.pool_manager = urllib3.ProxyManager(
@@ -85,7 +95,8 @@ class RESTClientObject(object):
ca_certs=ca_certs,
cert_file=configuration.cert_file,
key_file=configuration.key_file,
proxy_url=configuration.proxy
proxy_url=configuration.proxy,
**addition_pool_args
)
else:
self.pool_manager = urllib3.PoolManager(
@@ -94,7 +105,8 @@ class RESTClientObject(object):
cert_reqs=cert_reqs,
ca_certs=ca_certs,
cert_file=configuration.cert_file,
key_file=configuration.key_file
key_file=configuration.key_file,
**addition_pool_args
)
def request(self, method, url, query_params=None, headers=None,