@@ -22,6 +22,11 @@ class Collection
22
22
*/
23
23
protected $ collection ;
24
24
25
+ /**
26
+ * @var int Amount of times to retry CRUD operations for "not master"
27
+ */
28
+ protected $ maxRetries = 1 ;
29
+
25
30
/**
26
31
* Constructor, sets the MongoCollection instance.
27
32
*
@@ -56,6 +61,21 @@ public function setCollection(MongoCollection $collection)
56
61
return $ this ;
57
62
}
58
63
64
+ /**
65
+ * Set the max amount of times to retry CRUD operations in the case of
66
+ * "not master" exceptions.
67
+ *
68
+ * @param int $amount The amount of times
69
+ *
70
+ * @return object $this
71
+ */
72
+ public function setMaxRetries ($ amount )
73
+ {
74
+ $ this ->maxRetries = $ amount ;
75
+
76
+ return $ this ;
77
+ }
78
+
59
79
/**
60
80
* Drops the current collection.
61
81
*
@@ -221,18 +241,29 @@ public function remove($criteria, $options = array())
221
241
throw new \InvalidArgumentException ('Remove criteria must be an array. ' );
222
242
}
223
243
224
- try {
225
- $ result = $ this ->collection ->remove ($ criteria , $ options );
226
- } catch (MongoCursorException $ e ) {
227
- // Retry "remove" in case of rediscovery latency issues
228
- // in replica set failover. Error codes 10107, 13435, and 10058
229
- // are MongoCursorException's "not master" errors.
230
- if (in_array ($ e ->getCode (), array (10107 , 13435 , 10058 ))) {
244
+ $ maxRetries = $ this ->maxRetries ;
245
+ $ tries = 0 ;
246
+
247
+ do {
248
+ try {
231
249
$ result = $ this ->collection ->remove ($ criteria , $ options );
232
- } else {
233
- throw $ e ;
250
+ break ;
251
+ } catch (MongoCursorException $ e ) {
252
+ // Retry "save" in case of rediscovery latency issues
253
+ // in replica set failover. Error codes 10107, 13435, and 10058
254
+ // are MongoCursorException's "not master" errors.
255
+ if (in_array ($ e ->getCode (), array (10107 , 13435 , 10058 ))) {
256
+ if ($ tries === $ maxRetries ) {
257
+ throw $ e ;
258
+ } else {
259
+ $ tries ++;
260
+ continue ;
261
+ }
262
+ } else {
263
+ throw $ e ;
264
+ }
234
265
}
235
- }
266
+ } while ( $ tries <= $ maxRetries );
236
267
237
268
return $ result === true || (bool ) $ result ['ok ' ];
238
269
}
@@ -323,21 +354,32 @@ public function findOne($query = array(), $fields = array())
323
354
*/
324
355
public function insert (array $ data , $ options = array ())
325
356
{
357
+ $ maxRetries = $ this ->maxRetries ;
358
+ $ tries = 0 ;
359
+
326
360
// Check whether we're dealing with a batch insert.
327
361
if (isset ($ data [0 ]) && is_array ($ data [0 ])) {
328
362
// Insert using batchInsert
329
- try {
330
- $ result = $ this ->collection ->batchInsert ($ data , $ options );
331
- } catch (MongoCursorException $ e ) {
332
- // Retry "batchInsert" in case of rediscovery latency issues
333
- // in replica set failover. Error codes 10107, 13435, and 10058
334
- // are MongoCursorException's "not master" errors.
335
- if (in_array ($ e ->getCode (), array (10107 , 13435 , 10058 ))) {
363
+ do {
364
+ try {
336
365
$ result = $ this ->collection ->batchInsert ($ data , $ options );
337
- } else {
338
- throw $ e ;
366
+ break ;
367
+ } catch (MongoCursorException $ e ) {
368
+ // Retry "save" in case of rediscovery latency issues
369
+ // in replica set failover. Error codes 10107, 13435, and 10058
370
+ // are MongoCursorException's "not master" errors.
371
+ if (in_array ($ e ->getCode (), array (10107 , 13435 , 10058 ))) {
372
+ if ($ tries === $ maxRetries ) {
373
+ throw $ e ;
374
+ } else {
375
+ $ tries ++;
376
+ continue ;
377
+ }
378
+ } else {
379
+ throw $ e ;
380
+ }
339
381
}
340
- }
382
+ } while ( $ tries <= $ maxRetries );
341
383
342
384
if (! $ result || ! ($ result === true || (bool ) $ result ['ok ' ])) {
343
385
return false ;
@@ -354,18 +396,26 @@ public function insert(array $data, $options = array())
354
396
return $ result ;
355
397
}
356
398
357
- try {
358
- $ result = $ this ->collection ->insert ($ data , $ options );
359
- } catch (MongoCursorException $ e ) {
360
- if (in_array ($ e ->getCode (), array (10107 , 13435 , 10058 ))) {
361
- // Retry "insert" in case of rediscovery latency issues
399
+ do {
400
+ try {
401
+ $ result = $ this ->collection ->insert ($ data , $ options );
402
+ break ;
403
+ } catch (MongoCursorException $ e ) {
404
+ // Retry "save" in case of rediscovery latency issues
362
405
// in replica set failover. Error codes 10107, 13435, and 10058
363
406
// are MongoCursorException's "not master" errors.
364
- $ result = $ this ->collection ->insert ($ data , $ options );
365
- } else {
366
- throw $ e ;
407
+ if (in_array ($ e ->getCode (), array (10107 , 13435 , 10058 ))) {
408
+ if ($ tries === $ maxRetries ) {
409
+ throw $ e ;
410
+ } else {
411
+ $ tries ++;
412
+ continue ;
413
+ }
414
+ } else {
415
+ throw $ e ;
416
+ }
367
417
}
368
- }
418
+ } while ( $ tries <= $ maxRetries );
369
419
370
420
if ($ result === true || (bool ) $ result ['ok ' ]) {
371
421
return $ data ['_id ' ];
@@ -401,18 +451,29 @@ public function update($values = array(), $query = null, $options = array())
401
451
402
452
isset ($ query ) || $ query = array ();
403
453
404
- try {
405
- $ result = $ this ->collection ->update ($ query , $ values , $ options );
406
- } catch (MongoCursorException $ e ) {
407
- // Retry "update" in case of rediscovery latency issues
408
- // in replica set failover. Error codes 10107, 13435, and 10058
409
- // are MongoCursorException's "not master" errors.
410
- if (in_array ($ e ->getCode (), array (10107 , 13435 , 10058 ))) {
454
+ $ maxRetries = $ this ->maxRetries ;
455
+ $ tries = 0 ;
456
+
457
+ do {
458
+ try {
411
459
$ result = $ this ->collection ->update ($ query , $ values , $ options );
412
- } else {
413
- throw $ e ;
460
+ break ;
461
+ } catch (MongoCursorException $ e ) {
462
+ // Retry "save" in case of rediscovery latency issues
463
+ // in replica set failover. Error codes 10107, 13435, and 10058
464
+ // are MongoCursorException's "not master" errors.
465
+ if (in_array ($ e ->getCode (), array (10107 , 13435 , 10058 ))) {
466
+ if ($ tries === $ maxRetries ) {
467
+ throw $ e ;
468
+ } else {
469
+ $ tries ++;
470
+ continue ;
471
+ }
472
+ } else {
473
+ throw $ e ;
474
+ }
414
475
}
415
- }
476
+ } while ( $ tries <= $ maxRetries );
416
477
417
478
return $ result === true || (bool ) $ result ['ok ' ];
418
479
}
@@ -427,18 +488,29 @@ public function update($values = array(), $query = null, $options = array())
427
488
*/
428
489
public function save (&$ document , $ options = array ())
429
490
{
430
- try {
431
- $ result = $ this ->collection ->save ($ document , $ options );
432
- } catch (MongoCursorException $ e ) {
433
- // Retry "save" in case of rediscovery latency issues
434
- // in replica set failover. Error codes 10107, 13435, and 10058
435
- // are MongoCursorException's "not master" errors.
436
- if (in_array ($ e ->getCode (), array (10107 , 13435 , 10058 ))) {
491
+ $ maxRetries = $ this ->maxRetries ;
492
+ $ tries = 0 ;
493
+
494
+ do {
495
+ try {
437
496
$ result = $ this ->collection ->save ($ document , $ options );
438
- } else {
439
- throw $ e ;
497
+ break ;
498
+ } catch (MongoCursorException $ e ) {
499
+ // Retry "save" in case of rediscovery latency issues
500
+ // in replica set failover. Error codes 10107, 13435, and 10058
501
+ // are MongoCursorException's "not master" errors.
502
+ if (in_array ($ e ->getCode (), array (10107 , 13435 , 10058 ))) {
503
+ if ($ tries === $ maxRetries ) {
504
+ throw $ e ;
505
+ } else {
506
+ $ tries ++;
507
+ continue ;
508
+ }
509
+ } else {
510
+ throw $ e ;
511
+ }
440
512
}
441
- }
513
+ } while ( $ tries <= $ maxRetries );
442
514
443
515
return $ result === true || (bool ) $ result ['ok ' ];
444
516
}
0 commit comments