@@ -77,23 +77,35 @@ public function retry(Closure $closure, bool $idempotent)
7777 $ startTime = microtime (true );
7878 $ retryCount = 0 ;
7979 $ lastException = null ;
80- while (microtime (true ) < $ startTime + $ this ->timeoutMs / 1000 ) {
81- $ this ->logger ->debug ("YDB: Run user function. Retry count: $ retryCount. s: " .(microtime (true ) - $ startTime ));
80+ if (is_null ($ this ->timeoutMs )) {
81+ $ deadline = PHP_INT_MAX ;
82+ } else {
83+ $ deadline = $ startTime + $ this ->timeoutMs / 1000 ;
84+ }
85+ $ this ->logger ->debug ("YDB: begin retry function. Deadline: $ deadline " );
86+ do {
87+ $ this ->logger ->debug ("YDB: Run user function. Retry count: $ retryCount. s: " . (microtime (true ) - $ startTime ));
8288 try {
8389 return $ closure ();
84- } catch (Exception $ e ) {
85- $ this ->logger ->warning ("YDB: Received exception: " .$ e ->getMessage ());
86- if (!$ this ->canRetry ($ e , $ idempotent )){
87- $ lastException = $ e ;
88- break ;
90+ } catch (\Exception $ e ) {
91+ $ this ->logger ->debug ("YDB: Received exception: " . $ e ->getMessage ());
92+ $ lastException = $ e ;
93+ if (!$ this ->canRetry ($ e , $ idempotent )) {
94+ $ this ->logger ->error ("YDB: Received non-retryable exception in retry. ms: "
95+ . ((microtime (true ) - $ startTime ) * 1000 ) . "Retry count: $ retryCount " );
96+ throw $ lastException ;
97+ }
98+ $ delay = $ this ->retryDelay ($ retryCount , $ this ->backoffType (get_class ($ e ))) * 1000 ;
99+ if (microtime (true ) + $ delay / 1000000 > $ deadline ) {
100+ $ this ->logger ->error ("YDB: Timeout retry function. ms: "
101+ . ((microtime (true ) - $ startTime ) * 1000 ) . "Retry count: $ retryCount " );
102+ throw $ lastException ;
89103 }
90104 $ retryCount ++;
91- $ lastException = $ e ;
92- $ delay = $ this ->retryDelay ($ retryCount , $ this ->backoffType (get_class ($ e )))*1000 ;
105+ $ this ->logger ->debug ("YDB: Sleep $ delay microseconds before retry " );
93106 usleep ($ delay );
94107 }
95- }
96- throw $ lastException ;
108+ } while (true );
97109 }
98110
99111 /**
@@ -111,7 +123,7 @@ protected function alwaysRetry(string $exception)
111123 return in_array ($ exception , self ::$ alwaysRetry );
112124 }
113125
114- protected function canRetry (Exception $ e , bool $ idempotent )
126+ protected function canRetry (\ Exception $ e , bool $ idempotent )
115127 {
116128 return is_a ($ e , RetryableException::class) && ($ this ->alwaysRetry (get_class ($ e )) || $ idempotent );
117129 }
0 commit comments