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

Class SecondaryPreferred

pymongo/read_preferences.py:394–439  ·  view source on GitHub ↗

SecondaryPreferred read preference. * When directly connected to one mongod queries are allowed to standalone servers, to a replica set primary, or to replica set secondaries. * When connected to a mongos queries are distributed among shard secondaries, or the shard primary if n

Source from the content-addressed store, hash-verified

392
393
394class SecondaryPreferred(_ServerMode):
395 """SecondaryPreferred read preference.
396
397 * When directly connected to one mongod queries are allowed to standalone
398 servers, to a replica set primary, or to replica set secondaries.
399 * When connected to a mongos queries are distributed among shard
400 secondaries, or the shard primary if no secondary is available.
401 * When connected to a replica set queries are distributed among
402 secondaries, or the primary if no secondary is available.
403
404 .. note:: When a :class:`~pymongo.mongo_client.MongoClient` is first
405 created reads will be routed to the primary of the replica set until
406 an available secondary is discovered.
407
408 :param tag_sets: The :attr:`~tag_sets` for this read preference.
409 :param max_staleness: (integer, in seconds) The maximum estimated
410 length of time a replica set secondary can fall behind the primary in
411 replication before it will no longer be selected for operations.
412 Default -1, meaning no maximum. If it is set, it must be at least
413 90 seconds.
414 :param hedge: **DEPRECATED** - The :attr:`~hedge` for this read preference.
415
416 .. versionchanged:: 3.11
417 Added ``hedge`` parameter.
418 """
419
420 __slots__ = ()
421
422 def __init__(
423 self,
424 tag_sets: Optional[_TagSets] = None,
425 max_staleness: int = -1,
426 hedge: Optional[_Hedge] = None,
427 ) -> None:
428 super().__init__(_SECONDARY_PREFERRED, tag_sets, max_staleness, hedge)
429
430 def __call__(self, selection: Selection) -> Selection:
431 """Apply this read preference to Selection."""
432 secondaries = secondary_with_tags_server_selector(
433 self.tag_sets, max_staleness_selectors.select(self.max_staleness, selection)
434 )
435
436 if secondaries:
437 return secondaries
438 else:
439 return selection.primary_selection
440
441
442class Nearest(_ServerMode):

Calls

no outgoing calls