Skip to content

Commit 2453294

Browse files
committed
refactor(retry): migrate from backoff to tenacity
1 parent 25bd5ce commit 2453294

14 files changed

Lines changed: 225 additions & 138 deletions

File tree

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ repos:
9696
# `types-$lib` or `$lib-stubs`.
9797
additional_dependencies:
9898
- aiohttp==3.13.3
99-
- backoff==2.2.1
99+
- tenacity==9.1.2
100100
- cryptography==46.0.5
101101
- pyjwt==2.11.0
102102
- types-requests==2.32.4.20260107

auth/gcloud/aio/auth/token.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@
1717
from urllib.parse import urlencode
1818
from urllib.parse import urlparse
1919

20-
import backoff
2120
import cryptography # pylint: disable=unused-import
2221
import jwt
22+
from tenacity import retry
23+
from tenacity import retry_if_exception_type
24+
from tenacity import stop_after_attempt
25+
from tenacity import wait_random_exponential
2326

2427
from .build_constants import BUILD_GCLOUD_REST
2528
from .session import AioSession
@@ -263,7 +266,12 @@ async def ensure_token(self) -> None:
263266
async def refresh(self, *, timeout: int) -> TokenResponse:
264267
pass
265268

266-
@backoff.on_exception(backoff.expo, Exception, max_tries=5)
269+
@retry(
270+
retry=retry_if_exception_type(Exception),
271+
stop=stop_after_attempt(5),
272+
wait=wait_random_exponential(multiplier=1, max=60),
273+
reraise=True,
274+
)
267275
async def acquire_access_token(self, timeout: int = 10) -> None:
268276
resp = await self.refresh(timeout=timeout)
269277

auth/poetry.lock

Lines changed: 17 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

auth/pyproject.rest.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ classifiers = [
2222
[tool.poetry.dependencies]
2323
python = ">= 3.10, < 4.0"
2424
# aiohttp = ">= 3.9.2, < 4.0.0"
25-
backoff = ">= 1.0.0, < 3.0.0"
25+
tenacity = ">= 8.2.0, < 10.0.0"
2626
chardet = ">= 2.0, < 7.0"
2727
# See https://cryptography.io/en/latest/api-stability/#deprecation
2828
cryptography = ">= 2.0.0, < 49.0.0" # pin max to < (major + 3)

auth/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ classifiers = [
2222
[tool.poetry.dependencies]
2323
python = ">= 3.10, < 4.0"
2424
aiohttp = ">= 3.9.2, < 4.0.0"
25-
backoff = ">= 1.0.0, < 3.0.0"
25+
tenacity = ">= 8.2.0, < 10.0.0"
2626
chardet = ">= 2.0, < 7.0"
2727
# See https://cryptography.io/en/latest/api-stability/#deprecation
2828
cryptography = ">= 2.0.0, < 49.0.0" # pin max to < (major + 3)

bigquery/poetry.lock

Lines changed: 22 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bigquery/tests/integration/smoke_test.py

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import uuid
22

3-
import backoff
43
import pytest
54
from gcloud.aio.auth import BUILD_GCLOUD_REST # pylint: disable=no-name-in-module
65
from gcloud.aio.bigquery import SourceFormat
@@ -9,6 +8,12 @@
98
from gcloud.aio.datastore import Key # pylint: disable=no-name-in-module
109
from gcloud.aio.datastore import PathElement # pylint: disable=no-name-in-module
1110
from gcloud.aio.storage import Storage # pylint: disable=no-name-in-module
11+
from tenacity import retry
12+
from tenacity import retry_if_exception_type
13+
from tenacity import retry_if_result
14+
from tenacity import stop_after_attempt
15+
from tenacity import stop_after_delay
16+
from tenacity import wait_fixed
1217

1318
# Selectively load libraries based on the package
1419
if BUILD_GCLOUD_REST:
@@ -56,15 +61,22 @@ async def test_table_load_copy(
5661

5762
operation = await ds.export(export_bucket_name, kinds=[kind])
5863

59-
def is_processing(operation):
64+
def is_processing(operation) -> bool:
6065
return (
6166
operation
6267
and operation.metadata['common']['state'] == 'PROCESSING'
6368
)
6469

65-
operation = await backoff.on_predicate(
66-
backoff.constant, is_processing, interval=10, max_tries=10)(
67-
ds.get_datastore_operation)(operation.name)
70+
@retry(
71+
retry=retry_if_result(is_processing),
72+
wait=wait_fixed(10),
73+
stop=stop_after_attempt(10),
74+
reraise=True,
75+
)
76+
async def get_operation_with_retry(name: str):
77+
return await ds.get_datastore_operation(name)
78+
79+
operation = await get_operation_with_retry(operation.name)
6880

6981
assert operation.metadata['common']['state'] == 'SUCCESSFUL'
7082
# END: copy from `test_datastore_export`
@@ -87,19 +99,34 @@ def is_processing(operation):
8799
source_format=SourceFormat.DATASTORE_BACKUP,
88100
)
89101

90-
source_table = await backoff.on_exception(
91-
backoff.constant, Exception, max_time=60, jitter=None,
92-
interval=10)(t.get)()
102+
@retry(
103+
retry=retry_if_exception_type(Exception),
104+
wait=wait_fixed(10),
105+
stop=stop_after_delay(60),
106+
reraise=True,
107+
)
108+
async def get_source_table_with_retry():
109+
return await t.get()
110+
111+
source_table = await get_source_table_with_retry()
93112
assert int(source_table['numRows']) > 0
94113

95114
await t.insert_via_copy(project, dataset, copy_entity_table)
96115
t1 = Table(
97116
dataset, copy_entity_table, project=project,
98117
service_file=creds, session=s,
99118
)
100-
copy_table = await backoff.on_exception(
101-
backoff.constant, Exception, max_time=60, jitter=None,
102-
interval=10)(t1.get)()
119+
120+
@retry(
121+
retry=retry_if_exception_type(Exception),
122+
wait=wait_fixed(10),
123+
stop=stop_after_delay(60),
124+
reraise=True,
125+
)
126+
async def get_copy_table_with_retry():
127+
return await t1.get()
128+
129+
copy_table = await get_copy_table_with_retry()
103130
assert copy_table['numRows'] == source_table['numRows']
104131

105132
# delete the backup and copy table

datastore/poetry.lock

Lines changed: 22 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)