@@ -21,12 +21,14 @@ class Retry
2121 protected $ slowBackOff ;
2222
2323 protected $ fastBackOff ;
24+ protected $ noBackOff ;
2425
2526 public function __construct ()
2627 {
2728 $ this ->timeoutMs = 2000 ;
2829 $ this ->fastBackOff = new Backoff (6 , 5 );
2930 $ this ->slowBackOff = new Backoff (6 , 1000 );
31+ $ this ->noBackOff = new Backoff (0 , 0 );
3032 }
3133
3234 protected function retryDelay (int $ retryCount , Backoff $ backoff )
@@ -80,56 +82,61 @@ public function retry(Closure $closure, bool $idempotent)
8082 while (microtime (true ) < $ startTime + $ this ->timeoutMs / 1000 ) {
8183 try {
8284 return $ closure ();
83- } catch (RetryableException $ e ) {
84- if (isset ( self :: $ idempotentOnly [ get_class ($ e)]) && $ idempotent ) {
85+ } catch (Exception $ e ) {
86+ if (! $ this -> canRetry ($ e, $ idempotent )) {
8587 throw $ e ;
8688 }
8789 $ retryCount ++;
8890 $ this ->retryDelay ($ retryCount , $ this ->backoffType ($ e ));
8991 $ lastException = $ e ;
90- } catch (Exception $ e ) {
91- throw $ e ;
9292 }
9393 }
9494 throw $ lastException ;
9595 }
9696
9797 /**
98- * @param RetryableException $e
98+ * @param string $e
9999 * @return Backoff
100100 */
101- protected function backoffType (RetryableException $ e ): Backoff
101+ protected function backoffType (string $ e ): Backoff
102102 {
103- return $ this ->fastBackOff ;
104- // if ($e instanceof AbortedException) {
105- // return $this->fastBackOff;
106- // } elseif ($e instanceof BadSessionException) {
107- // return $this->fastBackOff;
108- // } elseif ($e instanceof SessionBusyException) {
109- // return $this->fastBackOff;
110- // } elseif ($e instanceof UndeterminedException) {
111- // return $this->fastBackOff;
112- // } elseif ($e instanceof UnavailableException) {
113- // return $this->fastBackOff;
114- // } elseif ($e instanceof UndeterminedException) {
115- // return $this->fastBackOff;
116- // } elseif ($e instanceof DeadlineExceededException) {
117- // return $this->fastBackOff;
118- // } else {
119- // return $this->slowBackOff;
120- // }
103+ return in_array ($ e , self ::$ immediatelyBackoff )?$ this ->noBackOff :
104+ (in_array ($ e , self ::$ fastBackoff )?$ this ->fastBackOff :$ this ->slowBackOff );
105+ }
106+
107+ protected function alwaysRetry (string $ exception ){
108+ return in_array ($ exception , self ::$ alwaysRetry );
121109 }
122110
123- private static $ idempotentOnly = [
111+ protected function canRetry (Exception $ e , bool $ idempotent )
112+ {
113+ return is_a ($ e , RetryableException::class)&&($ this ->alwaysRetry (get_class ($ e )) || $ idempotent );
114+ }
115+ private static $ immediatelyBackoff = [
116+ \YdbPlatform \Ydb \Exceptions \Grpc \AbortedException::class,
117+ \YdbPlatform \Ydb \Exceptions \Ydb \BadSessionException::class,
118+ ];
119+
120+ private static $ fastBackoff = [
124121 \YdbPlatform \Ydb \Exceptions \Grpc \CanceledException::class,
125122 \YdbPlatform \Ydb \Exceptions \Grpc \DeadlineExceededException::class,
126123 \YdbPlatform \Ydb \Exceptions \Grpc \InternalException::class,
127124 \YdbPlatform \Ydb \Exceptions \Grpc \UnavailableException::class,
128- \YdbPlatform \Ydb \Exceptions \Ydb \UndeterminedException::class
125+ \YdbPlatform \Ydb \Exceptions \Ydb \AbortedException::class,
126+ \YdbPlatform \Ydb \Exceptions \Ydb \UnavailableException::class,
127+ \YdbPlatform \Ydb \Exceptions \Ydb \CancelledException::class,
128+ \YdbPlatform \Ydb \Exceptions \Ydb \UndeterminedException::class,
129+ \YdbPlatform \Ydb \Exceptions \Ydb \SessionBusyException::class,
129130 ];
130131
131- private static $ fastBackoff = [
132-
132+ private static $ alwaysRetry = [
133+ \YdbPlatform \Ydb \Exceptions \Grpc \ResourceExhaustedException::class,
134+ \YdbPlatform \Ydb \Exceptions \Grpc \AbortedException::class,
135+ \YdbPlatform \Ydb \Exceptions \Ydb \AbortedException::class,
136+ \YdbPlatform \Ydb \Exceptions \Ydb \UnavailableException::class,
137+ \YdbPlatform \Ydb \Exceptions \Ydb \OverloadedException::class,
138+ \YdbPlatform \Ydb \Exceptions \Ydb \BadSessionException::class,
139+ \YdbPlatform \Ydb \Exceptions \Ydb \SessionBusyException::class,
133140 ];
134141
135142}
0 commit comments