Search in sources :

Example 1 with HttpExecutionContext

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

the class ProxyConnectConnectionFactoryFilterTest method cannotAccessNettyChannel.

@Test
void cannotAccessNettyChannel() {
    // Does not implement NettyConnectionContext:
    HttpExecutionContext executionContext = new HttpExecutionContextBuilder().build();
    HttpConnectionContext connectionContext = (mock(HttpConnectionContext.class));
    when(connectionContext.executionContext()).thenReturn(executionContext);
    when(connection.connectionContext()).thenReturn(connectionContext);
    configureRequestSend();
    configureConnectRequest();
    subscribeToProxyConnectionFactory();
    assertThat(subscriber.awaitOnError(), instanceOf(ClassCastException.class));
    assertConnectPayloadConsumed(false);
    assertConnectionClosed();
}
Also used : HttpConnectionContext(io.servicetalk.http.api.HttpConnectionContext) HttpExecutionContext(io.servicetalk.http.api.HttpExecutionContext) Test(org.junit.jupiter.api.Test)

Example 2 with HttpExecutionContext

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

the class InOrderRouter method handle.

@Override
public Single<StreamingHttpResponse> handle(final HttpServiceContext ctx, final StreamingHttpRequest request, final StreamingHttpResponseFactory factory) {
    for (final Route pair : routes) {
        if (pair.predicate().test(ctx, request)) {
            StreamingHttpService service = pair.service();
            final HttpExecutionStrategy strategy = pair.routeStrategy();
            HttpExecutionContext useContext = ctx.executionContext();
            if (null != strategy && useContext.executionStrategy().missing(strategy).hasOffloads()) {
                // Additional offloading needed
                service = StreamingHttpServiceToOffloadedStreamingHttpService.offloadService(strategy, useContext.executor(), IoThreadFactory.IoThread::currentThreadIsIoThread, service);
            }
            return service.handle(ctx, request, factory);
        }
    }
    return fallbackService.handle(ctx, request, factory);
}
Also used : HttpExecutionContext(io.servicetalk.http.api.HttpExecutionContext) IoThreadFactory(io.servicetalk.transport.api.IoThreadFactory) StreamingHttpServiceToOffloadedStreamingHttpService(io.servicetalk.http.api.StreamingHttpServiceToOffloadedStreamingHttpService) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) HttpExecutionStrategy(io.servicetalk.http.api.HttpExecutionStrategy)

Example 3 with HttpExecutionContext

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

the class TimeoutHttpRequesterFilterTest method applyFilter.

private static Single<StreamingHttpResponse> applyFilter(TimeoutHttpRequesterFilter filterFactory, final HttpExecutionStrategy strategy, final Single<StreamingHttpResponse> responseSingle) {
    HttpExecutionContext executionContext = new DefaultHttpExecutionContext(DEFAULT_ALLOCATOR, IO_EXECUTOR, EXECUTOR, strategy);
    HttpConnectionContext connectionContext = mock(HttpConnectionContext.class);
    when(connectionContext.executionContext()).thenReturn(executionContext);
    FilterableStreamingHttpConnection connection = mock(FilterableStreamingHttpConnection.class);
    when(connection.executionContext()).thenReturn(executionContext);
    when(connection.request(any())).thenReturn(responseSingle);
    StreamingHttpRequester requester = filterFactory.create(connection);
    return requester.request(mock(StreamingHttpRequest.class));
}
Also used : StreamingHttpRequester(io.servicetalk.http.api.StreamingHttpRequester) HttpConnectionContext(io.servicetalk.http.api.HttpConnectionContext) DefaultHttpExecutionContext(io.servicetalk.http.api.DefaultHttpExecutionContext) HttpExecutionContext(io.servicetalk.http.api.HttpExecutionContext) FilterableStreamingHttpConnection(io.servicetalk.http.api.FilterableStreamingHttpConnection) DefaultHttpExecutionContext(io.servicetalk.http.api.DefaultHttpExecutionContext) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest)

Example 4 with HttpExecutionContext

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

the class DefaultHttpServerBuilder method listenForService.

/**
 * Starts this server and returns the {@link HttpServerContext} after the server has been successfully started.
 * <p>
 * If the underlying protocol (e.g. TCP) supports it this should result in a socket bind/listen on {@code address}.
 * <p>/p>
 * The execution path for a request will be offloaded from the IO thread as required to ensure safety. The
 * <dl>
 *     <dt>read side</dt>
 *     <dd>IO thread → request → non-offload filters → offload filters → raw service</dd>
 *     <dt>subscribe/request side</dt>
 *     <dd>IO thread → subscribe/request/cancel → non-offload filters → offload filters → raw service</dd>
 * </dl>
 *
 * @param rawService {@link StreamingHttpService} to use for the server.
 * @param strategy the {@link HttpExecutionStrategy} to use for the service.
 * @return A {@link Single} that completes when the server is successfully started or terminates with an error if
 * the server could not be started.
 */
