Skip to content

Commit 22ae993

Browse files
committed
PYTHON-4579 Gossip $clusterTime from connection handshake
1 parent 14c8432 commit 22ae993

File tree

4 files changed

+18
-0
lines changed

4 files changed

+18
-0
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
@@ -312,6 +312,8 @@ def __init__(
312312
self.connect_rtt = 0.0
313313
self._client_id = pool._client_id
314314
self.creation_time = time.monotonic()
315+
# For gossiping $clusterTime from the connection handshake to the client.
316+
self._cluster_time = None
315317

316318
def set_conn_timeout(self, timeout: Optional[float]) -> None:
317319
"""Cache last timeout to avoid duplicate calls to conn.settimeout."""
@@ -1304,6 +1306,9 @@ async def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> A
13041306
conn.close_conn(ConnectionClosedReason.ERROR)
13051307
raise
13061308

1309+
if handler:
1310+
await handler.client._topology.receive_cluster_time(conn._cluster_time)
1311+
13071312
return conn
13081313

13091314
@contextlib.asynccontextmanager

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
@@ -312,6 +312,8 @@ def __init__(
312312
self.connect_rtt = 0.0
313313
self._client_id = pool._client_id
314314
self.creation_time = time.monotonic()
315+
# For gossiping $clusterTime from the connection handshake to the client.
316+
self._cluster_time = None
315317

316318
def set_conn_timeout(self, timeout: Optional[float]) -> None:
317319
"""Cache last timeout to avoid duplicate calls to conn.settimeout."""
@@ -1298,6 +1300,9 @@ def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> Connect
12981300
conn.close_conn(ConnectionClosedReason.ERROR)
12991301
raise
13001302

1303+
if handler:
1304+
handler.client._topology.receive_cluster_time(conn._cluster_time)
1305+
13011306
return conn
13021307

13031308
@contextlib.contextmanager

0 commit comments

Comments
 (0)