Skip to content

Commit f936b1b

Browse files
committed
update backoff logic and fix test
1 parent 36d4490 commit f936b1b

File tree

3 files changed

+15
-23
lines changed

3 files changed

+15
-23
lines changed

pymongo/asynchronous/pool.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,17 +1348,16 @@ async def _get_conn(
13481348
async with self._max_connecting_cond:
13491349
self._raise_if_not_ready(checkout_started_time, emit_event=False)
13501350
while not (self.conns or self._pending < self.max_connecting):
1351-
timeout = deadline - time.monotonic() if deadline else None
1351+
curr_time = time.monotonic()
1352+
timeout = deadline - curr_time if deadline else None
13521353
if self._backoff:
1353-
if self._backoff_connection_time < time.monotonic():
1354+
if self._backoff_connection_time < curr_time:
13541355
break
1355-
timeout = 0.01
1356+
if deadline is None or deadline > self._backoff_connection_time:
1357+
timeout = self._backoff_connection_time - curr_time
13561358
if not await _async_cond_wait(self._max_connecting_cond, timeout):
1357-
# Check whether we should continue to wait for the backoff condition.
1358-
curr_time = time.monotonic()
1359-
if self._backoff and (deadline is None or curr_time < deadline):
1360-
if self._backoff_connection_time > curr_time:
1361-
continue
1359+
# Check whether a backoff period has expired.
1360+
if self._backoff and time.monotonic() > self._backoff_connection_time:
13621361
break
13631362
# Timed out, notify the next thread to ensure a
13641363
# timeout doesn't consume the condition.
@@ -1376,9 +1375,6 @@ async def _get_conn(
13761375
if await self._perished(conn):
13771376
conn = None
13781377
continue
1379-
# See if we need to wait for the backoff period.
1380-
elif self._backoff and (self._backoff_connection_time > time.monotonic()):
1381-
continue
13821378
else: # We need to create a new connection
13831379
try:
13841380
conn = await self.connect(handler=handler)

pymongo/synchronous/pool.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,17 +1346,16 @@ def _get_conn(
13461346
with self._max_connecting_cond:
13471347
self._raise_if_not_ready(checkout_started_time, emit_event=False)
13481348
while not (self.conns or self._pending < self.max_connecting):
1349-
timeout = deadline - time.monotonic() if deadline else None
1349+
curr_time = time.monotonic()
1350+
timeout = deadline - curr_time if deadline else None
13501351
if self._backoff:
1351-
if self._backoff_connection_time < time.monotonic():
1352+
if self._backoff_connection_time < curr_time:
13521353
break
1353-
timeout = 0.01
1354+
if deadline is None or deadline > self._backoff_connection_time:
1355+
timeout = self._backoff_connection_time - curr_time
13541356
if not _cond_wait(self._max_connecting_cond, timeout):
1355-
# Check whether we should continue to wait for the backoff condition.
1356-
curr_time = time.monotonic()
1357-
if self._backoff and (deadline is None or curr_time < deadline):
1358-
if self._backoff_connection_time > curr_time:
1359-
continue
1357+
# Check whether a backoff period has expired.
1358+
if self._backoff and time.monotonic() > self._backoff_connection_time:
13601359
break
13611360
# Timed out, notify the next thread to ensure a
13621361
# timeout doesn't consume the condition.
@@ -1374,9 +1373,6 @@ def _get_conn(
13741373
if self._perished(conn):
13751374
conn = None
13761375
continue
1377-
# See if we need to wait for the backoff period.
1378-
elif self._backoff and (self._backoff_connection_time > time.monotonic()):
1379-
continue
13801376
else: # We need to create a new connection
13811377
try:
13821378
conn = self.connect(handler=handler)

test/discovery_and_monitoring/unified/backoff-network-error-fail.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@
202202
"client": "setupClient",
203203
"failPoint": {
204204
"configureFailPoint": "failCommand",
205-
"mode": "always",
205+
"mode": "alwaysOn",
206206
"data": {
207207
"failCommands": [
208208
"isMaster",

0 commit comments

Comments
 (0)