Example #1
0
 /**
  * Conditionally retry a closure if it yields an exception.
  *
  * If the closure does not return successfully within the configured number
  * of retries, its first exception will be thrown.
  *
  * @param \Closure $retry
  * @return mixed
  */
 protected function retry(\Closure $retry)
 {
     $numRetries = $this->config->getRetryConnect();
     if ($numRetries < 1) {
         return $retry();
     }
     $firstException = null;
     for ($i = 0; $i <= $numRetries; $i++) {
         try {
             return $retry();
         } catch (\MongoException $e) {
             if ($firstException === null) {
                 $firstException = $e;
             }
             if ($i === $numRetries) {
                 throw $firstException;
             }
         }
     }
 }