Skip to content

Commit a4dd0f1

Browse files
committed
wip add tests
1 parent ae45464 commit a4dd0f1

File tree

10 files changed

+439
-7
lines changed

10 files changed

+439
-7
lines changed

pymongo/asynchronous/pool.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,6 @@ async def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> A
11571157

11581158
# Clear the backoff state.
11591159
if self._backoff:
1160-
self._backoff = 0
11611160
await self.ready()
11621161

11631162
return conn
@@ -1242,7 +1241,7 @@ async def checkout(
12421241
await self.checkin(conn)
12431242

12441243
def _raise_if_not_ready(self, checkout_started_time: float, emit_event: bool) -> None:
1245-
if self.state != PoolState.READY:
1244+
if self.state not in (PoolState.READY, PoolState.BACKOFF):
12461245
if emit_event:
12471246
duration = time.monotonic() - checkout_started_time
12481247
if self.enabled_for_cmap:

pymongo/logger.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +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 attempt number {%s}"
45+
POOL_BACKOFF = "Connection pool backoff attempt number %s"
4646

4747
CONN_CREATED = "Connection created"
4848
CONN_READY = "Connection ready"

pymongo/monitoring.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ def pool_cleared(self, event):
137137
def pool_closed(self, event):
138138
logging.info("[pool {0.address}] pool closed".format(event))
139139
140+
def pool_backoff(self, event):
141+
logging.info("[pool {0.address}] pool backoff attempt {0.event}".format(event))
142+
140143
def connection_created(self, event):
141144
logging.info("[pool {0.address}][connection #{0.connection_id}] "
142145
"connection created".format(event))
@@ -305,6 +308,15 @@ def pool_closed(self, event: PoolClosedEvent) -> None:
305308
"""
306309
raise NotImplementedError
307310

311+
def pool_backoff(self, event: PoolBackoffEvent) -> None:
312+
"""Abstract method to handle a `PoolBackoffEvent`.
313+
314+
Emitted when a connection Pool is in backoff.
315+
316+
:param event: An instance of :class:`PoolBackoffEvent`.
317+
"""
318+
raise NotImplementedError
319+
308320
def connection_created(self, event: ConnectionCreatedEvent) -> None:
309321
"""Abstract method to handle a :class:`ConnectionCreatedEvent`.
310322
@@ -1852,12 +1864,12 @@ def publish_pool_closed(self, address: _Address) -> None:
18521864
except Exception:
18531865
_handle_exception()
18541866

1855-
def publish_pool_backoutt(self, address: _Address, attempt: int) -> None:
1867+
def publish_pool_backoff(self, address: _Address, attempt: int) -> None:
18561868
"""Publish a :class:`PoolBackoffEvent` to all pool listeners."""
18571869
event = PoolBackoffEvent(address, attempt)
18581870
for subscriber in self.__cmap_listeners:
18591871
try:
1860-
subscriber.pool_closed(event)
1872+
subscriber.pool_backoff(event)
18611873
except Exception:
18621874
_handle_exception()
18631875

pymongo/synchronous/pool.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,6 @@ def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> Connect
11531153

11541154
# Clear the backoff state.
11551155
if self._backoff:
1156-
self._backoff = 0
11571156
self.ready()
11581157

11591158
return conn
@@ -1238,7 +1237,7 @@ def checkout(
12381237
self.checkin(conn)
12391238

12401239
def _raise_if_not_ready(self, checkout_started_time: float, emit_event: bool) -> None:
1241-
if self.state != PoolState.READY:
1240+
if self.state not in (PoolState.READY, PoolState.BACKOFF):
12421241
if emit_event:
12431242
duration = time.monotonic() - checkout_started_time
12441243
if self.enabled_for_cmap:

test/asynchronous/test_connection_monitoring.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
ConnectionClosedReason,
5353
ConnectionCreatedEvent,
5454
ConnectionReadyEvent,
55+
PoolBackoffEvent,
5556
PoolClearedEvent,
5657
PoolClosedEvent,
5758
PoolCreatedEvent,
@@ -75,6 +76,7 @@
7576
"ConnectionPoolReady": PoolReadyEvent,
7677
"ConnectionPoolCleared": PoolClearedEvent,
7778
"ConnectionPoolClosed": PoolClosedEvent,
79+
"ConnectionPoolBackoff": PoolBackoffEvent,
7880
# Error types.
7981
"PoolClosedError": _PoolClosedError,
8082
"WaitQueueTimeoutError": WaitQueueTimeoutError,

0 commit comments

Comments
 (0)