Spencer Gibb
7 years ago
6 changed files with 157 additions and 0 deletions
@ -0,0 +1,46 @@
@@ -0,0 +1,46 @@
|
||||
/* |
||||
* Copyright 2013-2018 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
|
||||
package org.springframework.cloud.gateway.filter.factory; |
||||
|
||||
import java.util.function.Predicate; |
||||
|
||||
import org.springframework.http.HttpMethod; |
||||
import reactor.retry.DefaultRepeat; |
||||
import reactor.retry.Repeat; |
||||
import reactor.retry.RepeatContext; |
||||
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilter; |
||||
import org.springframework.tuple.Tuple; |
||||
import org.springframework.web.server.ServerWebExchange; |
||||
|
||||
public class RetryGatewayFilterFactory implements GatewayFilterFactory { |
||||
@Override |
||||
public GatewayFilter apply(Tuple args) { |
||||
return (exchange, chain) -> { |
||||
Predicate<? super RepeatContext<Object>> predicate = context -> { |
||||
ServerWebExchange ex = (ServerWebExchange) context.applicationContext(); |
||||
boolean retryableStatusCode = ex.getResponse().getStatusCode().is5xxServerError(); |
||||
boolean retryableMethod = ex.getRequest().getMethod().equals(HttpMethod.GET); |
||||
return retryableMethod && retryableStatusCode; |
||||
}; |
||||
Repeat<Object> repeat = DefaultRepeat.create(predicate, 4) |
||||
.withApplicationContext(exchange); |
||||
return chain.filter(exchange).repeatWhen(repeat).next(); |
||||
}; |
||||
} |
||||
} |
@ -0,0 +1,88 @@
@@ -0,0 +1,88 @@
|
||||
/* |
||||
* Copyright 2013-2018 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
* |
||||
*/ |
||||
|
||||
package org.springframework.cloud.gateway.filter.factory; |
||||
|
||||
import java.util.concurrent.ConcurrentHashMap; |
||||
import java.util.concurrent.atomic.AtomicInteger; |
||||
|
||||
import org.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
import org.junit.Test; |
||||
import org.junit.runner.RunWith; |
||||
|
||||
import org.springframework.boot.SpringBootConfiguration; |
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
||||
import org.springframework.boot.test.context.SpringBootTest; |
||||
import org.springframework.cloud.gateway.test.BaseWebClientTests; |
||||
import org.springframework.context.annotation.Import; |
||||
import org.springframework.http.server.reactive.ServerHttpRequest; |
||||
import org.springframework.test.annotation.DirtiesContext; |
||||
import org.springframework.test.context.junit4.SpringRunner; |
||||
import org.springframework.web.bind.annotation.PathVariable; |
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
import org.springframework.web.bind.annotation.RequestParam; |
||||
import org.springframework.web.bind.annotation.RestController; |
||||
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; |
||||
|
||||
@RunWith(SpringRunner.class) |
||||
@SpringBootTest(webEnvironment = RANDOM_PORT) |
||||
@DirtiesContext |
||||
public class RetryGatewayFilterFactoryIntegrationTests extends BaseWebClientTests { |
||||
|
||||
@Test |
||||
public void retryFilterGet() { |
||||
testClient.get() |
||||
.uri("/retry?key=get") |
||||
.exchange() |
||||
.expectStatus().isOk() |
||||
.expectBody(String.class).isEqualTo("3"); |
||||
} |
||||
|
||||
@Test |
||||
//TODO: support post
|
||||
public void retryFilterPost() { |
||||
testClient.post() |
||||
.uri("/retry?key=post") |
||||
.exchange() |
||||
.expectStatus().is5xxServerError(); |
||||
// .expectBody(String.class).isEqualTo("3");
|
||||
} |
||||
|
||||
@RestController |
||||
@EnableAutoConfiguration |
||||
@SpringBootConfiguration |
||||
@Import(DefaultTestConfig.class) |
||||
public static class TestConfig { |
||||
Log log = LogFactory.getLog(getClass()); |
||||
|
||||
ConcurrentHashMap<String, AtomicInteger> map = new ConcurrentHashMap<>(); |
||||
|
||||
@RequestMapping("/httpbin/retry") |
||||
public String retry(@RequestParam("key") String key) { |
||||
AtomicInteger count = map.computeIfAbsent(key, s -> new AtomicInteger()); |
||||
int i = count.incrementAndGet(); |
||||
log.warn("Retry count: "+i); |
||||
if (i < 3) { |
||||
throw new RuntimeException("temporarily broken"); |
||||
} |
||||
return String.valueOf(i); |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue