Skip to content

Commit

Permalink
Remove some unused features of gcloud.streaming.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Sep 2, 2016
1 parent 192023d commit 5a742eb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 207 deletions.
85 changes: 8 additions & 77 deletions gcloud/streaming/http_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from gcloud.streaming.util import calculate_wait_for_retry


_REDIRECTIONS = 5
# 308 and 429 don't have names in httplib.
RESUME_INCOMPLETE = 308
TOO_MANY_REQUESTS = 429
Expand Down Expand Up @@ -62,27 +63,6 @@
)


class _ExceptionRetryArgs(
collections.namedtuple(
'_ExceptionRetryArgs',
['http', 'http_request', 'exc', 'num_retries', 'max_retry_wait'])):
"""Bundle of information for retriable exceptions.
:type http: :class:`httplib2.Http` (or conforming alternative)
:param http: instance used to perform requests.
:type http_request: :class:`Request`
:param http_request: the request whose response was a retriable error.
:type exc: :class:`Exception` subclass
:param exc: the exception being raised.
:type num_retries: integer
:param num_retries: Number of retries consumed; used for exponential
backoff.
"""


@contextlib.contextmanager
def _httplib2_debug_level(http_request, level, http=None):
"""Temporarily change the value of httplib2.debuglevel, if necessary.
Expand Down Expand Up @@ -328,8 +308,7 @@ def _reset_http_connections(http):
del http.connections[conn_key]


def _make_api_request_no_retry(http, http_request, redirections=5,
check_response_func=_check_response):
def _make_api_request_no_retry(http, http_request, redirections=_REDIRECTIONS):
"""Send an HTTP request via the given http instance.
This wrapper exists to handle translation between the plain httplib2
Expand All @@ -344,9 +323,6 @@ def _make_api_request_no_retry(http, http_request, redirections=5,
:type redirections: integer
:param redirections: Number of redirects to follow.
:type check_response_func: function taking (response, content, url).
:param check_response_func: Function to validate the HTTP response.
:rtype: :class:`Response`
:returns: an object representing the server's response
Expand Down Expand Up @@ -374,16 +350,12 @@ def _make_api_request_no_retry(http, http_request, redirections=5,
raise RequestError()

response = Response(info, content, http_request.url)
check_response_func(response)
_check_response(response)
return response


def make_api_request(http, http_request,
retries=7,
max_retry_wait=60,
redirections=5,
check_response_func=_check_response,
wo_retry_func=_make_api_request_no_retry):
def make_api_request(http, http_request, retries=7,
redirections=_REDIRECTIONS):
"""Send an HTTP request via the given http, performing error/retry handling.
:type http: :class:`httplib2.Http`
Expand All @@ -396,19 +368,9 @@ def make_api_request(http, http_request,
:param retries: Number of retries to attempt on retryable
responses (such as 429 or 5XX).
:type max_retry_wait: integer
:param max_retry_wait: Maximum number of seconds to wait when retrying.
:type redirections: integer
:param redirections: Number of redirects to follow.
:type check_response_func: function taking (response, content, url).
:param check_response_func: Function to validate the HTTP response.
:type wo_retry_func: function taking
(http, request, redirections, check_response_func)
:param wo_retry_func: Function to make HTTP request without retries.
:rtype: :class:`Response`
:returns: an object representing the server's response.
Expand All @@ -418,48 +380,17 @@ def make_api_request(http, http_request,
retry = 0
while True:
try:
return wo_retry_func(
http, http_request, redirections=redirections,
check_response_func=check_response_func)
return _make_api_request_no_retry(http, http_request,
redirections=redirections)
except _RETRYABLE_EXCEPTIONS as exc:
retry += 1
if retry >= retries:
raise
retry_after = getattr(exc, 'retry_after', None)
if retry_after is None:
retry_after = calculate_wait_for_retry(retry, max_retry_wait)
retry_after = calculate_wait_for_retry(retry)

_reset_http_connections(http)
logging.debug('Retrying request to url %s after exception %s',
http_request.url, type(exc).__name__)
time.sleep(retry_after)


_HTTP_FACTORIES = []


def _register_http_factory(factory):
"""Register a custom HTTP factory.
:type factory: callable taking keyword arguments, returning an Http
instance (or an instance implementing the same API).
:param factory: the new factory (it may return ``None`` to defer to
a later factory or the default).
"""
_HTTP_FACTORIES.append(factory)


def get_http(**kwds):
"""Construct an Http instance.
:type kwds: dict
:param kwds: keyword arguments to pass to factories.
:rtype: :class:`httplib2.Http` (or a workalike)
:returns: The HTTP object created.
"""
for factory in _HTTP_FACTORIES:
http = factory(**kwds)
if http is not None:
return http
return httplib2.Http(**kwds)
4 changes: 2 additions & 2 deletions gcloud/streaming/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import mimetypes
import os

import httplib2
import six
from six.moves import http_client

Expand All @@ -31,7 +32,6 @@
from gcloud.streaming.exceptions import HttpError
from gcloud.streaming.exceptions import TransferInvalidError
from gcloud.streaming.exceptions import TransferRetryError
from gcloud.streaming.http_wrapper import get_http
from gcloud.streaming.http_wrapper import make_api_request
from gcloud.streaming.http_wrapper import Request
from gcloud.streaming.http_wrapper import RESUME_INCOMPLETE
Expand Down Expand Up @@ -184,7 +184,7 @@ def _initialize(self, http, url):
"""
self._ensure_uninitialized()
if self.http is None:
self._http = http or get_http()
self._http = http or httplib2.Http()
self._url = url

@property
Expand Down
11 changes: 5 additions & 6 deletions gcloud/streaming/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
import random


def calculate_wait_for_retry(retry_attempt, max_wait=60):
_MAX_RETRY_WAIT = 60


def calculate_wait_for_retry(retry_attempt):
"""Calculate the amount of time to wait before a retry attempt.
Wait time grows exponentially with the number of attempts. A
Expand All @@ -27,17 +30,13 @@ def calculate_wait_for_retry(retry_attempt, max_wait=60):
:type retry_attempt: integer
:param retry_attempt: Retry attempt counter.
:type max_wait: integer
:param max_wait: Upper bound for wait time [seconds].
:rtype: integer
:returns: Number of seconds to wait before retrying request.
"""

wait_time = 2 ** retry_attempt
max_jitter = wait_time / 4.0
wait_time += random.uniform(-max_jitter, max_jitter)
return max(1, min(wait_time, max_wait))
return max(1, min(wait_time, _MAX_RETRY_WAIT))


def acceptable_mime_type(accept_patterns, mime_type):
Expand Down
Loading

0 comments on commit 5a742eb

Please sign in to comment.