|
25 | 25 | import reactor.core.publisher.Mono; |
26 | 26 | import reactor.core.scheduler.Schedulers; |
27 | 27 | import reactor.util.context.Context; |
28 | | -import reactor.util.function.Tuples; |
| 28 | +import reactor.util.retry.Retry; |
29 | 29 |
|
30 | 30 | import java.time.Duration; |
31 | 31 | import java.util.ArrayList; |
|
34 | 34 | import java.util.concurrent.CompletionStage; |
35 | 35 | import java.util.concurrent.ThreadLocalRandom; |
36 | 36 | import java.util.concurrent.TimeUnit; |
37 | | -import java.util.function.Function; |
38 | 37 | import java.util.function.Supplier; |
39 | 38 |
|
40 | 39 | import org.neo4j.driver.Logger; |
@@ -145,7 +144,7 @@ public <T> CompletionStage<T> retryAsync( Supplier<CompletionStage<T>> work ) |
145 | 144 | @Override |
146 | 145 | public <T> Publisher<T> retryRx( Publisher<T> work ) |
147 | 146 | { |
148 | | - return Flux.from( work ).retryWhen( retryRxCondition() ); |
| 147 | + return Flux.from( work ).retryWhen( exponentialBackoffRetryRx() ); |
149 | 148 | } |
150 | 149 |
|
151 | 150 | protected boolean canRetryOn( Throwable error ) |
@@ -177,48 +176,46 @@ private static Throwable extractPossibleTerminationCause( Throwable error ) |
177 | 176 | return error; |
178 | 177 | } |
179 | 178 |
|
180 | | - private Function<Flux<Throwable>,Publisher<Context>> retryRxCondition() |
| 179 | + private Retry exponentialBackoffRetryRx() |
181 | 180 | { |
182 | | - return errorCurrentAttempt -> errorCurrentAttempt.flatMap( e -> Mono.subscriberContext().map( ctx -> Tuples.of( e, ctx ) ) ).flatMap( t2 -> |
183 | | - { |
184 | | - |
185 | | - Throwable throwable = t2.getT1(); |
186 | | - Throwable error = extractPossibleTerminationCause( throwable ); |
187 | | - |
188 | | - Context ctx = t2.getT2(); |
189 | | - |
190 | | - List<Throwable> errors = ctx.getOrDefault( "errors", null ); |
191 | | - |
192 | | - long startTime = ctx.getOrDefault( "startTime", -1L ); |
193 | | - long nextDelayMs = ctx.getOrDefault( "nextDelayMs", initialRetryDelayMs ); |
194 | | - |
195 | | - if ( canRetryOn( error ) ) |
196 | | - { |
197 | | - long currentTime = clock.millis(); |
198 | | - if ( startTime == -1 ) |
| 181 | + return Retry.from( retrySignals -> retrySignals.flatMap( retrySignal -> Mono.deferContextual( |
| 182 | + contextView -> |
199 | 183 | { |
200 | | - startTime = currentTime; |
201 | | - } |
| 184 | + Throwable throwable = retrySignal.failure(); |
| 185 | + Throwable error = extractPossibleTerminationCause( throwable ); |
202 | 186 |
|
203 | | - long elapsedTime = currentTime - startTime; |
204 | | - if ( elapsedTime < maxRetryTimeMs ) |
205 | | - { |
206 | | - long delayWithJitterMs = computeDelayWithJitter( nextDelayMs ); |
207 | | - log.warn( "Reactive transaction failed and is scheduled to retry in " + delayWithJitterMs + "ms", error ); |
208 | | - |
209 | | - nextDelayMs = (long) (nextDelayMs * multiplier); |
210 | | - errors = recordError( error, errors ); |
| 187 | + List<Throwable> errors = contextView.getOrDefault( "errors", null ); |
211 | 188 |
|
212 | | - // retry on netty event loop thread |
213 | | - EventExecutor eventExecutor = eventExecutorGroup.next(); |
214 | | - return Mono.just( ctx.put( "errors", errors ).put( "startTime", startTime ).put( "nextDelayMs", nextDelayMs ) ).delayElement( |
215 | | - Duration.ofMillis( delayWithJitterMs ), Schedulers.fromExecutorService( eventExecutor ) ); |
216 | | - } |
217 | | - } |
218 | | - addSuppressed( throwable, errors ); |
| 189 | + if ( canRetryOn( error ) ) |
| 190 | + { |
| 191 | + long currentTime = clock.millis(); |
| 192 | + |
| 193 | + long startTime = contextView.getOrDefault( "startTime", currentTime ); |
| 194 | + long nextDelayMs = contextView.getOrDefault( "nextDelayMs", initialRetryDelayMs ); |
| 195 | + |
| 196 | + long elapsedTime = currentTime - startTime; |
| 197 | + if ( elapsedTime < maxRetryTimeMs ) |
| 198 | + { |
| 199 | + long delayWithJitterMs = computeDelayWithJitter( nextDelayMs ); |
| 200 | + log.warn( "Reactive transaction failed and is scheduled to retry in " + delayWithJitterMs + "ms", error ); |
| 201 | + |
| 202 | + nextDelayMs = (long) (nextDelayMs * multiplier); |
| 203 | + errors = recordError( error, errors ); |
| 204 | + |
| 205 | + // retry on netty event loop thread |
| 206 | + EventExecutor eventExecutor = eventExecutorGroup.next(); |
| 207 | + Context context = Context.of( |
| 208 | + "errors", errors, |
| 209 | + "startTime", startTime, |
| 210 | + "nextDelayMs", nextDelayMs |
| 211 | + ); |
| 212 | + return Mono.just( context ).delayElement( Duration.ofMillis( delayWithJitterMs ), Schedulers.fromExecutorService( eventExecutor ) ); |
| 213 | + } |
| 214 | + } |
| 215 | + addSuppressed( throwable, errors ); |
219 | 216 |
|
220 | | - return Mono.error( throwable ); |
221 | | - } ); |
| 217 | + return Mono.error( throwable ); |
| 218 | + } ) ) ); |
222 | 219 | } |
223 | 220 |
|
224 | 221 | private <T> void executeWorkInEventLoop( CompletableFuture<T> resultFuture, Supplier<CompletionStage<T>> work ) |
|
0 commit comments