Skip to content

Commit f67195e

Browse files
committed
update to use durationms
1 parent 5e64aa9 commit f67195e

File tree

8 files changed

+28
-42
lines changed

8 files changed

+28
-42
lines changed

pymongo/asynchronous/pool.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,18 +1048,20 @@ def _handle_connection_error(self, error: BaseException, phase: str) -> None:
10481048
def backoff(self):
10491049
"""Set/increase backoff mode."""
10501050
self._backoff += 1
1051+
backoff_duration_sec = _backoff(self._backoff)
1052+
backoff_duration_ms = int(backoff_duration_sec * 1000)
10511053
if self.state != PoolState.BACKOFF:
10521054
self.state = PoolState.BACKOFF
10531055
if self.enabled_for_cmap:
10541056
assert self.opts._event_listeners is not None
1055-
self.opts._event_listeners.publish_pool_backoff(self.address, self._backoff)
1056-
self._backoff_connection_time = _backoff(self._backoff) + time.monotonic()
1057+
self.opts._event_listeners.publish_pool_backoff(self.address, backoff_duration_ms)
1058+
self._backoff_connection_time = backoff_duration_sec + time.monotonic()
10571059

10581060
# Log the pool backoff message.
10591061
if self.enabled_for_logging and _CONNECTION_LOGGER.isEnabledFor(logging.DEBUG):
10601062
_debug_log(
10611063
_CONNECTION_LOGGER,
1062-
message=_ConnectionStatusMessage.POOL_BACKOFF % self._backoff,
1064+
message=_ConnectionStatusMessage.POOL_BACKOFF % backoff_duration_ms,
10631065
clientId=self._client_id,
10641066
serverHost=self.address[0],
10651067
serverPort=self.address[1],

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 %sms"
4646

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

pymongo/monitoring.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -931,21 +931,21 @@ class PoolBackoffEvent(_PoolEvent):
931931
932932
:param address: The address (host, port) pair of the server this Pool is
933933
attempting to connect to.
934-
:param attempt: The backoff attempt number.
934+
:param duration_ms: The backoff duration in ms.
935935
936936
.. versionadded:: 4.16
937937
"""
938938

939-
__slots__ = ("__attempt",)
939+
__slots__ = ("__duration_ms",)
940940

941-
def __init__(self, address: _Address, attempt: int) -> None:
941+
def __init__(self, address: _Address, duration_ms: int) -> None:
942942
super().__init__(address)
943-
self.__attempt = attempt
943+
self.__duration_ms = duration_ms
944944

945945
@property
946-
def attempt(self) -> Optional[ObjectId]:
947-
"""The backoff attempt number."""
948-
return self.__attempt
946+
def duration_ms(self) -> Optional[ObjectId]:
947+
"""The backoff duration in ms."""
948+
return self.__duration_ms
949949

950950

951951
class ConnectionClosedReason:

pymongo/synchronous/pool.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,18 +1044,20 @@ def _handle_connection_error(self, error: BaseException, phase: str) -> None:
10441044
def backoff(self):
10451045
"""Set/increase backoff mode."""
10461046
self._backoff += 1
1047+
backoff_duration_sec = _backoff(self._backoff)
1048+
backoff_duration_ms = int(backoff_duration_sec * 1000)
10471049
if self.state != PoolState.BACKOFF:
10481050
self.state = PoolState.BACKOFF
10491051
if self.enabled_for_cmap:
10501052
assert self.opts._event_listeners is not None
1051-
self.opts._event_listeners.publish_pool_backoff(self.address, self._backoff)
1052-
self._backoff_connection_time = _backoff(self._backoff) + time.monotonic()
1053+
self.opts._event_listeners.publish_pool_backoff(self.address, backoff_duration_ms)
1054+
self._backoff_connection_time = backoff_duration_sec + time.monotonic()
10531055

10541056
# Log the pool backoff message.
10551057
if self.enabled_for_logging and _CONNECTION_LOGGER.isEnabledFor(logging.DEBUG):
10561058
_debug_log(
10571059
_CONNECTION_LOGGER,
1058-
message=_ConnectionStatusMessage.POOL_BACKOFF % self._backoff,
1060+
message=_ConnectionStatusMessage.POOL_BACKOFF % backoff_duration_ms,
10591061
clientId=self._client_id,
10601062
serverHost=self.address[0],
10611063
serverPort=self.address[1],

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

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -129,34 +129,22 @@
129129
"poolReadyEvent": {}
130130
},
131131
{
132-
"poolBackoffEvent": {
133-
"attempt": 1
134-
}
132+
"poolBackoffEvent": {}
135133
},
136134
{
137-
"poolBackoffEvent": {
138-
"attempt": 2
139-
}
135+
"poolBackoffEvent": {}
140136
},
141137
{
142-
"poolBackoffEvent": {
143-
"attempt": 3
144-
}
138+
"poolBackoffEvent": {}
145139
},
146140
{
147-
"poolBackoffEvent": {
148-
"attempt": 4
149-
}
141+
"poolBackoffEvent": {}
150142
},
151143
{
152-
"poolBackoffEvent": {
153-
"attempt": 5
154-
}
144+
"poolBackoffEvent": {}
155145
},
156146
{
157-
"poolBackoffEvent": {
158-
"attempt": 6
159-
}
147+
"poolBackoffEvent": {}
160148
}
161149
]
162150
}

test/discovery_and_monitoring/unified/auth-network-error.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,7 @@
119119
"arguments": {
120120
"client": "client",
121121
"event": {
122-
"poolBackoffEvent": {
123-
"attempt": 1
124-
}
122+
"poolBackoffEvent": {}
125123
},
126124
"count": 1
127125
}

test/discovery_and_monitoring/unified/auth-network-timeout-error.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,7 @@
119119
"arguments": {
120120
"client": "client",
121121
"event": {
122-
"poolBackoffEvent": {
123-
"attempt": 1
124-
}
122+
"poolBackoffEvent": {}
125123
},
126124
"count": 1
127125
}
@@ -132,9 +130,7 @@
132130
"arguments": {
133131
"client": "client",
134132
"event": {
135-
"poolBackoffEvent": {
136-
"attempt": 2
137-
}
133+
"poolBackoffEvent": {}
138134
},
139135
"count": 1
140136
}

test/unified_format_shared.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ def match_event(self, expectation, actual):
621621
self.test.assertIsInstance(actual, PoolClosedEvent)
622622
elif name == "poolBackoffEvent":
623623
self.test.assertIsInstance(actual, PoolBackoffEvent)
624-
self.test.assertIsInstance(actual.attempt, int)
624+
self.test.assertIsInstance(actual.duration_ms, int)
625625
elif name == "connectionCreatedEvent":
626626
self.test.assertIsInstance(actual, ConnectionCreatedEvent)
627627
elif name == "connectionReadyEvent":

0 commit comments

Comments
 (0)