Skip to content

Commit 9eb9165

Browse files
committed
add docs and unit-test
Signed-off-by: jiangyuan <[email protected]>
1 parent 350c9cd commit 9eb9165

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

docs/modules/ROOT/pages/spring-cloud-gateway/gatewayfilter-factories/retry-factory.adoc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ The `Retry` `GatewayFilter` factory supports the following parameters:
1212
Retries are performed after a backoff interval of `firstBackoff * (factor ^ n)`, where `n` is the iteration.
1313
If `maxBackoff` is configured, the maximum backoff applied is limited to `maxBackoff`.
1414
If `basedOnPreviousValue` is true, the backoff is calculated by using `prevBackoff * factor`.
15+
* `jitter`: The configured random jitter for the retries.
16+
Generating a backoff between `[backoff - backoff*randomFactor, backoff + backoff*randomFactor]`
17+
* `timeout`: The configured timeout for the retries.
1518

1619
The following defaults are configured for `Retry` filter, if enabled:
1720

@@ -20,6 +23,8 @@ The following defaults are configured for `Retry` filter, if enabled:
2023
* `methods`: GET method
2124
* `exceptions`: `IOException` and `TimeoutException`
2225
* `backoff`: disabled
26+
* `jitter`: disabled
27+
* `timeout`: unlimited
2328

2429
The following listing configures a Retry `GatewayFilter`:
2530

@@ -45,6 +50,9 @@ spring:
4550
maxBackoff: 50ms
4651
factor: 2
4752
basedOnPreviousValue: false
53+
jitter:
54+
randomFactor: 0.5
55+
timeout: 100ms
4856
----
4957

5058
NOTE: When using the retry filter with a `forward:` prefixed URL, the target endpoint should be written carefully so that, in case of an error, it does not do anything that could result in a response being sent to the client and committed.
@@ -77,10 +85,13 @@ spring:
7785
maxBackoff: 50ms
7886
factor: 2
7987
basedOnPreviousValue: false
88+
jitter:
89+
randomFactor: 0.5
90+
timeout: 100ms
8091
8192
- id: retryshortcut_route
8293
uri: https://example.org
8394
filters:
84-
- Retry=3,INTERNAL_SERVER_ERROR,GET,10ms,50ms,2,false
95+
- Retry=3,INTERNAL_SERVER_ERROR,GET,10ms,50ms,2,false,0.5,100ms
8596
----
8697

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/RetryGatewayFilterFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,8 @@ public static class JitterConfig {
492492
private double randomFactor = 0.5;
493493

494494
public void validate() {
495-
Assert.isTrue(randomFactor >= 0 && randomFactor <= 1, "random factor must be between 0 and 1 (default 0.5)");
495+
Assert.isTrue(randomFactor >= 0 && randomFactor <= 1,
496+
"random factor must be between 0 and 1 (default 0.5)");
496497
}
497498

498499
public JitterConfig() {
@@ -509,6 +510,7 @@ public double getRandomFactor() {
509510
public void setRandomFactor(double randomFactor) {
510511
this.randomFactor = randomFactor;
511512
}
513+
512514
}
513515

514516
}

spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/factory/RetryGatewayFilterFactoryIntegrationTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,18 @@ public void retryWithBackoff() {
109109
// @formatter:on
110110
}
111111

112+
@Test
113+
public void retryWithBackoffJitterTimeout() {
114+
// @formatter:off
115+
testClient.get()
116+
.uri("/retry?key=retry-with-backoff-jitter-timeout&count=3")
117+
.header(HttpHeaders.HOST, "www.retrywithbackoffjittertimeout.org")
118+
.exchange()
119+
.expectStatus().isOk()
120+
.expectHeader().value("X-Retry-Count", CoreMatchers.equalTo("3"));
121+
// @formatter:on
122+
}
123+
112124
@Test
113125
public void retryFilterGetJavaDsl() {
114126
testClient.get()

spring-cloud-gateway-server/src/test/resources/application-retrytests.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,22 @@ spring:
2929
- name: Retry
3030
args:
3131
methods: GET,POST
32+
# =====================================
33+
- id: retry_with_backoff_jitter_timeout_test
34+
uri: ${test.uri}
35+
predicates:
36+
- Host=**.retrywithbackoffjittertimeout.org
37+
filters:
38+
- name: Retry
39+
args:
40+
retries: 3
41+
statuses: INTERNAL_SERVER_ERROR
42+
methods: GET
43+
backoff:
44+
firstBackoff: 10ms
45+
maxBackoff: 50ms
46+
factor: 2
47+
basedOnPreviousValue: false
48+
jitter:
49+
randomFactor: 0.5
50+
timeout: 200ms

0 commit comments

Comments
 (0)