Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

PYTHON-5504 Prototype exponential backoff in with_transaction #2492

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

Merged
ShaneHarvey merged 2 commits into mongodb:backpressure from ShaneHarvey:PYTHON-5504
Aug 19, 2025
Merged
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
10 changes: 10 additions & 0 deletions pymongo/asynchronous/client_session.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@

from __future__ import annotations

import asyncio
import collections
import random
import time
import uuid
from collections.abc import Mapping as _Mapping
Expand Down Expand Up @@ -471,6 +473,8 @@ def _max_time_expired_error(exc: PyMongoError) -> bool:
# This limit is non-configurable and was chosen to be twice the 60 second
# default value of MongoDB's `transactionLifetimeLimitSeconds` parameter.
_WITH_TRANSACTION_RETRY_TIME_LIMIT = 120
_BACKOFF_MAX = 1
_BACKOFF_INITIAL = 0.050 # 50ms initial backoff


def _within_time_limit(start_time: float) -> bool:
Expand Down Expand Up @@ -700,7 +704,13 @@ async def callback(session, custom_arg, custom_kwarg=None):
https://github.com/mongodb/specifications/blob/master/source/transactions-convenient-api/transactions-convenient-api.md#handling-errors-inside-the-callback
"""
start_time = time.monotonic()
retry = 0
while True:
if retry: # Implement exponential backoff on retry.
jitter = random.random() # noqa: S311
backoff = jitter * min(_BACKOFF_INITIAL * (2**retry), _BACKOFF_MAX)
await asyncio.sleep(backoff)
retry += 1
await self.start_transaction(
read_concern, write_concern, read_preference, max_commit_time_ms
)
Expand Down
9 changes: 9 additions & 0 deletions pymongo/synchronous/client_session.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
from __future__ import annotations

import collections
import random
import time
import uuid
from collections.abc import Mapping as _Mapping
Expand Down Expand Up @@ -470,6 +471,8 @@ def _max_time_expired_error(exc: PyMongoError) -> bool:
# This limit is non-configurable and was chosen to be twice the 60 second
# default value of MongoDB's `transactionLifetimeLimitSeconds` parameter.
_WITH_TRANSACTION_RETRY_TIME_LIMIT = 120
_BACKOFF_MAX = 1
_BACKOFF_INITIAL = 0.050 # 50ms initial backoff


def _within_time_limit(start_time: float) -> bool:
Expand Down Expand Up @@ -699,7 +702,13 @@ def callback(session, custom_arg, custom_kwarg=None):
https://github.com/mongodb/specifications/blob/master/source/transactions-convenient-api/transactions-convenient-api.md#handling-errors-inside-the-callback
"""
start_time = time.monotonic()
retry = 0
while True:
if retry: # Implement exponential backoff on retry.
jitter = random.random() # noqa: S311
backoff = jitter * min(_BACKOFF_INITIAL * (2**retry), _BACKOFF_MAX)
time.sleep(backoff)
retry += 1
self.start_transaction(read_concern, write_concern, read_preference, max_commit_time_ms)
try:
ret = callback(self)
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /