@@ -77,23 +77,35 @@ public function retry(Closure $closure, bool $idempotent)
77
77
$ startTime = microtime (true );
78
78
$ retryCount = 0 ;
79
79
$ 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 ));
82
88
try {
83
89
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 ;
89
103
}
90
104
$ 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 " );
93
106
usleep ($ delay );
94
107
}
95
- }
96
- throw $ lastException ;
108
+ } while (true );
97
109
}
98
110
99
111
/**
@@ -111,7 +123,7 @@ protected function alwaysRetry(string $exception)
111
123
return in_array ($ exception , self ::$ alwaysRetry );
112
124
}
113
125
114
- protected function canRetry (Exception $ e , bool $ idempotent )
126
+ protected function canRetry (\ Exception $ e , bool $ idempotent )
115
127
{
116
128
return is_a ($ e , RetryableException::class) && ($ this ->alwaysRetry (get_class ($ e )) || $ idempotent );
117
129
}
0 commit comments