private Single<HttpServerContext> listenForService(final StreamingHttpService rawService, final HttpExecutionStrategy strategy) {
    InfluencerConnectionAcceptor connectionAcceptor = connectionAcceptorFactory == null ? null : InfluencerConnectionAcceptor.withStrategy(connectionAcceptorFactory.create(ACCEPT_ALL), connectionAcceptorFactory.requiredOffloads());
    final StreamingHttpService filteredService;
    final HttpExecutionContext executionContext;
    if (noOffloadServiceFilters.isEmpty()) {
        filteredService = serviceFilters.isEmpty() ? rawService : buildService(serviceFilters.stream(), rawService);
        executionContext = buildExecutionContext(strategy);
    } else {
        Stream<StreamingHttpServiceFilterFactory> nonOffloadingFilters = noOffloadServiceFilters.stream();
        if (strategy.isRequestResponseOffloaded()) {
            executionContext = buildExecutionContext(REQRESP_OFFLOADS.missing(strategy));
            BooleanSupplier shouldOffload = executionContext.ioExecutor().shouldOffloadSupplier();
            // We are going to have to offload, even if just to the raw service
            OffloadingFilter offloadingFilter = new OffloadingFilter(strategy, buildFactory(serviceFilters), shouldOffload);
            nonOffloadingFilters = Stream.concat(nonOffloadingFilters, Stream.of(offloadingFilter));
        } else {
            // All the filters can be appended.
            nonOffloadingFilters = Stream.concat(nonOffloadingFilters, serviceFilters.stream());
            executionContext = buildExecutionContext(strategy);
        }
        filteredService = buildService(nonOffloadingFilters, rawService);
    }
    return doBind(executionContext, connectionAcceptor, filteredService).afterOnSuccess(serverContext -> LOGGER.debug("Server for address {} uses strategy {}", serverContext.listenAddress(), strategy));
}
Also used : HttpExecutionContext(io.servicetalk.http.api.HttpExecutionContext) InfluencerConnectionAcceptor(io.servicetalk.transport.netty.internal.InfluencerConnectionAcceptor) BlockingStreamingHttpService(io.servicetalk.http.api.BlockingStreamingHttpService) HttpApiConversions.toStreamingHttpService(io.servicetalk.http.api.HttpApiConversions.toStreamingHttpService) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) StreamingHttpServiceFilterFactory(io.servicetalk.http.api.StreamingHttpServiceFilterFactory) BooleanSupplier(java.util.function.BooleanSupplier)

Example 5 with HttpExecutionContext

use of io.servicetalk.http.api.HttpExecutionContext 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)

Aggregations

HttpExecutionContext (io.servicetalk.http.api.HttpExecutionContext)14 HttpExecutionStrategy (io.servicetalk.http.api.HttpExecutionStrategy)5 FilterableStreamingHttpClient (io.servicetalk.http.api.FilterableStreamingHttpClient)4 StreamingHttpRequest (io.servicetalk.http.api.StreamingHttpRequest)3 StreamingHttpService (io.servicetalk.http.api.StreamingHttpService)3 IoExecutor (io.servicetalk.transport.api.IoExecutor)3 Channel (io.netty.channel.Channel)2 BufferAllocator (io.servicetalk.buffer.api.BufferAllocator)2 ServiceDiscoverer (io.servicetalk.client.api.ServiceDiscoverer)2 AsyncCloseables.newCompositeCloseable (io.servicetalk.concurrent.api.AsyncCloseables.newCompositeCloseable)2 Completable (io.servicetalk.concurrent.api.Completable)2 CompositeCloseable (io.servicetalk.concurrent.api.CompositeCloseable)2 Executor (io.servicetalk.concurrent.api.Executor)2 Single (io.servicetalk.concurrent.api.Single)2 Single.failed (io.servicetalk.concurrent.api.Single.failed)2 HttpConnectionContext (io.servicetalk.http.api.HttpConnectionContext)2 StreamingHttpClient (io.servicetalk.http.api.StreamingHttpClient)2 StreamingHttpRequestResponseFactory (io.servicetalk.http.api.StreamingHttpRequestResponseFactory)2 StreamingHttpResponseFactory (io.servicetalk.http.api.StreamingHttpResponseFactory)2 ExecutionStrategy (io.servicetalk.transport.api.ExecutionStrategy)2