Skip to content

Commit d38d930

Browse files
committed
PYTHON-4579 Gossip $clusterTime from connection handshake
1 parent 46e4660 commit d38d930

File tree

5 files changed

+19
-19
lines changed

5 files changed

+19
-19
lines changed

pymongo/asynchronous/network.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ async def command(
207207
)
208208

209209
response_doc = unpacked_docs[0]
210+
if not conn.ready:
211+
cluster_time = response_doc.get("$clusterTime")
212+
if cluster_time:
213+
conn._cluster_time = cluster_time
210214
if client:
211215
await client._process_response(response_doc, session)
212216
if check:

pymongo/asynchronous/pool.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ def __init__(
310310
self.connect_rtt = 0.0
311311
self._client_id = pool._client_id
312312
self.creation_time = time.monotonic()
313+
# For gossiping $clusterTime from the connection handshake to the client.
314+
self._cluster_time = None
313315

314316
def set_conn_timeout(self, timeout: Optional[float]) -> None:
315317
"""Cache last timeout to avoid duplicate calls to conn.settimeout."""
@@ -1314,6 +1316,9 @@ async def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> A
13141316
conn.close_conn(ConnectionClosedReason.ERROR)
13151317
raise
13161318

1319+
if handler:
1320+
await handler.client._topology.receive_cluster_time(conn._cluster_time)
1321+
13171322
return conn
13181323

13191324
@contextlib.asynccontextmanager

pymongo/synchronous/monitor.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -250,27 +250,9 @@ def _check_server(self) -> ServerDescription:
250250
self._conn_id = None
251251
start = time.monotonic()
252252
try:
253-
<<<<<<< HEAD
254-
try:
255-
return self._check_once()
256-
except (OperationFailure, NotPrimaryError) as exc:
257-
# Update max cluster time even when hello fails.
258-
details = cast(Mapping[str, Any], exc.details)
259-
self._topology.receive_cluster_time(details.get("$clusterTime"))
260-
raise
253+
return self._check_once()
261254
except asyncio.CancelledError:
262255
raise
263-
||||||| parent of 14c8432bc (PYTHON-4579 Stop gossiping $clusterTime on SDAM connections)
264-
try:
265-
return self._check_once()
266-
except (OperationFailure, NotPrimaryError) as exc:
267-
# Update max cluster time even when hello fails.
268-
details = cast(Mapping[str, Any], exc.details)
269-
self._topology.receive_cluster_time(details.get("$clusterTime"))
270-
raise
271-
=======
272-
return self._check_once()
273-
>>>>>>> 14c8432bc (PYTHON-4579 Stop gossiping $clusterTime on SDAM connections)
274256
except ReferenceError:
275257
raise
276258
except Exception as error:

pymongo/synchronous/network.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ def command(
207207
)
208208

209209
response_doc = unpacked_docs[0]
210+
if not conn.ready:
211+
cluster_time = response_doc.get("$clusterTime")
212+
if cluster_time:
213+
conn._cluster_time = cluster_time
210214
if client:
211215
client._process_response(response_doc, session)
212216
if check:

pymongo/synchronous/pool.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ def __init__(
310310
self.connect_rtt = 0.0
311311
self._client_id = pool._client_id
312312
self.creation_time = time.monotonic()
313+
# For gossiping $clusterTime from the connection handshake to the client.
314+
self._cluster_time = None
313315

314316
def set_conn_timeout(self, timeout: Optional[float]) -> None:
315317
"""Cache last timeout to avoid duplicate calls to conn.settimeout."""
@@ -1308,6 +1310,9 @@ def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> Connect
13081310
conn.close_conn(ConnectionClosedReason.ERROR)
13091311
raise
13101312

1313+
if handler:
1314+
handler.client._topology.receive_cluster_time(conn._cluster_time)
1315+
13111316
return conn
13121317

13131318
@contextlib.contextmanager

0 commit comments

Comments
 (0)