-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #344 from zalando/feature/retry-after
Added support for Retry-After
- Loading branch information
Showing
10 changed files
with
278 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
riptide-failsafe/src/main/java/org/zalando/riptide/failsafe/RetryAfterDelayFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package org.zalando.riptide.failsafe; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import net.jodah.failsafe.ExecutionContext; | ||
import net.jodah.failsafe.RetryPolicy.DelayFunction; | ||
import net.jodah.failsafe.util.Duration; | ||
import org.zalando.riptide.HttpResponseException; | ||
|
||
import javax.annotation.Nullable; | ||
import java.time.Clock; | ||
import java.time.Instant; | ||
import java.time.format.DateTimeParseException; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.regex.Pattern; | ||
|
||
import static java.lang.Long.parseLong; | ||
import static java.time.Duration.between; | ||
import static java.time.Instant.now; | ||
import static java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME; | ||
|
||
/** | ||
* @see <a href="https://tools.ietf.org/html/rfc7231#section-7.1.3">RFC 7231, section 7.1.3: Retry-After</a> | ||
*/ | ||
@Slf4j | ||
public final class RetryAfterDelayFunction implements DelayFunction<Object, Throwable> { | ||
|
||
private final Pattern digit = Pattern.compile("\\d"); | ||
|
||
private final Clock clock; | ||
|
||
public RetryAfterDelayFunction(final Clock clock) { | ||
this.clock = clock; | ||
} | ||
|
||
@Override | ||
public Duration computeDelay(final Object result, final Throwable failure, final ExecutionContext context) { | ||
return failure instanceof HttpResponseException ? computeDelay((HttpResponseException) failure) : null; | ||
} | ||
|
||
@Nullable | ||
private Duration computeDelay(final HttpResponseException failure) { | ||
@Nullable final String retryAfter = failure.getResponseHeaders().getFirst("Retry-After"); | ||
return retryAfter == null ? null : toDuration(parseDelay(retryAfter)); | ||
} | ||
|
||
/** | ||
* The value of this field can be either an HTTP-date or a number of seconds to delay after the response | ||
* is received. | ||
* | ||
* Retry-After = HTTP-date / delay-seconds | ||
* | ||
* @param retryAfter non-null header value | ||
* @return the parsed delay in seconds | ||
*/ | ||
@Nullable | ||
private Long parseDelay(final String retryAfter) { | ||
return onlyDigits(retryAfter) ? | ||
parseSeconds(retryAfter) : | ||
secondsUntil(parseDate(retryAfter)); | ||
} | ||
|
||
private boolean onlyDigits(final String s) { | ||
return digit.matcher(s).matches(); | ||
} | ||
|
||
private Long parseSeconds(final String retryAfter) { | ||
return parseLong(retryAfter); | ||
} | ||
|
||
@Nullable | ||
private Instant parseDate(final String retryAfter) { | ||
try { | ||
return Instant.from(RFC_1123_DATE_TIME.parse(retryAfter)); | ||
} catch (final DateTimeParseException e) { | ||
log.warn("Received invalid 'Retry-After' header [{}]; will ignore it", retryAfter); | ||
return null; | ||
} | ||
} | ||
|
||
@Nullable | ||
private Long secondsUntil(@Nullable final Instant end) { | ||
return end == null ? null : between(now(clock), end).getSeconds(); | ||
} | ||
|
||
@Nullable | ||
private Duration toDuration(@Nullable final Long seconds) { | ||
return seconds == null ? null : new Duration(seconds, TimeUnit.SECONDS); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
riptide-failsafe/src/test/java/org/zalando/riptide/failsafe/RetryAfterDelayFunctionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package org.zalando.riptide.failsafe; | ||
|
||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.github.restdriver.clientdriver.ClientDriverRule; | ||
import net.jodah.failsafe.CircuitBreaker; | ||
import net.jodah.failsafe.RetryPolicy; | ||
import org.apache.http.client.config.RequestConfig; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClientBuilder; | ||
import org.junit.After; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; | ||
import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor; | ||
import org.zalando.riptide.Http; | ||
import org.zalando.riptide.httpclient.RestAsyncClientHttpRequestFactory; | ||
|
||
import java.io.IOException; | ||
import java.time.Clock; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static com.github.restdriver.clientdriver.RestClientDriver.giveEmptyResponse; | ||
import static com.github.restdriver.clientdriver.RestClientDriver.onRequestTo; | ||
import static java.time.Instant.parse; | ||
import static java.time.ZoneOffset.UTC; | ||
import static java.util.concurrent.Executors.newSingleThreadExecutor; | ||
import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor; | ||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
import static org.springframework.http.HttpStatus.Series.SUCCESSFUL; | ||
import static org.zalando.riptide.Bindings.anySeries; | ||
import static org.zalando.riptide.Bindings.on; | ||
import static org.zalando.riptide.Navigators.series; | ||
import static org.zalando.riptide.Navigators.status; | ||
import static org.zalando.riptide.PassRoute.pass; | ||
import static org.zalando.riptide.failsafe.RetryRoute.retry; | ||
|
||
public class RetryAfterDelayFunctionTest { | ||
|
||
@Rule | ||
public final ClientDriverRule driver = new ClientDriverRule(); | ||
|
||
private final CloseableHttpClient client = HttpClientBuilder.create() | ||
.setDefaultRequestConfig(RequestConfig.custom() | ||
.setSocketTimeout(1000) | ||
.build()) | ||
.build(); | ||
|
||
private final Clock clock = Clock.fixed(parse("2018-04-11T22:34:27Z"), UTC); | ||
|
||
private final Http unit = Http.builder() | ||
.baseUrl(driver.getBaseUrl()) | ||
.requestFactory(new RestAsyncClientHttpRequestFactory(client, | ||
new ConcurrentTaskExecutor(newSingleThreadExecutor()))) | ||
.converter(createJsonConverter()) | ||
.plugin(new FailsafePlugin(newSingleThreadScheduledExecutor()) | ||
.withRetryPolicy(new RetryPolicy() | ||
.withDelay(2, SECONDS) | ||
.withDelay(new RetryAfterDelayFunction(clock)) | ||
.withMaxRetries(4)) | ||
.withCircuitBreaker(new CircuitBreaker() | ||
.withFailureThreshold(3, 10) | ||
.withSuccessThreshold(5) | ||
.withDelay(1, TimeUnit.MINUTES))) | ||
.build(); | ||
|
||
private static MappingJackson2HttpMessageConverter createJsonConverter() { | ||
final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); | ||
converter.setObjectMapper(createObjectMapper()); | ||
return converter; | ||
} | ||
|
||
private static ObjectMapper createObjectMapper() { | ||
return new ObjectMapper().findAndRegisterModules() | ||
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); | ||
} | ||
|
||
@After | ||
public void tearDown() throws IOException { | ||
client.close(); | ||
} | ||
|
||
@Test | ||
public void shouldRetryWithoutDynamicDelay() { | ||
driver.addExpectation(onRequestTo("/baz"), giveEmptyResponse().withStatus(503)); | ||
driver.addExpectation(onRequestTo("/baz"), giveEmptyResponse()); | ||
|
||
unit.get("/baz") | ||
.dispatch(series(), | ||
on(SUCCESSFUL).call(pass()), | ||
anySeries().dispatch(status(), | ||
on(HttpStatus.SERVICE_UNAVAILABLE).call(retry()))) | ||
.join(); | ||
} | ||
|
||
@Test | ||
public void shouldIgnoreDynamicDelayOnInvalidFormat() { | ||
driver.addExpectation(onRequestTo("/baz"), giveEmptyResponse().withStatus(503) | ||
.withHeader("Retry-After", "2018-04-11T22:34:28Z")); // should've been HTTP date | ||
driver.addExpectation(onRequestTo("/baz"), giveEmptyResponse()); | ||
|
||
unit.get("/baz") | ||
.dispatch(series(), | ||
on(SUCCESSFUL).call(pass()), | ||
anySeries().dispatch(status(), | ||
on(HttpStatus.SERVICE_UNAVAILABLE).call(retry()))) | ||
.join(); | ||
} | ||
|
||
@Test(timeout = 1500) | ||
public void shouldRetryOnDemandWithDynamicDelay() { | ||
driver.addExpectation(onRequestTo("/baz"), giveEmptyResponse().withStatus(503) | ||
.withHeader("Retry-After", "1")); | ||
driver.addExpectation(onRequestTo("/baz"), giveEmptyResponse()); | ||
|
||
unit.get("/baz") | ||
.dispatch(series(), | ||
on(SUCCESSFUL).call(pass()), | ||
anySeries().dispatch(status(), | ||
on(HttpStatus.SERVICE_UNAVAILABLE).call(retry()))) | ||
.join(); | ||
} | ||
|
||
@Test(timeout = 1500) | ||
public void shouldRetryWithDynamicDelay() { | ||
driver.addExpectation(onRequestTo("/baz"), giveEmptyResponse().withStatus(503) | ||
.withHeader("Retry-After", "1")); | ||
driver.addExpectation(onRequestTo("/baz"), giveEmptyResponse()); | ||
|
||
unit.get("/baz") | ||
.dispatch(series(), | ||
on(SUCCESSFUL).call(pass())) | ||
.join(); | ||
} | ||
|
||
@Test(timeout = 1500) | ||
public void shouldRetryWithDynamicDelayDate() { | ||
driver.addExpectation(onRequestTo("/baz"), giveEmptyResponse().withStatus(503) | ||
.withHeader("Retry-After", "Wed, 11 Apr 2018 22:34:28 GMT")); | ||
driver.addExpectation(onRequestTo("/baz"), giveEmptyResponse()); | ||
|
||
unit.get("/baz") | ||
.dispatch(series(), | ||
on(SUCCESSFUL).call(pass())) | ||
.join(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.