Skip to content

Commit

Permalink
fix: add ConnectionError to default retry (#445)
Browse files Browse the repository at this point in the history
This adds python built-in [ConnectionError](https://docs.python.org/3/library/exceptions.html#ConnectionError) to default retryable types. 
ConnectionError was recently added in the [BigQuery library](googleapis/python-bigquery#571) to allow retries. 

Fixes #426 🦕
  • Loading branch information
cojenco authored May 20, 2021
1 parent 179de2a commit 8344253
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
10 changes: 9 additions & 1 deletion google/cloud/storage/retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@
import json


_RETRYABLE_TYPES = (
# ConnectionError is a built-in exception only in Python3 and not in Python2.
try:
_RETRYABLE_STDLIB_TYPES = (ConnectionError,)
except NameError:
_RETRYABLE_STDLIB_TYPES = ()


_RETRYABLE_TYPES = _RETRYABLE_STDLIB_TYPES + (
api_exceptions.TooManyRequests, # 429
api_exceptions.InternalServerError, # 500
api_exceptions.BadGateway, # 502
Expand All @@ -30,6 +37,7 @@
requests.ConnectionError,
)


# Some retriable errors don't have their own custom exception in api_core.
_ADDITIONAL_RETRYABLE_STATUS_CODES = (408,)

Expand Down
21 changes: 21 additions & 0 deletions tests/unit/test_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
import mock


try:
ConnectionError
except NameError:
_HAS_STDLIB_CONNECTION_ERROR = False
else:
_HAS_STDLIB_CONNECTION_ERROR = True


class Test_should_retry(unittest.TestCase):
def _call_fut(self, exc):
from google.cloud.storage import retry
Expand Down Expand Up @@ -56,9 +64,22 @@ def test_w_google_api_call_error_miss(self):
self.assertFalse(self._call_fut(exc))

def test_w_requests_connection_error(self):
import requests

exc = requests.ConnectionError()
self.assertTrue(self._call_fut(exc))

def test_miss_w_stdlib_error(self):
exc = ValueError("testing")
self.assertFalse(self._call_fut(exc))

@unittest.skipUnless(
_HAS_STDLIB_CONNECTION_ERROR, "No builtin 'ConnectionError' in Python 2",
)
def test_w_stdlib_connection_error(self):
exc = ConnectionError()
self.assertTrue(self._call_fut(exc))


class TestConditionalRetryPolicy(unittest.TestCase):
def _make_one(self, retry_policy, conditional_predicate, required_kwargs):
Expand Down

0 comments on commit 8344253

Please sign in to comment.