Skip to content

Commit

Permalink
fix: add ConnectionError to default retry (#571)
Browse files Browse the repository at this point in the history
  • Loading branch information
tswast authored Mar 23, 2021
1 parent 8f274e8 commit a3edb8b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
8 changes: 4 additions & 4 deletions google/cloud/bigquery/retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@

from google.api_core import exceptions
from google.api_core import retry
import requests.exceptions


_RETRYABLE_REASONS = frozenset(
["rateLimitExceeded", "backendError", "internalError", "badGateway"]
)

_UNSTRUCTURED_RETRYABLE_TYPES = (
ConnectionError,
exceptions.TooManyRequests,
exceptions.InternalServerError,
exceptions.BadGateway,
requests.exceptions.ConnectionError,
)


Expand All @@ -33,10 +36,7 @@ def _should_retry(exc):
We retry if and only if the 'reason' is 'backendError'
or 'rateLimitExceeded'.
"""
if not hasattr(exc, "errors"):
return False

if len(exc.errors) == 0:
if not hasattr(exc, "errors") or len(exc.errors) == 0:
# Check for unstructured error returns, e.g. from GFE
return isinstance(exc, _UNSTRUCTURED_RETRYABLE_TYPES)

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"google-resumable-media >= 0.6.0, < 2.0dev",
"packaging >= 14.3",
"protobuf >= 3.12.0",
"requests >= 2.18.0, < 3.0.0dev",
]
extras = {
"bqstorage": [
Expand Down
1 change: 1 addition & 0 deletions testing/constraints-3.6.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ pandas==0.23.0
proto-plus==1.10.0
protobuf==3.12.0
pyarrow==1.0.0
requests==2.18.0
six==1.13.0
tqdm==4.7.4
9 changes: 9 additions & 0 deletions tests/unit/test_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import unittest

import mock
import requests.exceptions


class Test_should_retry(unittest.TestCase):
Expand Down Expand Up @@ -42,6 +43,14 @@ def test_w_rateLimitExceeded(self):
exc = mock.Mock(errors=[{"reason": "rateLimitExceeded"}], spec=["errors"])
self.assertTrue(self._call_fut(exc))

def test_w_unstructured_connectionerror(self):
exc = ConnectionError()
self.assertTrue(self._call_fut(exc))

def test_w_unstructured_requests_connectionerror(self):
exc = requests.exceptions.ConnectionError()
self.assertTrue(self._call_fut(exc))

def test_w_unstructured_too_many_requests(self):
from google.api_core.exceptions import TooManyRequests

Expand Down

0 comments on commit a3edb8b

Please sign in to comment.