Search in sources :

Example 1 with FilterableStreamingHttpClient

use of io.servicetalk.http.api.FilterableStreamingHttpClient in project servicetalk by apple.

the class EnforceSequentialModeRequesterFilterTest method responseCompletesAfterRequestPayloadBodyCompletes.

@Test
void responseCompletesAfterRequestPayloadBodyCompletes() {
    TestPublisher<Buffer> payloadBody = new TestPublisher<>();
    StreamingHttpRequest request = REQ_RES_FACTORY.post("/").payloadBody(payloadBody);
    FilterableStreamingHttpClient client = EnforceSequentialModeRequesterFilter.INSTANCE.create(this.client);
    StepVerifiers.create(client.request(request)).expectCancellable().then(payloadBody::onComplete).expectSuccess().verify();
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) FilterableStreamingHttpClient(io.servicetalk.http.api.FilterableStreamingHttpClient) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) Test(org.junit.jupiter.api.Test)

Example 2 with FilterableStreamingHttpClient

use of io.servicetalk.http.api.FilterableStreamingHttpClient in project servicetalk by apple.

the class DefaultMultiAddressUrlHttpClientBuilder method buildStreaming.

@Override
public StreamingHttpClient buildStreaming() {
    final CompositeCloseable closeables = newCompositeCloseable();
    try {
        final HttpClientBuildContext<HostAndPort, InetSocketAddress> buildContext = builderTemplate.copyBuildCtx();
        final ClientFactory clientFactory = new ClientFactory(buildContext.builder, singleAddressInitializer);
        HttpExecutionContext executionContext = buildContext.builder.executionContextBuilder.build();
        final CachingKeyFactory keyFactory = closeables.prepend(new CachingKeyFactory());
        FilterableStreamingHttpClient urlClient = closeables.prepend(new StreamingUrlHttpClient(executionContext, clientFactory, keyFactory, defaultReqRespFactory(buildContext.httpConfig().asReadOnly(), executionContext.bufferAllocator())));
        // Need to wrap the top level client (group) in order for non-relative redirects to work
        urlClient = redirectConfig == null ? urlClient : new RedirectingHttpRequesterFilter(redirectConfig).create(urlClient);
        HttpExecutionStrategy computedStrategy = buildContext.builder.computeChainStrategy(executionContext.executionStrategy());
        LOGGER.debug("Client created with base strategy {} → computed strategy {}", executionContext.executionStrategy(), computedStrategy);
        return new FilterableClientToClient(urlClient, computedStrategy);
    } catch (final Throwable t) {
        closeables.closeAsync().subscribe();
        throw t;
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) CompositeCloseable(io.servicetalk.concurrent.api.CompositeCloseable) AsyncCloseables.newCompositeCloseable(io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable) HostAndPort(io.servicetalk.transport.api.HostAndPort) HttpExecutionContext(io.servicetalk.http.api.HttpExecutionContext) FilterableStreamingHttpClient(io.servicetalk.http.api.FilterableStreamingHttpClient) HttpExecutionStrategy(io.servicetalk.http.api.HttpExecutionStrategy) RedirectingHttpRequesterFilter(io.servicetalk.http.utils.RedirectingHttpRequesterFilter)

Example 3 with FilterableStreamingHttpClient

use of io.servicetalk.http.api.FilterableStreamingHttpClient in project servicetalk by apple.

the class RetryingHttpRequesterFilterAutoRetryStrategiesTest method newFilter.

private ContextAwareRetryingHttpClientFilter newFilter(final RetryingHttpRequesterFilter filter) {
    final FilterableStreamingHttpClient client = mock(FilterableStreamingHttpClient.class);
    final HttpExecutionContext executionContext = mock(HttpExecutionContext.class);
    when(executionContext.executor()).then((Answer<Executor>) invocation -> immediate());
    when(client.executionContext()).then(__ -> executionContext);
    final ContextAwareRetryingHttpClientFilter f = (ContextAwareRetryingHttpClientFilter) filter.create(client);
    f.inject(lbEvents, sdStatus);
    return f;
}
Also used : FilterableStreamingHttpClient(io.servicetalk.http.api.FilterableStreamingHttpClient) DEFAULT_ALLOCATOR(io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR) TestPublisher(io.servicetalk.concurrent.api.TestPublisher) TestCompletableSubscriber(io.servicetalk.concurrent.test.internal.TestCompletableSubscriber) Answer(org.mockito.stubbing.Answer) BackOffPolicy.ofNoRetries(io.servicetalk.http.netty.RetryingHttpRequesterFilter.BackOffPolicy.ofNoRetries) Executor(io.servicetalk.concurrent.Executor) IsNull.nullValue(org.hamcrest.core.IsNull.nullValue) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) DELIBERATE_EXCEPTION(io.servicetalk.concurrent.internal.DeliberateException.DELIBERATE_EXCEPTION) Nonnull(javax.annotation.Nonnull) HttpExecutionContext(io.servicetalk.http.api.HttpExecutionContext) Executors.immediate(io.servicetalk.concurrent.api.Executors.immediate) RetryableConnectException(io.servicetalk.client.api.RetryableConnectException) Completable(io.servicetalk.concurrent.api.Completable) ContextAwareRetryingHttpClientFilter(io.servicetalk.http.netty.RetryingHttpRequesterFilter.ContextAwareRetryingHttpClientFilter) LOAD_BALANCER_READY_EVENT(io.servicetalk.client.api.LoadBalancerReadyEvent.LOAD_BALANCER_READY_EVENT) NoAvailableHostException(io.servicetalk.client.api.NoAvailableHostException) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Mockito.when(org.mockito.Mockito.when) UnknownHostException(java.net.UnknownHostException) SourceAdapters.toSource(io.servicetalk.concurrent.api.SourceAdapters.toSource) GET(io.servicetalk.http.api.HttpRequestMethod.GET) StreamingHttpRequests.newRequest(io.servicetalk.http.api.StreamingHttpRequests.newRequest) HttpRequestMetaData(io.servicetalk.http.api.HttpRequestMetaData) Test(org.junit.jupiter.api.Test) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) RetryingHttpRequesterFilter.disableAutoRetries(io.servicetalk.http.netty.RetryingHttpRequesterFilter.disableAutoRetries) Matchers.sameInstance(org.hamcrest.Matchers.sameInstance) TestCompletable(io.servicetalk.concurrent.api.TestCompletable) Matchers.is(org.hamcrest.Matchers.is) Matchers.anyOf(org.hamcrest.Matchers.anyOf) INSTANCE(io.servicetalk.http.api.DefaultHttpHeadersFactory.INSTANCE) HTTP_1_1(io.servicetalk.http.api.HttpProtocolVersion.HTTP_1_1) Mockito.mock(org.mockito.Mockito.mock) Executor(io.servicetalk.concurrent.Executor) ContextAwareRetryingHttpClientFilter(io.servicetalk.http.netty.RetryingHttpRequesterFilter.ContextAwareRetryingHttpClientFilter) FilterableStreamingHttpClient(io.servicetalk.http.api.FilterableStreamingHttpClient) HttpExecutionContext(io.servicetalk.http.api.HttpExecutionContext)

Example 4 with FilterableStreamingHttpClient

use of io.servicetalk.http.api.FilterableStreamingHttpClient in project servicetalk by apple.

the class ConditionalFilterFactory method append.

public FilterFactory append(FilterFactory append) {
    StreamingHttpClientFilterFactory clientFactory = appendClientFilterFactory(this, append);
    StreamingHttpConnectionFilterFactory connectionFactory = appendConnectionFilterFactory(this, append);
    return new FilterFactory() {

        @Override
        public StreamingHttpClientFilter create(final FilterableStreamingHttpClient client) {
            return clientFactory.create(client);
        }

        @Override
        public StreamingHttpConnectionFilter create(final FilterableStreamingHttpConnection connection) {
            return connectionFactory.create(connection);
        }
    };
}
Also used : StreamingHttpClientFilterFactory(io.servicetalk.http.api.StreamingHttpClientFilterFactory) FilterableStreamingHttpClient(io.servicetalk.http.api.FilterableStreamingHttpClient) FilterableStreamingHttpConnection(io.servicetalk.http.api.FilterableStreamingHttpConnection) StreamingHttpConnectionFilterFactory(io.servicetalk.http.api.StreamingHttpConnectionFilterFactory) StreamingHttpClientFilterFactory(io.servicetalk.http.api.StreamingHttpClientFilterFactory) StreamingHttpConnectionFilterFactory(io.servicetalk.http.api.StreamingHttpConnectionFilterFactory)

Example 5 with FilterableStreamingHttpClient

use of io.servicetalk.http.api.FilterableStreamingHttpClient in project servicetalk by apple.

the class ClientEffectiveStrategyTest method clientStrategy.

