Search in sources :

Example 1 with TimeoutHttpServiceFilter

use of io.servicetalk.http.utils.TimeoutHttpServiceFilter 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 2 with TimeoutHttpServiceFilter

use of io.servicetalk.http.utils.TimeoutHttpServiceFilter in project servicetalk by apple.

the class DefaultGrpcServerBuilder method preBuild.

private ExecutionContextInterceptorHttpServerBuilder preBuild() {
    final ExecutionContextInterceptorHttpServerBuilder interceptor = new ExecutionContextInterceptorHttpServerBuilder(httpServerBuilderSupplier.get());
    interceptor.appendNonOffloadingServiceFilter(GrpcExceptionMapperServiceFilter.INSTANCE);
    directCallInitializer.initialize(interceptor);
    initializer.initialize(interceptor);
    interceptor.appendServiceFilter(new TimeoutHttpServiceFilter(grpcDetermineTimeout(defaultTimeout), true));
    return interceptor;
}
Also used : TimeoutHttpServiceFilter(io.servicetalk.http.utils.TimeoutHttpServiceFilter)

Aggregations

TimeoutHttpServiceFilter (io.servicetalk.http.utils.TimeoutHttpServiceFilter)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 (io.servicetalk.concurrent.api.Single)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 HttpClient (io.servicetalk.http.api.HttpClient)1 HttpResponse (io.servicetalk.http.api.HttpResponse)1 HttpResponseStatus (io.servicetalk.http.api.HttpResponseStatus)1 OK (io.servicetalk.http.api.HttpResponseStatus.OK)1 StreamingHttpClientFilter (io.servicetalk.http.api.StreamingHttpClientFilter)1 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)1 StreamingHttpRequester (io.servicetalk.http.api.StreamingHttpRequester)1 StreamingHttpResponse (io.servicetalk.http.api.StreamingHttpResponse)1 HttpClients.forSingleAddress (io.servicetalk.http.netty.HttpClients.forSingleAddress)1