diff options
author | Alex Shatov <alexs@att.com> | 2017-09-13 16:22:17 -0400 |
---|---|---|
committer | Alex Shatov <alexs@att.com> | 2017-09-13 16:22:17 -0400 |
commit | 541b5bcdd5add5aec1fa77ae2b6ea79fc8ad6e9d (patch) | |
tree | dddad223074c44f2c4cb87883d32141bc958c366 /policyhandler/policy_rest.py | |
parent | 42a989a6b9b6388947da6fc273728a24c19fa686 (diff) |
setting the pool-size in requests session
* to cache the connections - performance improvement
Change-Id: I5a9b3ee30d40dc8d1b8154d5345a00b716e07964
Issue-Id: DCAEGEN2-62
Signed-off-by: Alex Shatov <alexs@att.com>
Diffstat (limited to 'policyhandler/policy_rest.py')
-rw-r--r-- | policyhandler/policy_rest.py | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/policyhandler/policy_rest.py b/policyhandler/policy_rest.py index d49164c..bf8a31d 100644 --- a/policyhandler/policy_rest.py +++ b/policyhandler/policy_rest.py @@ -136,6 +136,8 @@ class PolicyRest(object): """ policy-engine """ _logger = logging.getLogger("policy_handler.policy_rest") _lazy_inited = False + + _requests_session = None _url = None _headers = None _target_entity = None @@ -151,7 +153,20 @@ class PolicyRest(object): if PolicyRest._lazy_inited: return PolicyRest._lazy_inited = True + config = Config.config[Config.FIELD_POLICY_ENGINE] + + pool_size = config.get("pool_connections", 20) + PolicyRest._requests_session = requests.Session() + PolicyRest._requests_session.mount( + 'https://', + requests.adapters.HTTPAdapter(pool_connections=pool_size, pool_maxsize=pool_size) + ) + PolicyRest._requests_session.mount( + 'http://', + requests.adapters.HTTPAdapter(pool_connections=pool_size, pool_maxsize=pool_size) + ) + PolicyRest._url = config["url"] + config["path_api"] PolicyRest._headers = config["headers"] PolicyRest._target_entity = config.get("target_entity", Config.FIELD_POLICY_ENGINE) @@ -186,7 +201,7 @@ class PolicyRest(object): PolicyRest._logger.info(log_line) res = None try: - res = requests.post(full_path, json=json_body, headers=headers) + res = PolicyRest._requests_session.post(full_path, json=json_body, headers=headers) except requests.exceptions.RequestException as ex: error_code = AuditHttpCode.SERVICE_UNAVAILABLE_ERROR.value error_msg = "failed to post to PDP {0} {1} msg={2} headers={3}" \ |