use of io.servicetalk.http.utils.TimeoutHttpRequesterFilter in project servicetalk by apple.
the class DefaultGrpcClientBuilder method newGrpcClientCallFactory.
private GrpcClientCallFactory newGrpcClientCallFactory() {
SingleAddressHttpClientBuilder<U, R> builder = httpClientBuilderSupplier.get().protocols(h2Default());
builder.appendClientFilter(CatchAllHttpClientFilter.INSTANCE);
httpInitializer.initialize(builder);
builder.appendClientFilter(new TimeoutHttpRequesterFilter(GRPC_TIMEOUT_REQHDR, true));
Duration timeout = isInfinite(defaultTimeout, GRPC_MAX_TIMEOUT) ? null : defaultTimeout;
return GrpcClientCallFactory.from(builder.buildStreaming(), timeout);
}
use of io.servicetalk.http.utils.TimeoutHttpRequesterFilter in project servicetalk by apple.
the class ResponseTimeoutTest method setUp.
private void setUp(Duration clientTimeout, Duration serverTimeout) throws Exception {
ctx = forAddress(localAddress(0)).appendServiceFilter(new TimeoutHttpServiceFilter((req, ts) -> serverTimeout, true)).listenAndAwait((__, ___, factory) -> {
Single<HttpResponse> resp = Single.never();
serverResponses.add(resp);
return resp;
});
client = forSingleAddress(serverHostAndPort(ctx)).appendClientFilter(client -> new StreamingHttpClientFilter(client) {
@Override
protected Single<StreamingHttpResponse> request(final StreamingHttpRequester delegate, final StreamingHttpRequest request) {
return Single.succeeded(null).afterOnSubscribe(delayedClientCancels::add).concat(delegate().request(request).liftSync(target -> new Subscriber<StreamingHttpResponse>() {
@Override
public void onSubscribe(final Cancellable cancellable) {
target.onSubscribe(() -> {
delayedClientCancels.add(cancellable);
cancellable.cancel();
});
}
@Override
public void onSuccess(final StreamingHttpResponse result) {
ClientTerminationSignal signal = OK.equals(result.status()) ? new ClientTerminationSignal(target, result) : new ClientTerminationSignal(target, new HttpResponseStatusException(result.status()));
delayedClientTermination.add(signal);
target.onSuccess(result);
}
@Override
public void onError(final Throwable t) {
delayedClientTermination.add(new ClientTerminationSignal(target, t));
target.onError(t);
}
})).filter(Objects::nonNull).firstOrError().map(thing -> (StreamingHttpResponse) thing);
}
}).appendConnectionFactoryFilter(original -> new CountingConnectionFactory(original, connectionCount)).appendClientFilter(new TimeoutHttpRequesterFilter((req, ts) -> clientTimeout, true)).build();
}
use of io.servicetalk.http.utils.TimeoutHttpRequesterFilter in project servicetalk by apple.
the class TimeoutClient method main.
public static void main(String[] args) throws Exception {
try (HttpClient client = HttpClients.forSingleAddress("localhost", 8080).appendClientFilter(new TimeoutHttpRequesterFilter(ofSeconds(10), true)).build()) {
// first request, with default timeout from HttpClient (this will succeed)
Single<HttpResponse> respSingle1 = client.request(client.get("/defaultTimeout")).whenOnError(System.err::println).whenOnSuccess(resp -> {
System.out.println(resp.toString((name, value) -> value));
System.out.println(resp.payloadBody(textSerializerUtf8()));
});
// second request, with custom timeout that is lower than the client default (this will timeout)
Single<HttpResponse> respSingle2 = client.request(client.get("/3secondTimeout")).timeout(ofSeconds(3)).whenOnError(System.err::println).whenOnSuccess(resp -> {
System.out.println(resp.toString((name, value) -> value));
System.out.println(resp.payloadBody(textSerializerUtf8()));
});
// Issue the requests in parallel.
collectUnorderedDelayError(respSingle1, respSingle2).toFuture().get();
}
}
Aggregations