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();
}
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);
}
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));
}
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));
}
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;
}
}
Aggregations