From b69bd6cdd894e29ea53ce00e02ea4ad6172e1661 Mon Sep 17 00:00:00 2001 From: asahi Date: Mon, 14 Apr 2025 12:45:42 +0800 Subject: [PATCH] =?UTF-8?q?doc:=20=E9=98=85=E8=AF=BBtransient=20errors?= =?UTF-8?q?=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spring/webflux/Reactor.md | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/spring/webflux/Reactor.md b/spring/webflux/Reactor.md index c77210c..523a1f6 100644 --- a/spring/webflux/Reactor.md +++ b/spring/webflux/Reactor.md @@ -62,6 +62,9 @@ - [retry原理](#retry原理) - [retryWhen](#retrywhen) - [retry helper](#retry-helper) + - [retrying with transient errors](#retrying-with-transient-errors) + - [handling Exceptions in operators or functions](#handling-exceptions-in-operators-or-functions) +>>>>>>> 8d41980 (doc: 阅读transient errors文档) # Reactor ## Reactive Programming @@ -967,4 +970,25 @@ project reactor提供了`Retry` helper,如`RetrySpec`和`RetryBackoffSpec`, - `doBeforeRetry`和`doAfterRetry`:针对retrytrigger执行side effect - > `doBeforeRetry()`方法触发在delay发生之前,而`doAfterRetry()`触发在delay之后 - `onRetryExhaustedThrow(BiFunction)`:在重试数量达到上限后,通过`onRetryExhaustedThrow(BiFunction)`来自定义异常。 - - 通常情况下,当重试数量达到上限后,自定义异常类型通过`Exceptions.retryExhausted(…​)`方法来构建,其返回的异常类型为`RetryExhaustedException`,可以通过`Exceptions.isRetryExhausted(Throwable)`方法来进行区分 \ No newline at end of file + - 通常情况下,当重试数量达到上限后,自定义异常类型通过`Exceptions.retryExhausted(…​)`方法来构建,其返回的异常类型为`RetryExhaustedException`,可以通过`Exceptions.isRetryExhausted(Throwable)`方法来进行区分 +##### retrying with transient errors +在`RetrySignal`中存在如下两个方法: +- `totalRetriesInARow()`:每当`error is recovered`(retry attempt导致了`onNext`而不是`onError`)时,index都会被设置为0 +- `totalRetries()`:`totalRetries()`方法返回的值是单调递增的,并不会被重置 + +当使用`RetrySpec`和`RetryBackoffSpec`时,可以通过`transientErrors(true)`方法来令策略使用`totalRetriesInARow()`处理transient error。 + +示例如下所示: +```java +AtomicInteger errorCount = new AtomicInteger(); +Flux transientFlux = httpRequest.get() + .doOnError(e -> errorCount.incrementAndGet()); + +transientFlux.retryWhen(Retry.max(2).transientErrors(true)) + .blockLast(); +assertThat(errorCount).hasValue(6); +``` + +> `transientErrors`主要是为了处理周期性发生异常,异常能够重试成功,并且发生异常后一段时间平稳运行的场景 + +#### handling Exceptions in operators or functions