Skip to content

Commit 7548f7b

Browse files
committed
clean up logic
1 parent 0b4b265 commit 7548f7b

File tree

4 files changed

+31
-27
lines changed

4 files changed

+31
-27
lines changed

pymongo/asynchronous/pool.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import asyncio
1818
import collections
1919
import contextlib
20+
import errno
2021
import logging
2122
import os
2223
import sys
@@ -1034,18 +1035,19 @@ async def remove_stale_sockets(self, reference_generation: int) -> None:
10341035
# error._add_error_label("SystemOverloadedError")
10351036

10361037
def _handle_connection_error(self, error: Exception, phase: str) -> None:
1037-
# Handle system overload condition. When the base AutoReconnect is
1038-
# raised and we are not an sdam pool, add to backoff and add the
1039-
# appropriate error label.
1040-
if (
1041-
not self.is_sdam
1042-
and "connection reset by peer" in str(error).lower()
1043-
or ("connection closed" in str(error).lower() and self._backoff)
1044-
):
1045-
self._backoff += 1
1046-
error._add_error_label("SystemOverloadedError")
1047-
error._add_error_label("RetryableError")
1048-
print(f"Setting backoff in {phase}:", self._backoff) # noqa: T201
1038+
# Handle system overload condition for non-sdam pools.
1039+
# Look for an AutoReconnect error raise from a ConnectionResetError with
1040+
# errno == errno.ECONNRESET. If found, set backoff and add error labels.
1041+
if self.is_sdam or type(error) != AutoReconnect or not len(error.errors):
1042+
return
1043+
if not isinstance(error.errors[0], ConnectionResetError):
1044+
return
1045+
if error.errors[0].errno != errno.ECONNRESET:
1046+
return
1047+
self._backoff += 1
1048+
error._add_error_label("SystemOverloadedError")
1049+
error._add_error_label("RetryableError")
1050+
print(f"Setting backoff in {phase}:", self._backoff) # noqa: T201
10491051

10501052
async def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> AsyncConnection:
10511053
"""Connect to Mongo and return a new AsyncConnection.

pymongo/network_layer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,11 @@ def close(self, exc: Optional[Exception] = None) -> None:
270270
self.transport.abort()
271271
self._resolve_pending(exc)
272272
self._connection_lost = True
273-
self._closing_exception = exc
273+
self._closing_exception = exc # type:ignore[assignment]
274274

275275
def connection_lost(self, exc: Optional[Exception] = None) -> None:
276276
self._resolve_pending(exc)
277-
self._closing_exception = exc
277+
self._closing_exception = exc # type:ignore[assignment]
278278
if not self._closed.done():
279279
self._closed.set_result(None)
280280

pymongo/pool_shared.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def _raise_connection_failure(
149149
# 'timed out' appears in all the timeout related SSLErrors raised.
150150
raise NetworkTimeout(msg) from error
151151
else:
152-
raise AutoReconnect(msg) from error
152+
raise AutoReconnect(msg, errors=[error]) from error
153153

154154

155155
class _CancellationContext:

pymongo/synchronous/pool.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import asyncio
1818
import collections
1919
import contextlib
20+
import errno
2021
import logging
2122
import os
2223
import sys
@@ -1030,18 +1031,19 @@ def remove_stale_sockets(self, reference_generation: int) -> None:
10301031
# error._add_error_label("SystemOverloadedError")
10311032

10321033
def _handle_connection_error(self, error: Exception, phase: str) -> None:
1033-
# Handle system overload condition. When the base AutoReconnect is
1034-
# raised and we are not an sdam pool, add to backoff and add the
1035-
# appropriate error label.
1036-
if (
1037-
not self.is_sdam
1038-
and "connection reset by peer" in str(error).lower()
1039-
or ("connection closed" in str(error).lower() and self._backoff)
1040-
):
1041-
self._backoff += 1
1042-
error._add_error_label("SystemOverloadedError")
1043-
error._add_error_label("RetryableError")
1044-
print(f"Setting backoff in {phase}:", self._backoff) # noqa: T201
1034+
# Handle system overload condition for non-sdam pools.
1035+
# Look for an AutoReconnect error raise from a ConnectionResetError with
1036+
# errno == errno.ECONNRESET. If found, set backoff and add error labels.
1037+
if self.is_sdam or type(error) != AutoReconnect or not len(error.errors):
1038+
return
1039+
if not isinstance(error.errors[0], ConnectionResetError):
1040+
return
1041+
if error.errors[0].errno != errno.ECONNRESET:
1042+
return
1043+
self._backoff += 1
1044+
error._add_error_label("SystemOverloadedError")
1045+
error._add_error_label("RetryableError")
1046+
print(f"Setting backoff in {phase}:", self._backoff) # noqa: T201
10451047

10461048
def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> Connection:
10471049
"""Connect to Mongo and return a new Connection.

0 commit comments

Comments
 (0)