Skip to content

Commit ff98684

Browse files
committed
requests: use PYTHONBUGZILLA_REQUESTS_TIMEOUT env variable
Add a default timeout of 5 minutes, but let users override it with PYTHONBUGZILLA_REQUESTS_TIMEOUT environment variable Resolves: #135 Signed-off-by: Cole Robinson <crobinso@redhat.com>
1 parent e1bd6a7 commit ff98684

2 files changed

Lines changed: 14 additions & 10 deletions

File tree

bugzilla/_session.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from logging import getLogger
55

6+
import os
67
import requests
78

89
from ._compatimports import urlparse
@@ -43,6 +44,14 @@ def __init__(self, url, user_agent,
4344
self._session.params["Bugzilla_api_key"] = self._api_key
4445
self._set_tokencache_param()
4546

47+
def _get_timeout(self):
48+
# Default to 5 minutes. This is longer than bugzilla.redhat.com's
49+
# apparent 3 minute timeout so shouldn't affect legitimate usage,
50+
# but saves us from indefinite hangs
51+
DEFAULT_TIMEOUT = 300
52+
envtimeout = os.environ.get("PYTHONBUGZILLA_REQUESTS_TIMEOUT")
53+
return float(envtimeout or DEFAULT_TIMEOUT)
54+
4655
def get_user_agent(self):
4756
return self._user_agent
4857
def get_scheme(self):
@@ -77,4 +86,7 @@ def get_requests_session(self):
7786
return self._session
7887

7988
def request(self, *args, **kwargs):
89+
timeout = self._get_timeout()
90+
if "timeout" not in kwargs:
91+
kwargs["timeout"] = timeout
8092
return self._session.request(*args, **kwargs)

tests/utils.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,8 @@ def open_functional_bz(bzclass, url, kwargs):
4747
if kwargs.get("force_xmlrpc", False):
4848
assert bz.is_xmlrpc() is True
4949

50-
# Set a session timeout of 30 seconds
51-
session = bz.get_requests_session()
52-
origrequest = session.request
53-
54-
def fake_request(*args, **kwargs):
55-
if "timeout" not in kwargs:
56-
kwargs["timeout"] = 60
57-
return origrequest(*args, **kwargs)
58-
59-
session.request = fake_request
50+
# Set a request timeout of 60 seconds
51+
os.environ["PYTHONBUGZILLA_REQUESTS_TIMEOUT"] = "60"
6052
return bz
6153

6254

0 commit comments

Comments
 (0)