@@ -77,27 +77,35 @@ public function retry(Closure $closure, bool $idempotent)
77
77
$ startTime = microtime (true );
78
78
$ retryCount = 0 ;
79
79
$ lastException = null ;
80
- $ deadline = is_null ($ this ->timeoutMs ) ? PHP_INT_MAX : $ startTime + $ this ->timeoutMs / 1000 ;
80
+ if (is_null ($ this ->timeoutMs )) {
81
+ $ deadline = PHP_INT_MAX ;
82
+ } else {
83
+ $ deadline = $ startTime + $ this ->timeoutMs / 1000 ;
84
+ }
81
85
$ this ->logger ->debug ("YDB: begin retry function. Deadline: $ deadline " );
82
86
do {
83
- $ this ->logger ->debug ("YDB: Run user function. Retry count: $ retryCount. s: " . (microtime (true ) - $ startTime ));
87
+ $ this ->logger ->debug ("YDB: Run user function. Retry count: $ retryCount. s: " . (microtime (true ) - $ startTime ));
84
88
try {
85
89
return $ closure ();
86
90
} catch (\Exception $ e ) {
87
- $ this ->logger ->debug ("YDB: Received exception: " . $ e ->getMessage ());
91
+ $ this ->logger ->debug ("YDB: Received exception: " . $ e ->getMessage ());
88
92
$ lastException = $ e ;
89
- if (!$ this ->canRetry ($ e , $ idempotent )){
90
- break ;
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 ;
91
103
}
92
104
$ retryCount ++;
93
- $ delay = $ this ->retryDelay ($ retryCount , $ this ->backoffType (get_class ($ e )))*1000 ;
94
105
$ this ->logger ->debug ("YDB: Sleep $ delay microseconds before retry " );
95
106
usleep ($ delay );
96
107
}
97
- } while (microtime (true ) < $ deadline );
98
- $ this ->logger ->error ("YDB: Timeout retry function. ms: "
99
- .((microtime (true )-$ startTime )*1000 ). "Retry count: $ retryCount " );
100
- throw $ lastException ;
108
+ } while (true );
101
109
}
102
110
103
111
/**
0 commit comments