diff options
author | Ross Brattain <ross.b.brattain@intel.com> | 2017-09-21 03:44:28 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@opnfv.org> | 2017-09-21 03:44:28 +0000 |
commit | 36a2e04b468d980c98bd43768e4c88d37a8a69f1 (patch) | |
tree | 9ac2f2476328d10c05f328b544ff092266280767 | |
parent | 173c6ad6f464c514185efc0b5a608ceb6cc118ae (diff) | |
parent | c039fecf1c533b09286b0eec89ddc13862b3d452 (diff) |
Merge "Introduced timeout to post method of HttpClient"
-rw-r--r-- | api/resources/v1/env.py | 2 | ||||
-rw-r--r-- | yardstick/common/httpClient.py | 24 |
2 files changed, 15 insertions, 11 deletions
diff --git a/api/resources/v1/env.py b/api/resources/v1/env.py index 47ea91643..04cc659c7 100644 --- a/api/resources/v1/env.py +++ b/api/resources/v1/env.py @@ -123,7 +123,7 @@ class V1Env(ApiResource): "isDefault": True, } try: - HttpClient().post(url, data) + HttpClient().post(url, data, timeout=10) except Exception: LOG.exception('Create datasources failed') raise diff --git a/yardstick/common/httpClient.py b/yardstick/common/httpClient.py index 11c2d752d..54f7be670 100644 --- a/yardstick/common/httpClient.py +++ b/yardstick/common/httpClient.py @@ -9,6 +9,7 @@ from __future__ import absolute_import import logging +import time from oslo_serialization import jsonutils import requests @@ -18,18 +19,21 @@ logger = logging.getLogger(__name__) class HttpClient(object): - def post(self, url, data): + def post(self, url, data, timeout=0): data = jsonutils.dump_as_bytes(data) headers = {'Content-Type': 'application/json'} - try: - response = requests.post(url, data=data, headers=headers) - result = response.json() - logger.debug('The result is: %s', result) - - return result - except Exception as e: - logger.debug('Failed: %s', e) - raise + t_end = time.time() + timeout + while True: + try: + response = requests.post(url, data=data, headers=headers) + result = response.json() + logger.debug('The result is: %s', result) + return result + except Exception: + if time.time() > t_end: + logger.exception('') + raise + time.sleep(1) def get(self, url): response = requests.get(url) |