-
Notifications
You must be signed in to change notification settings - Fork 339
fix: Setting a default timeout on all HTTP connections #397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -775,7 +775,7 @@ def __init__(self, app): | |
self._auth_override = json.dumps(auth_override, separators=(',', ':')) | ||
else: | ||
self._auth_override = None | ||
self._timeout = app.options.get('httpTimeout') | ||
self._timeout = app.options.get('httpTimeout', _http_client.DEFAULT_TIMEOUT_SECONDS) | ||
self._clients = {} | ||
|
||
emulator_host = os.environ.get(_EMULATOR_HOST_ENV_VAR) | ||
|
@@ -900,14 +900,13 @@ def __init__(self, credential, base_url, timeout, params=None): | |
credential: A Google credential that can be used to authenticate requests. | ||
base_url: A URL prefix to be added to all outgoing requests. This is typically the | ||
Firebase Realtime Database URL. | ||
timeout: HTTP request timeout in seconds. If not set connections will never | ||
timeout: HTTP request timeout in seconds. If set to None connections will never | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: the 'which is the default behaviour ...' part is no longer true. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That is still true. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops; yeah I missed the change from 'if unset' to 'if set to none' (despite suggesting exactly that earlier. 😳) |
||
timeout, which is the default behavior of the underlying requests library. | ||
params: Dict of query parameters to add to all outgoing requests. | ||
""" | ||
_http_client.JsonHttpClient.__init__( | ||
self, credential=credential, base_url=base_url, headers={'User-Agent': _USER_AGENT}) | ||
self.credential = credential | ||
self.timeout = timeout | ||
super().__init__( | ||
credential=credential, base_url=base_url, | ||
timeout=timeout, headers={'User-Agent': _USER_AGENT}) | ||
self.params = params if params else {} | ||
|
||
def request(self, method, url, **kwargs): | ||
|
@@ -937,8 +936,6 @@ def request(self, method, url, **kwargs): | |
query = extra_params | ||
kwargs['params'] = query | ||
|
||
if self.timeout: | ||
kwargs['timeout'] = self.timeout | ||
try: | ||
return super(_Client, self).request(method, url, **kwargs) | ||
except requests.exceptions.RequestException as error: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,6 +74,28 @@ def test_credential(): | |
assert recorder[0].url == _TEST_URL | ||
assert recorder[0].headers['Authorization'] == 'Bearer mock-token' | ||
|
||
def test_default_timeout(): | ||
client = _http_client.HttpClient() | ||
assert client.timeout == _http_client.DEFAULT_TIMEOUT_SECONDS | ||
recorder = _instrument(client, 'body') | ||
client.request('get', _TEST_URL) | ||
assert len(recorder) == 1 | ||
assert recorder[0]._extra_kwargs['timeout'] == pytest.approx( | ||
_http_client.DEFAULT_TIMEOUT_SECONDS, 0.001) | ||
|
||
@pytest.mark.parametrize('timeout', [7, 0, None]) | ||
def test_custom_timeout(timeout): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function looks mostly identical to test_default_timeout. You could optionally refactor to something like this: def _helper_test_timeout(**kwargs):
client = _http_client.HttpClient()
timeout = kwargs.get('timeout', _http_client.DEFAULT_TIMEOUT_SECONDS)
assert client.timeout == timeout
...
if timeout is None
assert recorder[0]._extra_kwargs['timeout'] is None
else:
assert ... == pytest.approx(timeout, 0.001)
def test_default_timeout():
_helper_test_timeout()
@parameterize(...)
def test_custom_timeout(timeout)
_helper_test_timeout(timeout=timeout) (Or you could parameterize the helper directly.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactored with parameterization. |
||
client = _http_client.HttpClient(timeout=timeout) | ||
assert client.timeout == timeout | ||
recorder = _instrument(client, 'body') | ||
client.request('get', _TEST_URL) | ||
assert len(recorder) == 1 | ||
if timeout is None: | ||
assert recorder[0]._extra_kwargs['timeout'] is None | ||
else: | ||
assert recorder[0]._extra_kwargs['timeout'] == pytest.approx(timeout, 0.001) | ||
|
||
|
||
def _instrument(client, payload, status=200): | ||
recorder = [] | ||
adapter = testutils.MockAdapter(payload, status, recorder) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a behavioural change. If you want to keep the behaviour the same, we could instead explicitly set a None timeout and defer using the new default of 120s until the next major release of firebase-admin-python. (Although defaulting to an indefinite timeout does seem a little broken, so I'd be happy to call this a bug fix instead.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
google-auth
shipped the same change as minor version bump. Given our next version is going to be a major version bump this should be ok. And yes, I also prefer to consider this a bug fix than anything else.