Skip to content

Commit eaab484

Browse files
committed
add pool backoff message
1 parent 4cc6619 commit eaab484

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

pymongo/asynchronous/pool.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ async def remove_stale_sockets(self, reference_generation: int) -> None:
10291029
self.requests -= 1
10301030
self.size_cond.notify()
10311031

1032-
def _handle_connection_error(self, error: BaseException, phase: str) -> None:
1032+
def _handle_connection_error(self, error: BaseException, phase: str, conn_id: int) -> None:
10331033
# Handle system overload condition for non-sdam pools.
10341034
# Look for an AutoReconnect error raised from a ConnectionResetError with
10351035
# errno == errno.ECONNRESET or raised from an OSError that we've created due to
@@ -1040,7 +1040,18 @@ def _handle_connection_error(self, error: BaseException, phase: str) -> None:
10401040
self._backoff += 1
10411041
error._add_error_label("SystemOverloadedError")
10421042
error._add_error_label("RetryableError")
1043-
print(f"Setting backoff in {phase}:", self._backoff) # noqa: T201
1043+
# Log the pool backoff message.
1044+
if self.enabled_for_logging and _CONNECTION_LOGGER.isEnabledFor(logging.DEBUG):
1045+
_debug_log(
1046+
_CONNECTION_LOGGER,
1047+
message=_ConnectionStatusMessage.POOL_BACKOFF,
1048+
clientId=self._client_id,
1049+
serverHost=self.address[0],
1050+
serverPort=self.address[1],
1051+
driverConnectionId=conn_id,
1052+
reason=_verbose_connection_error_reason(ConnectionClosedReason.POOL_BACKOFF),
1053+
error=ConnectionClosedReason.POOL_BACKOFF,
1054+
)
10441055

10451056
async def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> AsyncConnection:
10461057
"""Connect to Mongo and return a new AsyncConnection.
@@ -1103,7 +1114,7 @@ async def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> A
11031114
error=ConnectionClosedReason.ERROR,
11041115
)
11051116
if context["has_created_socket"]:
1106-
self._handle_connection_error(error, "handshake")
1117+
self._handle_connection_error(error, "handshake", conn_id)
11071118
if isinstance(error, (IOError, OSError, *SSLErrors)):
11081119
details = _get_timeout_details(self.opts)
11091120
_raise_connection_failure(self.address, error, timeout_details=details)
@@ -1127,7 +1138,7 @@ async def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> A
11271138
except BaseException as e:
11281139
async with self.lock:
11291140
self.active_contexts.discard(conn.cancel_context)
1130-
self._handle_connection_error(e, "hello")
1141+
self._handle_connection_error(e, "hello", conn_id)
11311142
await conn.close_conn(ConnectionClosedReason.ERROR)
11321143
raise
11331144

pymongo/logger.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class _ConnectionStatusMessage(str, enum.Enum):
4242
POOL_READY = "Connection pool ready"
4343
POOL_CLOSED = "Connection pool closed"
4444
POOL_CLEARED = "Connection pool cleared"
45+
POOL_BACKOFF = "Connection pool backoff"
4546

4647
CONN_CREATED = "Connection created"
4748
CONN_READY = "Connection ready"
@@ -88,6 +89,7 @@ class _SDAMStatusMessage(str, enum.Enum):
8889
_VERBOSE_CONNECTION_ERROR_REASONS = {
8990
ConnectionClosedReason.POOL_CLOSED: "Connection pool was closed",
9091
ConnectionCheckOutFailedReason.POOL_CLOSED: "Connection pool was closed",
92+
ConnectionClosedReason.POOL_BACKOFF: "Connection pool is in backoff",
9193
ConnectionClosedReason.STALE: "Connection pool was stale",
9294
ConnectionClosedReason.ERROR: "An error occurred while using the connection",
9395
ConnectionCheckOutFailedReason.CONN_ERROR: "An error occurred while trying to establish a new connection",

pymongo/synchronous/pool.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ def remove_stale_sockets(self, reference_generation: int) -> None:
10251025
self.requests -= 1
10261026
self.size_cond.notify()
10271027

1028-
def _handle_connection_error(self, error: BaseException, phase: str) -> None:
1028+
def _handle_connection_error(self, error: BaseException, phase: str, conn_id: int) -> None:
10291029
# Handle system overload condition for non-sdam pools.
10301030
# Look for an AutoReconnect error raised from a ConnectionResetError with
10311031
# errno == errno.ECONNRESET or raised from an OSError that we've created due to
@@ -1036,7 +1036,18 @@ def _handle_connection_error(self, error: BaseException, phase: str) -> None:
10361036
self._backoff += 1
10371037
error._add_error_label("SystemOverloadedError")
10381038
error._add_error_label("RetryableError")
1039-
print(f"Setting backoff in {phase}:", self._backoff) # noqa: T201
1039+
# Log the pool backoff message.
1040+
if self.enabled_for_logging and _CONNECTION_LOGGER.isEnabledFor(logging.DEBUG):
1041+
_debug_log(
1042+
_CONNECTION_LOGGER,
1043+
message=_ConnectionStatusMessage.POOL_BACKOFF,
1044+
clientId=self._client_id,
1045+
serverHost=self.address[0],
1046+
serverPort=self.address[1],
1047+
driverConnectionId=conn_id,
1048+
reason=_verbose_connection_error_reason(ConnectionClosedReason.POOL_BACKOFF),
1049+
error=ConnectionClosedReason.POOL_BACKOFF,
1050+
)
10401051

10411052
def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> Connection:
10421053
"""Connect to Mongo and return a new Connection.
@@ -1099,7 +1110,7 @@ def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> Connect
10991110
error=ConnectionClosedReason.ERROR,
11001111
)
11011112
if context["has_created_socket"]:
1102-
self._handle_connection_error(error, "handshake")
1113+
self._handle_connection_error(error, "handshake", conn_id)
11031114
if isinstance(error, (IOError, OSError, *SSLErrors)):
11041115
details = _get_timeout_details(self.opts)
11051116
_raise_connection_failure(self.address, error, timeout_details=details)
@@ -1123,7 +1134,7 @@ def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> Connect
11231134
except BaseException as e:
11241135
with self.lock:
11251136
self.active_contexts.discard(conn.cancel_context)
1126-
self._handle_connection_error(e, "hello")
1137+
self._handle_connection_error(e, "hello", conn_id)
11271138
conn.close_conn(ConnectionClosedReason.ERROR)
11281139
raise
11291140

0 commit comments

Comments
 (0)