@ParameterizedTest(name = "API={0} builder={1} filter={2} LB={3} CF={4}")
@MethodSource("casesSupplier")
void clientStrategy(ClientType clientType, @Nullable final HttpExecutionStrategy builderStrategy, @Nullable final HttpExecutionStrategy filterStrategy, @Nullable final HttpExecutionStrategy lbStrategy, @Nullable final HttpExecutionStrategy cfStrategy) throws Exception {
    HttpExecutionStrategy effectiveStrategy = computeClientExecutionStrategy(builderStrategy, filterStrategy, lbStrategy, cfStrategy);
    SingleAddressHttpClientBuilder<HostAndPort, InetSocketAddress> clientBuilder = HttpClients.forSingleAddress(serverHostAndPort(context));
    if (builderStrategy != null) {
        clientBuilder.executionStrategy(builderStrategy);
    }
    ClientInvokingThreadRecorder invokingThreadsRecorder = new ClientInvokingThreadRecorder(clientType, effectiveStrategy);
    clientBuilder.appendClientFilter(invokingThreadsRecorder);
    if (null != filterStrategy) {
        clientBuilder.appendClientFilter(new StreamingHttpClientFilterFactory() {

            @Override
            public StreamingHttpClientFilter create(final FilterableStreamingHttpClient client) {
                return new StreamingHttpClientFilter(client) {
                };
            }

            @Override
            public HttpExecutionStrategy requiredOffloads() {
                return filterStrategy;
            }
        });
    }
    if (null != lbStrategy) {
        HttpLoadBalancerFactory<InetSocketAddress> lfFactory = DefaultHttpLoadBalancerFactory.Builder.from(new LoadBalancerFactoryImpl() {

            @Override
            public ExecutionStrategy requiredOffloads() {
                return lbStrategy;
            }
        }).build();
        clientBuilder.loadBalancerFactory(lfFactory);
    }
    if (null != cfStrategy) {
        clientBuilder.appendConnectionFilter(new StreamingHttpConnectionFilterFactory() {

            @Override
            public StreamingHttpConnectionFilter create(final FilterableStreamingHttpConnection connection) {
                return new StreamingHttpConnectionFilter(connection) {
                };
            }

            @Override
            public HttpExecutionStrategy requiredOffloads() {
                return cfStrategy;
            }
        });
    }
    // Exercise the client
    try (StreamingHttpClient client = clientBuilder.buildStreaming()) {
        String responseBody = getResponse(clientType, client);
        assertThat(responseBody, is(GREETING));
        invokingThreadsRecorder.verifyOffloads();
    }
}
Also used : FilterableStreamingHttpClient(io.servicetalk.http.api.FilterableStreamingHttpClient) StreamingHttpClient(io.servicetalk.http.api.StreamingHttpClient) BlockingStreamingHttpClient(io.servicetalk.http.api.BlockingStreamingHttpClient) InetSocketAddress(java.net.InetSocketAddress) FilterableStreamingHttpConnection(io.servicetalk.http.api.FilterableStreamingHttpConnection) StreamingHttpConnectionFilterFactory(io.servicetalk.http.api.StreamingHttpConnectionFilterFactory) StreamingHttpClientFilterFactory(io.servicetalk.http.api.StreamingHttpClientFilterFactory) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) HostAndPort(io.servicetalk.transport.api.HostAndPort) StreamingHttpClientFilter(io.servicetalk.http.api.StreamingHttpClientFilter) StreamingHttpConnectionFilter(io.servicetalk.http.api.StreamingHttpConnectionFilter) FilterableStreamingHttpClient(io.servicetalk.http.api.FilterableStreamingHttpClient) HttpExecutionStrategy(io.servicetalk.http.api.HttpExecutionStrategy) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

FilterableStreamingHttpClient (io.servicetalk.http.api.FilterableStreamingHttpClient)7 HttpExecutionContext (io.servicetalk.http.api.HttpExecutionContext)4 HttpExecutionStrategy (io.servicetalk.http.api.HttpExecutionStrategy)4 FilterableStreamingHttpConnection (io.servicetalk.http.api.FilterableStreamingHttpConnection)3 AsyncCloseables.newCompositeCloseable (io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable)2 Completable (io.servicetalk.concurrent.api.Completable)2 CompositeCloseable (io.servicetalk.concurrent.api.CompositeCloseable)2 TestPublisher (io.servicetalk.concurrent.api.TestPublisher)2 HttpRequestMetaData (io.servicetalk.http.api.HttpRequestMetaData)2 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)2 StreamingHttpRequestResponseFactory (io.servicetalk.http.api.StreamingHttpRequestResponseFactory)2 Test (org.junit.jupiter.api.Test)2 SslContext (io.netty.handler.ssl.SslContext)1 Buffer (io.servicetalk.buffer.api.Buffer)1 BufferAllocator (io.servicetalk.buffer.api.BufferAllocator)1 DEFAULT_ALLOCATOR (io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR)1 ClientGroup (io.servicetalk.client.api.ClientGroup)1 LOAD_BALANCER_READY_EVENT (io.servicetalk.client.api.LoadBalancerReadyEvent.LOAD_BALANCER_READY_EVENT)1 NoAvailableHostException (io.servicetalk.client.api.NoAvailableHostException)1 RetryableConnectException (io.servicetalk.client.api.RetryableConnectException)1