Skip to content

feat: add retry policy for connection #906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions google/cloud/bigquery_storage_v1/retry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import time

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


_CONNECTION_RETRIABLE_REASONS = (
EOFError,
exceptions.Aborted,
exceptions.Cancelled,
exceptions.DeadlineExceeded,
exceptions.ServiceUnavailable,
)

_DEFAULT_CONNECTION_RETRY_DEADLINE = 10 # 10 seconds
_DEFAULT_CONNECTION_RETRY_BACKOFF = 0.05 # 50 miliseconds


def _should_retry_connection(exc):
return isinstance(exc, _CONNECTION_RETRIABLE_REASONS)


_DEFAULT_CONNECTION_RETRY = retry.Retry(
predicate=_should_retry_connection,
initial=_DEFAULT_CONNECTION_RETRY_BACKOFF,
multiplier=2,
timeout=_DEFAULT_CONNECTION_RETRY_DEADLINE,
)

def _stateless_retry(func, func_args={}, num_retry=1, backoff=0.05):
for i in range(num_retry + 1):
try:
return func(**func_args)
except Exception:
time.sleep(backoff)
continue
raise exceptions.Unknown("Max retry for connection is reached.")
Loading
Loading