Skip to content

Commit f1ffbfd

Browse files
authored
Merge pull request #2240 from dhermes/remove-some-streaming-yagni
Remove some unused features of gcloud.streaming.
2 parents 192023d + 5a742eb commit f1ffbfd

File tree

5 files changed

+48
-207
lines changed

5 files changed

+48
-207
lines changed

gcloud/streaming/http_wrapper.py

Lines changed: 8 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from gcloud.streaming.util import calculate_wait_for_retry
3636

3737

38+
_REDIRECTIONS = 5
3839
# 308 and 429 don't have names in httplib.
3940
RESUME_INCOMPLETE = 308
4041
TOO_MANY_REQUESTS = 429
@@ -62,27 +63,6 @@
6263
)
6364

6465

65-
class _ExceptionRetryArgs(
66-
collections.namedtuple(
67-
'_ExceptionRetryArgs',
68-
['http', 'http_request', 'exc', 'num_retries', 'max_retry_wait'])):
69-
"""Bundle of information for retriable exceptions.
70-
71-
:type http: :class:`httplib2.Http` (or conforming alternative)
72-
:param http: instance used to perform requests.
73-
74-
:type http_request: :class:`Request`
75-
:param http_request: the request whose response was a retriable error.
76-
77-
:type exc: :class:`Exception` subclass
78-
:param exc: the exception being raised.
79-
80-
:type num_retries: integer
81-
:param num_retries: Number of retries consumed; used for exponential
82-
backoff.
83-
"""
84-
85-
8666
@contextlib.contextmanager
8767
def _httplib2_debug_level(http_request, level, http=None):
8868
"""Temporarily change the value of httplib2.debuglevel, if necessary.
@@ -328,8 +308,7 @@ def _reset_http_connections(http):
328308
del http.connections[conn_key]
329309

330310

331-
def _make_api_request_no_retry(http, http_request, redirections=5,
332-
check_response_func=_check_response):
311+
def _make_api_request_no_retry(http, http_request, redirections=_REDIRECTIONS):
333312
"""Send an HTTP request via the given http instance.
334313
335314
This wrapper exists to handle translation between the plain httplib2
@@ -344,9 +323,6 @@ def _make_api_request_no_retry(http, http_request, redirections=5,
344323
:type redirections: integer
345324
:param redirections: Number of redirects to follow.
346325
347-
:type check_response_func: function taking (response, content, url).
348-
:param check_response_func: Function to validate the HTTP response.
349-
350326
:rtype: :class:`Response`
351327
:returns: an object representing the server's response
352328
@@ -374,16 +350,12 @@ def _make_api_request_no_retry(http, http_request, redirections=5,
374350
raise RequestError()
375351

376352
response = Response(info, content, http_request.url)
377-
check_response_func(response)
353+
_check_response(response)
378354
return response
379355

380356

381-
def make_api_request(http, http_request,
382-
retries=7,
383-
max_retry_wait=60,
384-
redirections=5,
385-
check_response_func=_check_response,
386-
wo_retry_func=_make_api_request_no_retry):
357+
def make_api_request(http, http_request, retries=7,
358+
redirections=_REDIRECTIONS):
387359
"""Send an HTTP request via the given http, performing error/retry handling.
388360
389361
:type http: :class:`httplib2.Http`
@@ -396,19 +368,9 @@ def make_api_request(http, http_request,
396368
:param retries: Number of retries to attempt on retryable
397369
responses (such as 429 or 5XX).
398370
399-
:type max_retry_wait: integer
400-
:param max_retry_wait: Maximum number of seconds to wait when retrying.
401-
402371
:type redirections: integer
403372
:param redirections: Number of redirects to follow.
404373
405-
:type check_response_func: function taking (response, content, url).
406-
:param check_response_func: Function to validate the HTTP response.
407-
408-
:type wo_retry_func: function taking
409-
(http, request, redirections, check_response_func)
410-
:param wo_retry_func: Function to make HTTP request without retries.
411-
412374
:rtype: :class:`Response`
413375
:returns: an object representing the server's response.
414376
@@ -418,48 +380,17 @@ def make_api_request(http, http_request,
418380
retry = 0
419381
while True:
420382
try:
421-
return wo_retry_func(
422-
http, http_request, redirections=redirections,
423-
check_response_func=check_response_func)
383+
return _make_api_request_no_retry(http, http_request,
384+
redirections=redirections)
424385
except _RETRYABLE_EXCEPTIONS as exc:
425386
retry += 1
426387
if retry >= retries:
427388
raise
428389
retry_after = getattr(exc, 'retry_after', None)
429390
if retry_after is None:
430-
retry_after = calculate_wait_for_retry(retry, max_retry_wait)
391+
retry_after = calculate_wait_for_retry(retry)
431392

432393
_reset_http_connections(http)
433394
logging.debug('Retrying request to url %s after exception %s',
434395
http_request.url, type(exc).__name__)
435396
time.sleep(retry_after)
436-
437-
438-
_HTTP_FACTORIES = []
439-
440-
441-
def _register_http_factory(factory):
442-
"""Register a custom HTTP factory.
443-
444-
:type factory: callable taking keyword arguments, returning an Http
445-
instance (or an instance implementing the same API).
446-
:param factory: the new factory (it may return ``None`` to defer to
447-
a later factory or the default).
448-
"""
449-
_HTTP_FACTORIES.append(factory)
450-
451-
452-
def get_http(**kwds):
453-
"""Construct an Http instance.
454-
455-
:type kwds: dict
456-
:param kwds: keyword arguments to pass to factories.
457-
458-
:rtype: :class:`httplib2.Http` (or a workalike)
459-
:returns: The HTTP object created.
460-
"""
461-
for factory in _HTTP_FACTORIES:
462-
http = factory(**kwds)
463-
if http is not None:
464-
return http
465-
return httplib2.Http(**kwds)

gcloud/streaming/transfer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import mimetypes
2323
import os
2424

25+
import httplib2
2526
import six
2627
from six.moves import http_client
2728

@@ -31,7 +32,6 @@
3132
from gcloud.streaming.exceptions import HttpError
3233
from gcloud.streaming.exceptions import TransferInvalidError
3334
from gcloud.streaming.exceptions import TransferRetryError
34-
from gcloud.streaming.http_wrapper import get_http
3535
from gcloud.streaming.http_wrapper import make_api_request
3636
from gcloud.streaming.http_wrapper import Request
3737
from gcloud.streaming.http_wrapper import RESUME_INCOMPLETE
@@ -184,7 +184,7 @@ def _initialize(self, http, url):
184184
"""
185185
self._ensure_uninitialized()
186186
if self.http is None:
187-
self._http = http or get_http()
187+
self._http = http or httplib2.Http()
188188
self._url = url
189189

190190
@property

gcloud/streaming/util.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
import random
1818

1919

20-
def calculate_wait_for_retry(retry_attempt, max_wait=60):
20+
_MAX_RETRY_WAIT = 60
21+
22+
23+
def calculate_wait_for_retry(retry_attempt):
2124
"""Calculate the amount of time to wait before a retry attempt.
2225
2326
Wait time grows exponentially with the number of attempts. A
@@ -27,17 +30,13 @@ def calculate_wait_for_retry(retry_attempt, max_wait=60):
2730
:type retry_attempt: integer
2831
:param retry_attempt: Retry attempt counter.
2932
30-
:type max_wait: integer
31-
:param max_wait: Upper bound for wait time [seconds].
32-
3333
:rtype: integer
3434
:returns: Number of seconds to wait before retrying request.
3535
"""
36-
3736
wait_time = 2 ** retry_attempt
3837
max_jitter = wait_time / 4.0
3938
wait_time += random.uniform(-max_jitter, max_jitter)
40-
return max(1, min(wait_time, max_wait))
39+
return max(1, min(wait_time, _MAX_RETRY_WAIT))
4140

4241

4342
def acceptable_mime_type(accept_patterns, mime_type):

0 commit comments

Comments
 (0)