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 Jan 22, 2025
1 parent 46e4660 commit d38d930
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 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 @@ -310,6 +310,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 @@ -1314,6 +1316,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
20 changes: 1 addition & 19 deletions pymongo/synchronous/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,27 +250,9 @@ def _check_server(self) -> ServerDescription:
self._conn_id = None
start = time.monotonic()
try:
<<<<<<< HEAD
try:
return self._check_once()
except (OperationFailure, NotPrimaryError) as exc:
# Update max cluster time even when hello fails.
details = cast(Mapping[str, Any], exc.details)
self._topology.receive_cluster_time(details.get("$clusterTime"))
raise
return self._check_once()
except asyncio.CancelledError:
raise
||||||| parent of 14c8432bc (PYTHON-4579 Stop gossiping $clusterTime on SDAM connections)
try:
return self._check_once()
except (OperationFailure, NotPrimaryError) as exc:
# Update max cluster time even when hello fails.
details = cast(Mapping[str, Any], exc.details)
self._topology.receive_cluster_time(details.get("$clusterTime"))
raise
=======
return self._check_once()
>>>>>>> 14c8432bc (PYTHON-4579 Stop gossiping $clusterTime on SDAM connections)
except ReferenceError:
raise
except Exception as error:
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 @@ -310,6 +310,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 @@ -1308,6 +1310,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 d38d930

Please sign in to comment.