Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust servicetalk-examples-http-retry #2746

Merged
merged 1 commit into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,56 @@
package io.servicetalk.examples.http.retry;

import io.servicetalk.http.api.HttpClient;
import io.servicetalk.http.api.HttpResponseStatus;
import io.servicetalk.http.netty.HttpClients;
import io.servicetalk.http.netty.RetryingHttpRequesterFilter;
import io.servicetalk.http.netty.RetryingHttpRequesterFilter.BackOffPolicy;

import static io.servicetalk.http.api.HttpResponseStatus.StatusClass.SERVER_ERROR_5XX;
import java.time.Duration;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import static io.servicetalk.http.api.HttpResponseStatus.BAD_GATEWAY;
import static io.servicetalk.http.api.HttpResponseStatus.GATEWAY_TIMEOUT;
import static io.servicetalk.http.api.HttpResponseStatus.SERVICE_UNAVAILABLE;
import static io.servicetalk.http.api.HttpResponseStatus.TOO_MANY_REQUESTS;
import static io.servicetalk.http.api.HttpSerializers.textSerializerUtf8;

/**
* Extends the Async "Hello World" client to immediately retry requests that get a 5XX response. Up to three attempts
* total, one initial attempt and up to two retries, will be made before failure.
* Extends the Async "Hello World" client to retry requests that receive a retryable status code in response.
* Up to three attempts total (one initial attempt and up to two retries) with exponential backoff and jitter will be
* made before failure.
*/
public final class RetryClient {

private static final Set<HttpResponseStatus> RETRYABLE_STATUS_CODES;

static {
Set<HttpResponseStatus> set = new HashSet<>();
// Modify the set of status codes as needed
set.add(TOO_MANY_REQUESTS); // 429
set.add(BAD_GATEWAY); // 502
set.add(SERVICE_UNAVAILABLE); // 503
set.add(GATEWAY_TIMEOUT); // 504
RETRYABLE_STATUS_CODES = Collections.unmodifiableSet(set);
}

public static void main(String[] args) throws Exception {
try (HttpClient client = HttpClients.forSingleAddress("localhost", 8080)
.appendClientFilter(new RetryingHttpRequesterFilter.Builder()
.responseMapper(httpResponseMetaData ->
SERVER_ERROR_5XX.contains(httpResponseMetaData.status()) ?
// Response status is 500-599, we request a retry
RETRYABLE_STATUS_CODES.contains(httpResponseMetaData.status()) ?
// Response status is retryable
new RetryingHttpRequesterFilter.HttpResponseException(
"Retry 5XX", httpResponseMetaData)
// Not a 5XX response, we do not know whether retry is required
// Not a retryable status
: null)
.retryResponses((meta, error) -> RetryingHttpRequesterFilter.BackOffPolicy.ofImmediate(2))
.retryResponses((meta, error) -> BackOffPolicy.ofExponentialBackoffDeltaJitter(
Duration.ofMillis(10), // initialDelay
Duration.ofMillis(10), // jitter
Duration.ofMillis(100), // maxDelay
2)) // maxRetries
.build())
.build()) {
client.request(client.get("/sayHello"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,56 @@
package io.servicetalk.examples.http.retry;

import io.servicetalk.http.api.HttpClient;
import io.servicetalk.http.api.HttpResponseStatus;
import io.servicetalk.http.netty.HttpClients;
import io.servicetalk.http.netty.RetryingHttpRequesterFilter;

import static io.servicetalk.http.api.HttpResponseStatus.StatusClass.SERVER_ERROR_5XX;
import java.time.Duration;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import static io.servicetalk.http.api.HttpResponseStatus.BAD_GATEWAY;
import static io.servicetalk.http.api.HttpResponseStatus.GATEWAY_TIMEOUT;
import static io.servicetalk.http.api.HttpResponseStatus.SERVICE_UNAVAILABLE;
import static io.servicetalk.http.api.HttpResponseStatus.TOO_MANY_REQUESTS;
import static io.servicetalk.http.api.HttpSerializers.textSerializerUtf8;

/**
* Extends the Async "Hello World" multi-address client to immediately retry requests that get a 5XX response. Up to
* three attempts total, one initial attempt and up to two retries, will be made before failure.
* Extends the Async "Hello World" multi-address client to retry requests that receive a retryable status code in
* response. Up to three attempts total (one initial attempt and up to two retries) with exponential backoff and jitter
* will be made before failure.
*/
public class RetryUrlClient {

private static final Set<HttpResponseStatus> RETRYABLE_STATUS_CODES;

static {
Set<HttpResponseStatus> set = new HashSet<>();
// Modify the set of status codes as needed
set.add(TOO_MANY_REQUESTS); // 429
set.add(BAD_GATEWAY); // 502
set.add(SERVICE_UNAVAILABLE); // 503
set.add(GATEWAY_TIMEOUT); // 504
RETRYABLE_STATUS_CODES = Collections.unmodifiableSet(set);
}

public static void main(String[] args) throws Exception {
try (HttpClient client = HttpClients.forMultiAddressUrl().initializer((scheme, address, builder) -> {
// If necessary, users can set different retry strategies based on `scheme` and/or `address`.
builder.appendClientFilter(new RetryingHttpRequesterFilter.Builder()
.responseMapper(httpResponseMetaData ->
SERVER_ERROR_5XX.contains(httpResponseMetaData.status()) ?
// Response status is 500-599, we request a retry
RETRYABLE_STATUS_CODES.contains(httpResponseMetaData.status()) ?
// Response status is retryable
new RetryingHttpRequesterFilter.HttpResponseException(
"Retry 5XX", httpResponseMetaData)
// Not a 5XX response, we do not know whether retry is required
// Not a retryable status
: null)
.retryResponses((meta, error) -> RetryingHttpRequesterFilter.BackOffPolicy.ofImmediate(2))
.retryResponses((meta, error) -> RetryingHttpRequesterFilter.BackOffPolicy.ofExponentialBackoffDeltaJitter(
Duration.ofMillis(10), // initialDelay
Duration.ofMillis(10), // jitter
Duration.ofMillis(100), // maxDelay
2)) // maxRetries
.build());
}).build()) {
client.request(client.get("http://localhost:8080/sayHello"))
Expand Down
Loading