@@ -62,22 +62,22 @@ def _validate_tag_sets(tag_sets):
62
62
return tag_sets
63
63
64
64
65
+ # Distinct from the validator in common.py for URI option "maxStalenessSeconds".
65
66
def _validate_max_staleness (max_staleness ):
66
67
"""Validate max_staleness."""
67
- if max_staleness is None :
68
- return 0.0
69
-
70
- errmsg = "max_staleness must be an integer or float"
71
- try :
72
- max_staleness = float (max_staleness )
73
- except ValueError :
74
- raise ValueError (errmsg )
75
- except TypeError :
76
- raise TypeError (errmsg )
77
-
78
- if not 0 < max_staleness < 1e9 :
79
- raise ValueError (
80
- "max_staleness must be greater than 0 and less than one billion" )
68
+ if max_staleness != - 1 :
69
+ errmsg = "max_staleness must be an integer or float"
70
+ try :
71
+ max_staleness = float (max_staleness )
72
+ except ValueError :
73
+ raise ValueError (errmsg )
74
+ except TypeError :
75
+ raise TypeError (errmsg )
76
+
77
+ if not 0 < max_staleness < 1e9 :
78
+ raise ValueError (
79
+ "max_staleness must be greater than 0"
80
+ " and less than one billion" )
81
81
82
82
return max_staleness
83
83
@@ -88,7 +88,7 @@ class _ServerMode(object):
88
88
89
89
__slots__ = ("__mongos_mode" , "__mode" , "__tag_sets" , "__max_staleness" )
90
90
91
- def __init__ (self , mode , tag_sets = None , max_staleness = None ):
91
+ def __init__ (self , mode , tag_sets = None , max_staleness = - 1 ):
92
92
self .__mongos_mode = _MONGOS_MODES [mode ]
93
93
self .__mode = mode
94
94
self .__tag_sets = _validate_tag_sets (tag_sets )
@@ -107,7 +107,7 @@ def document(self):
107
107
doc = {'mode' : self .__mongos_mode }
108
108
if self .__tag_sets not in (None , [{}]):
109
109
doc ['tags' ] = self .__tag_sets
110
- if self .__max_staleness :
110
+ if self .__max_staleness != - 1 :
111
111
doc ['maxStalenessSeconds' ] = self .__max_staleness
112
112
return doc
113
113
@@ -136,9 +136,7 @@ def tag_sets(self):
136
136
def max_staleness (self ):
137
137
"""The maximum estimated length of time (in seconds) a replica set
138
138
secondary can fall behind the primary in replication before it will
139
- no longer be selected for operations."""
140
- if not self .__max_staleness :
141
- return None
139
+ no longer be selected for operations, or -1 for no maximum."""
142
140
return self .__max_staleness
143
141
144
142
@property
@@ -152,11 +150,11 @@ def min_wire_version(self):
152
150
`min_wire_version`, or the driver raises
153
151
:exc:`~pymongo.errors.ConfigurationError`.
154
152
"""
155
- return 5 if self .__max_staleness else 0
153
+ return 0 if self .__max_staleness == - 1 else 5
156
154
157
155
def __repr__ (self ):
158
156
return "%s(tag_sets=%r, max_staleness=%r)" % (
159
- self .name , self .__tag_sets , self .max_staleness )
157
+ self .name , self .__tag_sets , self .__max_staleness )
160
158
161
159
def __eq__ (self , other ):
162
160
if isinstance (other , _ServerMode ):
@@ -182,7 +180,7 @@ def __setstate__(self, value):
182
180
self .__mode = value ['mode' ]
183
181
self .__mongos_mode = _MONGOS_MODES [self .__mode ]
184
182
self .__tag_sets = _validate_tag_sets (value ['tag_sets' ])
185
- self .__max_staleness = value ['max_staleness' ]
183
+ self .__max_staleness = _validate_max_staleness ( value ['max_staleness' ])
186
184
187
185
188
186
class Primary (_ServerMode ):
@@ -227,9 +225,10 @@ class PrimaryPreferred(_ServerMode):
227
225
- `max_staleness`: (integer or float, in seconds) The maximum estimated
228
226
length of time a replica set secondary can fall behind the primary in
229
227
replication before it will no longer be selected for operations.
228
+ Default -1, meaning no maximum.
230
229
"""
231
230
232
- def __init__ (self , tag_sets = None , max_staleness = None ):
231
+ def __init__ (self , tag_sets = None , max_staleness = - 1 ):
233
232
super (PrimaryPreferred , self ).__init__ (_PRIMARY_PREFERRED ,
234
233
tag_sets ,
235
234
max_staleness )
@@ -260,9 +259,10 @@ class Secondary(_ServerMode):
260
259
- `max_staleness`: (integer or float, in seconds) The maximum estimated
261
260
length of time a replica set secondary can fall behind the primary in
262
261
replication before it will no longer be selected for operations.
262
+ Default -1, meaning no maximum.
263
263
"""
264
264
265
- def __init__ (self , tag_sets = None , max_staleness = None ):
265
+ def __init__ (self , tag_sets = None , max_staleness = - 1 ):
266
266
super (Secondary , self ).__init__ (_SECONDARY , tag_sets , max_staleness )
267
267
268
268
def __call__ (self , selection ):
@@ -288,9 +288,10 @@ class SecondaryPreferred(_ServerMode):
288
288
- `max_staleness`: (integer or float, in seconds) The maximum estimated
289
289
length of time a replica set secondary can fall behind the primary in
290
290
replication before it will no longer be selected for operations.
291
+ Default -1, meaning no maximum.
291
292
"""
292
293
293
- def __init__ (self , tag_sets = None , max_staleness = None ):
294
+ def __init__ (self , tag_sets = None , max_staleness = - 1 ):
294
295
super (SecondaryPreferred , self ).__init__ (_SECONDARY_PREFERRED ,
295
296
tag_sets ,
296
297
max_staleness )
@@ -323,9 +324,10 @@ class Nearest(_ServerMode):
323
324
- `max_staleness`: (integer or float, in seconds) The maximum estimated
324
325
length of time a replica set secondary can fall behind the primary in
325
326
replication before it will no longer be selected for operations.
327
+ Default -1, meaning no maximum.
326
328
"""
327
329
328
- def __init__ (self , tag_sets = None , max_staleness = None ):
330
+ def __init__ (self , tag_sets = None , max_staleness = - 1 ):
329
331
super (Nearest , self ).__init__ (_NEAREST , tag_sets , max_staleness )
330
332
331
333
def __call__ (self , selection ):
@@ -340,12 +342,12 @@ def __call__(self, selection):
340
342
Secondary , SecondaryPreferred , Nearest )
341
343
342
344
343
- def make_read_preference (mode , tag_sets , max_staleness = None ):
345
+ def make_read_preference (mode , tag_sets , max_staleness = - 1 ):
344
346
if mode == _PRIMARY :
345
347
if tag_sets not in (None , [{}]):
346
348
raise ConfigurationError ("Read preference primary "
347
349
"cannot be combined with tags" )
348
- if max_staleness :
350
+ if max_staleness != - 1 :
349
351
raise ConfigurationError ("Read preference primary cannot be "
350
352
"combined with maxStalenessSeconds" )
351
353
return Primary ()
0 commit comments