Search in sources :

Example 1 with TimeoutHttpRequesterFilter

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);
}
Also used : Duration(java.time.Duration) TimeoutHttpRequesterFilter(io.servicetalk.http.utils.TimeoutHttpRequesterFilter)

Example 2 with TimeoutHttpRequesterFilter

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();
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) Arrays(java.util.Arrays) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) Subscriber(io.servicetalk.concurrent.SingleSource.Subscriber) TimeoutException(java.util.concurrent.TimeoutException) Cancellable(io.servicetalk.concurrent.Cancellable) StreamingHttpRequester(io.servicetalk.http.api.StreamingHttpRequester) HttpServers.forAddress(io.servicetalk.http.netty.HttpServers.forAddress) FilterableStreamingHttpConnection(io.servicetalk.http.api.FilterableStreamingHttpConnection) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) Duration(java.time.Duration) HttpClient(io.servicetalk.http.api.HttpClient) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DelegatingConnectionFactory(io.servicetalk.client.api.DelegatingConnectionFactory) Nullable(javax.annotation.Nullable) MethodSource(org.junit.jupiter.params.provider.MethodSource) PrintWriter(java.io.PrintWriter) ConnectionFactory(io.servicetalk.client.api.ConnectionFactory) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ServerContext(io.servicetalk.transport.api.ServerContext) Single.defer(io.servicetalk.concurrent.api.Single.defer) Single(io.servicetalk.concurrent.api.Single) StringWriter(java.io.StringWriter) Collection(java.util.Collection) HttpClients.forSingleAddress(io.servicetalk.http.netty.HttpClients.forSingleAddress) HttpResponse(io.servicetalk.http.api.HttpResponse) TimeoutHttpRequesterFilter(io.servicetalk.http.utils.TimeoutHttpRequesterFilter) BlockingQueue(java.util.concurrent.BlockingQueue) AsyncCloseables.newCompositeCloseable(io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable) StreamingHttpClientFilter(io.servicetalk.http.api.StreamingHttpClientFilter) TimeoutHttpServiceFilter(io.servicetalk.http.utils.TimeoutHttpServiceFilter) OK(io.servicetalk.http.api.HttpResponseStatus.OK) InetSocketAddress(java.net.InetSocketAddress) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Objects(java.util.Objects) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) CountDownLatch(java.util.concurrent.CountDownLatch) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Matcher(org.hamcrest.Matcher) TransportObserver(io.servicetalk.transport.api.TransportObserver) Single.failed(io.servicetalk.concurrent.api.Single.failed) Matchers.is(org.hamcrest.Matchers.is) HttpResponseStatus(io.servicetalk.http.api.HttpResponseStatus) StreamingHttpRequester(io.servicetalk.http.api.StreamingHttpRequester) Cancellable(io.servicetalk.concurrent.Cancellable) TimeoutHttpServiceFilter(io.servicetalk.http.utils.TimeoutHttpServiceFilter) StreamingHttpClientFilter(io.servicetalk.http.api.StreamingHttpClientFilter) Single(io.servicetalk.concurrent.api.Single) Subscriber(io.servicetalk.concurrent.SingleSource.Subscriber) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) TimeoutHttpRequesterFilter(io.servicetalk.http.utils.TimeoutHttpRequesterFilter) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse)

Example 3 with TimeoutHttpRequesterFilter

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();
    }
}
Also used : Single(io.servicetalk.concurrent.api.Single) Duration(java.time.Duration) Single.collectUnorderedDelayError(io.servicetalk.concurrent.api.Single.collectUnorderedDelayError) HttpSerializers.textSerializerUtf8(io.servicetalk.http.api.HttpSerializers.textSerializerUtf8) HttpResponse(io.servicetalk.http.api.HttpResponse) HttpClient(io.servicetalk.http.api.HttpClient) TimeoutHttpRequesterFilter(io.servicetalk.http.utils.TimeoutHttpRequesterFilter) Duration.ofSeconds(java.time.Duration.ofSeconds) HttpClients(io.servicetalk.http.netty.HttpClients) HttpClient(io.servicetalk.http.api.HttpClient) HttpResponse(io.servicetalk.http.api.HttpResponse) TimeoutHttpRequesterFilter(io.servicetalk.http.utils.TimeoutHttpRequesterFilter)

Aggregations

TimeoutHttpRequesterFilter (io.servicetalk.http.utils.TimeoutHttpRequesterFilter)3 Duration (java.time.Duration)3 Single (io.servicetalk.concurrent.api.Single)2 HttpClient (io.servicetalk.http.api.HttpClient)2 HttpResponse (io.servicetalk.http.api.HttpResponse)2 ConnectionFactory (io.servicetalk.client.api.ConnectionFactory)1 DelegatingConnectionFactory (io.servicetalk.client.api.DelegatingConnectionFactory)1 Cancellable (io.servicetalk.concurrent.Cancellable)1 Subscriber (io.servicetalk.concurrent.SingleSource.Subscriber)1 AsyncCloseables.newCompositeCloseable (io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable)1 Single.collectUnorderedDelayError (io.servicetalk.concurrent.api.Single.collectUnorderedDelayError)1 Single.defer (io.servicetalk.concurrent.api.Single.defer)1 Single.failed (io.servicetalk.concurrent.api.Single.failed)1 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)1 FilterableStreamingHttpConnection (io.servicetalk.http.api.FilterableStreamingHttpConnection)1 HttpResponseStatus (io.servicetalk.http.api.HttpResponseStatus)1 OK (io.servicetalk.http.api.HttpResponseStatus.OK)1 HttpSerializers.textSerializerUtf8 (io.servicetalk.http.api.HttpSerializers.textSerializerUtf8)1 StreamingHttpClientFilter (io.servicetalk.http.api.StreamingHttpClientFilter)1 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)1