Skip to content

Commit

Permalink
PYTHON-4579 Gossip $clusterTime from connection handshake
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaneHarvey committed Oct 14, 2024
1 parent 14c8432 commit 22ae993
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pymongo/asynchronous/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ async def command(
)

response_doc = unpacked_docs[0]
if not conn.ready:
cluster_time = response_doc.get("$clusterTime")
if cluster_time:
conn._cluster_time = cluster_time
if client:
await client._process_response(response_doc, session)
if check:
Expand Down
5 changes: 5 additions & 0 deletions pymongo/asynchronous/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ def __init__(
self.connect_rtt = 0.0
self._client_id = pool._client_id
self.creation_time = time.monotonic()
# For gossiping $clusterTime from the connection handshake to the client.
self._cluster_time = None

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

if handler:
await handler.client._topology.receive_cluster_time(conn._cluster_time)

return conn

@contextlib.asynccontextmanager
Expand Down
4 changes: 4 additions & 0 deletions pymongo/synchronous/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ def command(
)

response_doc = unpacked_docs[0]
if not conn.ready:
cluster_time = response_doc.get("$clusterTime")
if cluster_time:
conn._cluster_time = cluster_time
if client:
client._process_response(response_doc, session)
if check:
Expand Down
5 changes: 5 additions & 0 deletions pymongo/synchronous/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ def __init__(
self.connect_rtt = 0.0
self._client_id = pool._client_id
self.creation_time = time.monotonic()
# For gossiping $clusterTime from the connection handshake to the client.
self._cluster_time = None

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

if handler:
handler.client._topology.receive_cluster_time(conn._cluster_time)

return conn

@contextlib.contextmanager
Expand Down

0 comments on commit 22ae993

Please sign in to comment.