Skip to content

Commit a3edb8b

Browse files
authored
fix: add ConnectionError to default retry (#571)
1 parent 8f274e8 commit a3edb8b

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

google/cloud/bigquery/retry.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@
1414

1515
from google.api_core import exceptions
1616
from google.api_core import retry
17+
import requests.exceptions
1718

1819

1920
_RETRYABLE_REASONS = frozenset(
2021
["rateLimitExceeded", "backendError", "internalError", "badGateway"]
2122
)
2223

2324
_UNSTRUCTURED_RETRYABLE_TYPES = (
25+
ConnectionError,
2426
exceptions.TooManyRequests,
2527
exceptions.InternalServerError,
2628
exceptions.BadGateway,
29+
requests.exceptions.ConnectionError,
2730
)
2831

2932

@@ -33,10 +36,7 @@ def _should_retry(exc):
3336
We retry if and only if the 'reason' is 'backendError'
3437
or 'rateLimitExceeded'.
3538
"""
36-
if not hasattr(exc, "errors"):
37-
return False
38-
39-
if len(exc.errors) == 0:
39+
if not hasattr(exc, "errors") or len(exc.errors) == 0:
4040
# Check for unstructured error returns, e.g. from GFE
4141
return isinstance(exc, _UNSTRUCTURED_RETRYABLE_TYPES)
4242

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"google-resumable-media >= 0.6.0, < 2.0dev",
3636
"packaging >= 14.3",
3737
"protobuf >= 3.12.0",
38+
"requests >= 2.18.0, < 3.0.0dev",
3839
]
3940
extras = {
4041
"bqstorage": [

testing/constraints-3.6.txt

+1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ pandas==0.23.0
1717
proto-plus==1.10.0
1818
protobuf==3.12.0
1919
pyarrow==1.0.0
20+
requests==2.18.0
2021
six==1.13.0
2122
tqdm==4.7.4

tests/unit/test_retry.py

+9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import unittest
1616

1717
import mock
18+
import requests.exceptions
1819

1920

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

46+
def test_w_unstructured_connectionerror(self):
47+
exc = ConnectionError()
48+
self.assertTrue(self._call_fut(exc))
49+
50+
def test_w_unstructured_requests_connectionerror(self):
51+
exc = requests.exceptions.ConnectionError()
52+
self.assertTrue(self._call_fut(exc))
53+
4554
def test_w_unstructured_too_many_requests(self):
4655
from google.api_core.exceptions import TooManyRequests
4756

0 commit comments

Comments
 (0)