Skip to content

Commit 5fdb15d

Browse files
authored
fix: revert "feat: add timeout to AuthorizedSession.request() (#397)" (#401)
This reverts commit 381dd400911d29926ffbf04e0f2ba53ef7bb997e.
1 parent 731ad66 commit 5fdb15d

File tree

3 files changed

+12
-189
lines changed

3 files changed

+12
-189
lines changed

packages/google-auth/google/auth/transport/requests.py

Lines changed: 11 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import functools
2020
import logging
21-
import time
2221

2322
try:
2423
import requests
@@ -65,33 +64,6 @@ def data(self):
6564
return self._response.content
6665

6766

68-
class TimeoutGuard(object):
69-
"""A context manager raising an error if the suite execution took too long.
70-
"""
71-
72-
def __init__(self, timeout, timeout_error_type=requests.exceptions.Timeout):
73-
self._timeout = timeout
74-
self.remaining_timeout = timeout
75-
self._timeout_error_type = timeout_error_type
76-
77-
def __enter__(self):
78-
self._start = time.time()
79-
return self
80-
81-
def __exit__(self, exc_type, exc_value, traceback):
82-
if exc_value:
83-
return # let the error bubble up automatically
84-
85-
if self._timeout is None:
86-
return # nothing to do, the timeout was not specified
87-
88-
elapsed = time.time() - self._start
89-
self.remaining_timeout = self._timeout - elapsed
90-
91-
if self.remaining_timeout <= 0:
92-
raise self._timeout_error_type()
93-
94-
9567
class Request(transport.Request):
9668
"""Requests request adapter.
9769
@@ -221,12 +193,8 @@ def __init__(
221193
# credentials.refresh).
222194
self._auth_request = auth_request
223195

224-
def request(self, method, url, data=None, headers=None, timeout=None, **kwargs):
225-
"""Implementation of Requests' request.
226-
227-
The ``timeout`` argument is interpreted as the approximate total time
228-
of **all** requests that are made under the hood.
229-
"""
196+
def request(self, method, url, data=None, headers=None, **kwargs):
197+
"""Implementation of Requests' request."""
230198
# pylint: disable=arguments-differ
231199
# Requests has a ton of arguments to request, but only two
232200
# (method, url) are required. We pass through all of the other
@@ -240,28 +208,13 @@ def request(self, method, url, data=None, headers=None, timeout=None, **kwargs):
240208
# and we want to pass the original headers if we recurse.
241209
request_headers = headers.copy() if headers is not None else {}
242210

243-
# Do not apply the timeout unconditionally in order to not override the
244-
# _auth_request's default timeout.
245-
auth_request = (
246-
self._auth_request
247-
if timeout is None
248-
else functools.partial(self._auth_request, timeout=timeout)
211+
self.credentials.before_request(
212+
self._auth_request, method, url, request_headers
249213
)
250214

251-
with TimeoutGuard(timeout) as guard:
252-
self.credentials.before_request(auth_request, method, url, request_headers)
253-
timeout = guard.remaining_timeout
254-
255-
with TimeoutGuard(timeout) as guard:
256-
response = super(AuthorizedSession, self).request(
257-
method,
258-
url,
259-
data=data,
260-
headers=request_headers,
261-
timeout=timeout,
262-
**kwargs
263-
)
264-
timeout = guard.remaining_timeout
215+
response = super(AuthorizedSession, self).request(
216+
method, url, data=data, headers=request_headers, **kwargs
217+
)
265218

266219
# If the response indicated that the credentials needed to be
267220
# refreshed, then refresh the credentials and re-attempt the
@@ -280,33 +233,17 @@ def request(self, method, url, data=None, headers=None, timeout=None, **kwargs):
280233
self._max_refresh_attempts,
281234
)
282235

283-
if self._refresh_timeout is not None:
284-
timeout = (
285-
self._refresh_timeout
286-
if timeout is None
287-
else min(timeout, self._refresh_timeout)
288-
)
289-
290-
# Do not apply the timeout unconditionally in order to not override the
291-
# _auth_request's default timeout.
292-
auth_request = (
293-
self._auth_request
294-
if timeout is None
295-
else functools.partial(self._auth_request, timeout=timeout)
236+
auth_request_with_timeout = functools.partial(
237+
self._auth_request, timeout=self._refresh_timeout
296238
)
239+
self.credentials.refresh(auth_request_with_timeout)
297240

298-
with TimeoutGuard(timeout) as guard:
299-
self.credentials.refresh(auth_request)
300-
timeout = guard.remaining_timeout
301-
302-
# Recurse. Pass in the original headers, not our modified set, but
303-
# do pass the adjusted timeout (i.e. the remaining time).
241+
# Recurse. Pass in the original headers, not our modified set.
304242
return self.request(
305243
method,
306244
url,
307245
data=data,
308246
headers=headers,
309-
timeout=timeout,
310247
_credential_refresh_attempt=_credential_refresh_attempt + 1,
311248
**kwargs
312249
)

packages/google-auth/noxfile.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
TEST_DEPENDENCIES = [
1818
"flask",
19-
"freezegun",
2019
"mock",
2120
"oauth2client",
2221
"pytest",

packages/google-auth/tests/transport/test_requests.py

Lines changed: 1 addition & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import datetime
16-
import functools
17-
18-
import freezegun
1915
import mock
20-
import pytest
2116
import requests
2217
import requests.adapters
2318
from six.moves import http_client
@@ -27,12 +22,6 @@
2722
from tests.transport import compliance
2823

2924

30-
@pytest.fixture
31-
def frozen_time():
32-
with freezegun.freeze_time("1970-01-01 00:00:00", tick=False) as frozen:
33-
yield frozen
34-
35-
3625
class TestRequestResponse(compliance.RequestResponseTests):
3726
def make_request(self):
3827
return google.auth.transport.requests.Request()
@@ -45,41 +34,6 @@ def test_timeout(self):
4534
assert http.request.call_args[1]["timeout"] == 5
4635

4736

48-
class TestTimeoutGuard(object):
49-
def make_guard(self, *args, **kwargs):
50-
return google.auth.transport.requests.TimeoutGuard(*args, **kwargs)
51-
52-
def test_tracks_elapsed_time(self, frozen_time):
53-
with self.make_guard(timeout=10) as guard:
54-
frozen_time.tick(delta=3.8)
55-
assert guard.remaining_timeout == 6.2
56-
57-
def test_noop_if_no_timeout(self, frozen_time):
58-
with self.make_guard(timeout=None) as guard:
59-
frozen_time.tick(delta=datetime.timedelta(days=3650))
60-
# NOTE: no timeout error raised, despite years have passed
61-
assert guard.remaining_timeout is None
62-
63-
def test_error_on_timeout(self, frozen_time):
64-
with pytest.raises(requests.exceptions.Timeout):
65-
with self.make_guard(timeout=10) as guard:
66-
frozen_time.tick(delta=10.001)
67-
assert guard.remaining_timeout == pytest.approx(-0.001)
68-
69-
def test_custom_timeout_error_type(self, frozen_time):
70-
class FooError(Exception):
71-
pass
72-
73-
with pytest.raises(FooError):
74-
with self.make_guard(timeout=1, timeout_error_type=FooError):
75-
frozen_time.tick(2)
76-
77-
def test_lets_errors_bubble_up(self, frozen_time):
78-
with pytest.raises(IndexError):
79-
with self.make_guard(timeout=1):
80-
[1, 2, 3][3]
81-
82-
8337
class CredentialsStub(google.auth.credentials.Credentials):
8438
def __init__(self, token="token"):
8539
super(CredentialsStub, self).__init__()
@@ -95,18 +49,6 @@ def refresh(self, request):
9549
self.token += "1"
9650

9751

98-
class TimeTickCredentialsStub(CredentialsStub):
99-
"""Credentials that spend some (mocked) time when refreshing a token."""
100-
101-
def __init__(self, time_tick, token="token"):
102-
self._time_tick = time_tick
103-
super(TimeTickCredentialsStub, self).__init__(token=token)
104-
105-
def refresh(self, request):
106-
self._time_tick()
107-
super(TimeTickCredentialsStub, self).refresh(requests)
108-
109-
11052
class AdapterStub(requests.adapters.BaseAdapter):
11153
def __init__(self, responses, headers=None):
11254
super(AdapterStub, self).__init__()
@@ -127,18 +69,6 @@ def close(self): # pragma: NO COVER
12769
return
12870

12971

130-
class TimeTickAdapterStub(AdapterStub):
131-
"""Adapter that spends some (mocked) time when making a request."""
132-
133-
def __init__(self, time_tick, responses, headers=None):
134-
self._time_tick = time_tick
135-
super(TimeTickAdapterStub, self).__init__(responses, headers=headers)
136-
137-
def send(self, request, **kwargs):
138-
self._time_tick()
139-
return super(TimeTickAdapterStub, self).send(request, **kwargs)
140-
141-
14272
def make_response(status=http_client.OK, data=None):
14373
response = requests.Response()
14474
response.status_code = status
@@ -191,9 +121,7 @@ def test_request_refresh(self):
191121
[make_response(status=http_client.UNAUTHORIZED), final_response]
192122
)
193123

194-
authed_session = google.auth.transport.requests.AuthorizedSession(
195-
credentials, refresh_timeout=60
196-
)
124+
authed_session = google.auth.transport.requests.AuthorizedSession(credentials)
197125
authed_session.mount(self.TEST_URL, adapter)
198126

199127
result = authed_session.request("GET", self.TEST_URL)
@@ -208,44 +136,3 @@ def test_request_refresh(self):
208136

209137
assert adapter.requests[1].url == self.TEST_URL
210138
assert adapter.requests[1].headers["authorization"] == "token1"
211-
212-
def test_request_timout(self, frozen_time):
213-
tick_one_second = functools.partial(frozen_time.tick, delta=1.0)
214-
215-
credentials = mock.Mock(
216-
wraps=TimeTickCredentialsStub(time_tick=tick_one_second)
217-
)
218-
adapter = TimeTickAdapterStub(
219-
time_tick=tick_one_second,
220-
responses=[
221-
make_response(status=http_client.UNAUTHORIZED),
222-
make_response(status=http_client.OK),
223-
],
224-
)
225-
226-
authed_session = google.auth.transport.requests.AuthorizedSession(credentials)
227-
authed_session.mount(self.TEST_URL, adapter)
228-
229-
# Because at least two requests have to be made, and each takes one
230-
# second, the total timeout specified will be exceeded.
231-
with pytest.raises(requests.exceptions.Timeout):
232-
authed_session.request("GET", self.TEST_URL, timeout=1.9)
233-
234-
def test_request_timeout_w_refresh_timeout(self, frozen_time):
235-
credentials = mock.Mock(wraps=CredentialsStub())
236-
adapter = TimeTickAdapterStub(
237-
time_tick=functools.partial(frozen_time.tick, delta=1.0), # one second
238-
responses=[
239-
make_response(status=http_client.UNAUTHORIZED),
240-
make_response(status=http_client.OK),
241-
],
242-
)
243-
244-
authed_session = google.auth.transport.requests.AuthorizedSession(
245-
credentials, refresh_timeout=0.9
246-
)
247-
authed_session.mount(self.TEST_URL, adapter)
248-
249-
# The timeout is long, but the short refresh timeout will prevail.
250-
with pytest.raises(requests.exceptions.Timeout):
251-
authed_session.request("GET", self.TEST_URL, timeout=60)

0 commit comments

Comments
 (0)