MCPcopy Create free account
hub / github.com/mongodb/mongo-python-driver / PrimaryPreferred

Class PrimaryPreferred

pymongo/read_preferences.py:308–352  ·  view source on GitHub ↗

PrimaryPreferred 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 sent to the primary of a shard if available, otherwise a shard s

Source from the content-addressed store, hash-verified

306
307
308class PrimaryPreferred(_ServerMode):
309 """PrimaryPreferred read preference.
310
311 * When directly connected to one mongod queries are allowed to standalone
312 servers, to a replica set primary, or to replica set secondaries.
313 * When connected to a mongos queries are sent to the primary of a shard if
314 available, otherwise a shard secondary.
315 * When connected to a replica set queries are sent to the primary if
316 available, otherwise a secondary.
317
318 .. note:: When a :class:`~pymongo.mongo_client.MongoClient` is first
319 created reads will be routed to an available secondary until the
320 primary of the replica set is discovered.
321
322 :param tag_sets: The :attr:`~tag_sets` to use if the primary is not
323 available.
324 :param max_staleness: (integer, in seconds) The maximum estimated
325 length of time a replica set secondary can fall behind the primary in
326 replication before it will no longer be selected for operations.
327 Default -1, meaning no maximum. If it is set, it must be at least
328 90 seconds.
329 :param hedge: **DEPRECATED** - The :attr:`~hedge` for this read preference.
330
331 .. versionchanged:: 3.11
332 Added ``hedge`` parameter.
333 """
334
335 __slots__ = ()
336
337 def __init__(
338 self,
339 tag_sets: Optional[_TagSets] = None,
340 max_staleness: int = -1,
341 hedge: Optional[_Hedge] = None,
342 ) -> None:
343 super().__init__(_PRIMARY_PREFERRED, tag_sets, max_staleness, hedge)
344
345 def __call__(self, selection: Selection) -> Selection:
346 """Apply this read preference to Selection."""
347 if selection.primary:
348 return selection.primary_selection
349 else:
350 return secondary_with_tags_server_selector(
351 self.tag_sets, max_staleness_selectors.select(self.max_staleness, selection)
352 )
353
354
355class Secondary(_ServerMode):

Calls

no outgoing calls