Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/cloud/bigquery/retry.py: 64%

22 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-03-26 06:07 +0000

1# Copyright 2018 Google LLC 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14 

15from google.api_core import exceptions 

16from google.api_core import retry 

17from google.auth import exceptions as auth_exceptions # type: ignore 

18import requests.exceptions 

19 

20 

21_RETRYABLE_REASONS = frozenset( 

22 ["rateLimitExceeded", "backendError", "internalError", "badGateway"] 

23) 

24 

25_UNSTRUCTURED_RETRYABLE_TYPES = ( 

26 ConnectionError, 

27 exceptions.TooManyRequests, 

28 exceptions.InternalServerError, 

29 exceptions.BadGateway, 

30 requests.exceptions.ChunkedEncodingError, 

31 requests.exceptions.ConnectionError, 

32 requests.exceptions.Timeout, 

33 auth_exceptions.TransportError, 

34) 

35 

36_DEFAULT_JOB_DEADLINE = 60.0 * 10.0 # seconds 

37 

38 

39def _should_retry(exc): 

40 """Predicate for determining when to retry. 

41 

42 We retry if and only if the 'reason' is 'backendError' 

43 or 'rateLimitExceeded'. 

44 """ 

45 if not hasattr(exc, "errors") or len(exc.errors) == 0: 

46 # Check for unstructured error returns, e.g. from GFE 

47 return isinstance(exc, _UNSTRUCTURED_RETRYABLE_TYPES) 

48 

49 reason = exc.errors[0]["reason"] 

50 return reason in _RETRYABLE_REASONS 

51 

52 

53DEFAULT_RETRY = retry.Retry(predicate=_should_retry, deadline=600.0) 

54"""The default retry object. 

55 

56Any method with a ``retry`` parameter will be retried automatically, 

57with reasonable defaults. To disable retry, pass ``retry=None``. 

58To modify the default retry behavior, call a ``with_XXX`` method 

59on ``DEFAULT_RETRY``. For example, to change the deadline to 30 seconds, 

60pass ``retry=bigquery.DEFAULT_RETRY.with_deadline(30)``. 

61""" 

62 

63DEFAULT_TIMEOUT = None 

64"""The default API timeout. 

65 

66This is the time to wait per request. To adjust the total wait time, set a 

67deadline on the retry object. 

68""" 

69 

70job_retry_reasons = "rateLimitExceeded", "backendError" 

71 

72 

73def _job_should_retry(exc): 

74 if not hasattr(exc, "errors") or len(exc.errors) == 0: 

75 return False 

76 

77 reason = exc.errors[0]["reason"] 

78 return reason in job_retry_reasons 

79 

80 

81DEFAULT_JOB_RETRY = retry.Retry( 

82 predicate=_job_should_retry, deadline=_DEFAULT_JOB_DEADLINE 

83) 

84""" 

85The default job retry object. 

86"""