1616
1717from __future__ import annotations
1818
19- import asyncio
2019import atexit
2120import logging
2221import time
2726from pymongo ._csot import MovingMinimum
2827from pymongo .errors import NetworkTimeout , NotPrimaryError , OperationFailure , _OperationCancelled
2928from pymongo .hello import Hello
30- from pymongo .lock import _async_create_lock
29+ from pymongo .lock import _create_lock
3130from pymongo .logger import _SDAM_LOGGER , _debug_log , _SDAMStatusMessage
3231from pymongo .periodic_executor import _shutdown_executors
3332from pymongo .pool_options import _is_faas
@@ -277,7 +276,7 @@ async def _check_server(self) -> ServerDescription:
277276 await self ._reset_connection ()
278277 if isinstance (error , _OperationCancelled ):
279278 raise
280- await self ._rtt_monitor .reset ()
279+ self ._rtt_monitor .reset ()
281280 # Server type defaults to Unknown.
282281 return ServerDescription (address , error = error )
283282
@@ -316,9 +315,9 @@ async def _check_once(self) -> ServerDescription:
316315 self ._cancel_context = conn .cancel_context
317316 response , round_trip_time = await self ._check_with_socket (conn )
318317 if not response .awaitable :
319- await self ._rtt_monitor .add_sample (round_trip_time )
318+ self ._rtt_monitor .add_sample (round_trip_time )
320319
321- avg_rtt , min_rtt = await self ._rtt_monitor .get ()
320+ avg_rtt , min_rtt = self ._rtt_monitor .get ()
322321 sd = ServerDescription (address , response , avg_rtt , min_round_trip_time = min_rtt )
323322 if self ._publish :
324323 assert self ._listeners is not None
@@ -414,8 +413,6 @@ def _get_seedlist(self) -> Optional[list[tuple[str, Any]]]:
414413 if len (seedlist ) == 0 :
415414 # As per the spec: this should be treated as a failure.
416415 raise Exception
417- except asyncio .CancelledError :
418- raise
419416 except Exception :
420417 # As per the spec, upon encountering an error:
421418 # - An error must not be raised
@@ -444,28 +441,28 @@ def __init__(self, topology: Topology, topology_settings: TopologySettings, pool
444441 self ._pool = pool
445442 self ._moving_average = MovingAverage ()
446443 self ._moving_min = MovingMinimum ()
447- self ._lock = _async_create_lock ()
444+ self ._lock = _create_lock ()
448445
449446 async def close (self ) -> None :
450447 self .gc_safe_close ()
451448 # Increment the generation and maybe close the socket. If the executor
452449 # thread has the socket checked out, it will be closed when checked in.
453450 await self ._pool .reset ()
454451
455- async def add_sample (self , sample : float ) -> None :
452+ def add_sample (self , sample : float ) -> None :
456453 """Add a RTT sample."""
457- async with self ._lock :
454+ with self ._lock :
458455 self ._moving_average .add_sample (sample )
459456 self ._moving_min .add_sample (sample )
460457
461- async def get (self ) -> tuple [Optional [float ], float ]:
458+ def get (self ) -> tuple [Optional [float ], float ]:
462459 """Get the calculated average, or None if no samples yet and the min."""
463- async with self ._lock :
460+ with self ._lock :
464461 return self ._moving_average .get (), self ._moving_min .get ()
465462
466- async def reset (self ) -> None :
463+ def reset (self ) -> None :
467464 """Reset the average RTT."""
468- async with self ._lock :
465+ with self ._lock :
469466 self ._moving_average .reset ()
470467 self ._moving_min .reset ()
471468
@@ -475,12 +472,10 @@ async def _run(self) -> None:
475472 # heartbeat protocol (MongoDB 4.4+).
476473 # XXX: Skip check if the server is unknown?
477474 rtt = await self ._ping ()
478- await self .add_sample (rtt )
475+ self .add_sample (rtt )
479476 except ReferenceError :
480477 # Topology was garbage-collected.
481478 await self .close ()
482- except asyncio .CancelledError :
483- raise
484479 except Exception :
485480 await self ._pool .reset ()
486481
0 commit comments