MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / SrvMonitor

Class SrvMonitor

pymongo/asynchronous/monitor.py:378–436  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

376
377
378class SrvMonitor(MonitorBase):
379 def __init__(self, topology: Topology, topology_settings: TopologySettings):
380 """Class to poll SRV records on a background thread.
381
382 Pass a Topology and a TopologySettings.
383
384 The Topology is weakly referenced.
385 """
386 super().__init__(
387 topology,
388 "pymongo_srv_polling_thread",
389 common.MIN_SRV_RESCAN_INTERVAL,
390 topology_settings.heartbeat_frequency,
391 )
392 self._settings = topology_settings
393 self._seedlist = self._settings._seeds
394 assert isinstance(self._settings.fqdn, str)
395 self._fqdn: str = self._settings.fqdn
396 self._startup_time = time.monotonic()
397
398 async def _run(self) -> None:
399 # Don't poll right after creation, wait 60 seconds first
400 if time.monotonic() < self._startup_time + common.MIN_SRV_RESCAN_INTERVAL:
401 return
402 seedlist = await self._get_seedlist()
403 if seedlist:
404 self._seedlist = seedlist
405 try:
406 await self._topology.on_srv_update(self._seedlist)
407 except ReferenceError:
408 # Topology was garbage-collected.
409 await self.close()
410
411 async def _get_seedlist(self) -> Optional[list[tuple[str, Any]]]:
412 """Poll SRV records for a seedlist.
413
414 Returns a list of ServerDescriptions.
415 """
416 try:
417 resolver = _SrvResolver(
418 self._fqdn,
419 self._settings.pool_options.connect_timeout,
420 self._settings.srv_service_name,
421 )
422 seedlist, ttl = await resolver.get_hosts_and_min_ttl()
423 if len(seedlist) == 0:
424 # As per the spec: this should be treated as a failure.
425 raise Exception
426 except Exception as exc:
427 # As per the spec, upon encountering an error:
428 # - An error must not be raised
429 # - SRV records must be rescanned every heartbeatFrequencyMS
430 # - Topology must be left unchanged
431 self.request_check()
432 _debug_log(_SDAM_LOGGER, message="SRV monitor check failed", failure=repr(exc))
433 return None
434 else:
435 self._executor.update_interval(max(ttl, common.MIN_SRV_RESCAN_INTERVAL))

Callers 1

__init__Method · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected