From 6f5450d70a691374369aa227036bf91c038f31b7 Mon Sep 17 00:00:00 2001 From: Alexander Mohr Date: Fri, 11 May 2018 13:10:30 -0700 Subject: [PATCH 1/3] fix leak --- datadog/api/http_client.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/datadog/api/http_client.py b/datadog/api/http_client.py index 079e20e53..5ba4e558f 100644 --- a/datadog/api/http_client.py +++ b/datadog/api/http_client.py @@ -94,6 +94,8 @@ def request(cls, method, url, headers, params, data, timeout, proxies, verify, m u"Datadog's usage. We recommand upgrading it ('pip install -U requests')." u"If you need help or have any question, please contact support@datadoghq.com" ) + finally: + s.close() return result From 7612f7b23611e9d6c4414f4088af9ba7b7fc26a2 Mon Sep 17 00:00:00 2001 From: Alexander Mohr Date: Thu, 31 May 2018 14:00:53 -0700 Subject: [PATCH 2/3] use context --- datadog/api/http_client.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/datadog/api/http_client.py b/datadog/api/http_client.py index 5ba4e558f..c72f586cf 100644 --- a/datadog/api/http_client.py +++ b/datadog/api/http_client.py @@ -12,6 +12,7 @@ # 3p try: import requests + import requests.adapters except ImportError: requests = None @@ -64,19 +65,20 @@ class RequestClient(HTTPClient): def request(cls, method, url, headers, params, data, timeout, proxies, verify, max_retries): """ """ - # Use a session to set a max_retries parameters - s = requests.Session() - http_adapter = requests.adapters.HTTPAdapter(max_retries=max_retries) - s.mount('https://', http_adapter) - try: - result = s.request( - method, url, - headers=headers, params=params, data=data, - timeout=timeout, - proxies=proxies, verify=verify) - - result.raise_for_status() + # Use a session to set a max_retries parameters + with requests.Session() as s: + http_adapter = requests.adapters.HTTPAdapter(max_retries=max_retries) + s.mount('https://', http_adapter) + + # Since stream=False we can close the session after this call + result = s.request( + method, url, + headers=headers, params=params, data=data, + timeout=timeout, + proxies=proxies, verify=verify) + + result.raise_for_status() except requests.ConnectionError as e: raise _remove_context(ClientError(method, url, e)) From 21514a90976d3022594a104b50e592c9fa56dbe2 Mon Sep 17 00:00:00 2001 From: Alexander Mohr Date: Thu, 31 May 2018 14:01:20 -0700 Subject: [PATCH 3/3] remove this --- datadog/api/http_client.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/datadog/api/http_client.py b/datadog/api/http_client.py index c72f586cf..2e9230e0e 100644 --- a/datadog/api/http_client.py +++ b/datadog/api/http_client.py @@ -96,8 +96,6 @@ def request(cls, method, url, headers, params, data, timeout, proxies, verify, m u"Datadog's usage. We recommand upgrading it ('pip install -U requests')." u"If you need help or have any question, please contact support@datadoghq.com" ) - finally: - s.close() return result