|
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 |
| -import java.util.ArrayList; |
| 31 | +import java.util.LinkedList; |
32 | 32 | import java.util.List;
|
33 | 33 | import java.util.concurrent.CompletableFuture;
|
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,50 @@ 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.transformDeferredContextual( ( flux, contextView ) -> flux.map( |
| 182 | + signal -> |
199 | 183 | {
|
200 |
| - startTime = currentTime; |
201 |
| - } |
| 184 | + Throwable throwable = signal.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 ); |
| 187 | + List<Throwable> errors = contextView.getOrDefault( "errors", null ); |
208 | 188 |
|
209 |
| - nextDelayMs = (long) (nextDelayMs * multiplier); |
210 |
| - errors = recordError( error, errors ); |
| 189 | + long startTime = contextView.getOrDefault( "startTime", -1L ); |
| 190 | + long nextDelayMs = contextView.getOrDefault( "nextDelayMs", initialRetryDelayMs ); |
211 | 191 |
|
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 ); |
| 192 | + if ( canRetryOn( error ) ) |
| 193 | + { |
| 194 | + long currentTime = clock.millis(); |
| 195 | + if ( startTime == -1 ) |
| 196 | + { |
| 197 | + startTime = currentTime; |
| 198 | + } |
| 199 | + |
| 200 | + long elapsedTime = currentTime - startTime; |
| 201 | + if ( elapsedTime < maxRetryTimeMs ) |
| 202 | + { |
| 203 | + long delayWithJitterMs = computeDelayWithJitter( nextDelayMs ); |
| 204 | + log.warn( "Reactive transaction failed and is scheduled to retry in " + delayWithJitterMs + "ms", error ); |
| 205 | + |
| 206 | + nextDelayMs = (long) (nextDelayMs * multiplier); |
| 207 | + errors = recordError( error, errors ); |
| 208 | + |
| 209 | + // retry on netty event loop thread |
| 210 | + EventExecutor eventExecutor = eventExecutorGroup.next(); |
| 211 | + Context context = Context.of( |
| 212 | + "errors", errors, |
| 213 | + "startTime", startTime, |
| 214 | + "nextDelayMs", nextDelayMs |
| 215 | + ); |
| 216 | + return Mono.just( context ).delayElement( Duration.ofMillis( delayWithJitterMs ), Schedulers.fromExecutorService( eventExecutor ) ); |
| 217 | + } |
| 218 | + } |
| 219 | + addSuppressed( throwable, errors ); |
219 | 220 |
|
220 |
| - return Mono.error( throwable ); |
221 |
| - } ); |
| 221 | + return Mono.error( throwable ); |
| 222 | + } ) ) ); |
222 | 223 | }
|
223 | 224 |
|
224 | 225 | private <T> void executeWorkInEventLoop( CompletableFuture<T> resultFuture, Supplier<CompletionStage<T>> work )
|
@@ -373,7 +374,7 @@ private static List<Throwable> recordError( Throwable error, List<Throwable> err
|
373 | 374 | {
|
374 | 375 | if ( errors == null )
|
375 | 376 | {
|
376 |
| - errors = new ArrayList<>(); |
| 377 | + errors = new LinkedList<>(); |
377 | 378 | }
|
378 | 379 | errors.add( error );
|
379 | 380 | return errors;
|
|
0 commit comments