use of io.servicetalk.concurrent.api.CompositeCloseable in project servicetalk by apple.
the class HttpServerMultipleRequestsTest method consumeOfRequestBodyDoesNotCloseConnection.
@Disabled("https://github.com/apple/servicetalk/issues/981")
@Test
void consumeOfRequestBodyDoesNotCloseConnection() throws Exception {
StreamingHttpService service = (ctx, request, responseFactory) -> {
request.messageBody().ignoreElements().subscribe();
CharSequence requestId = request.headers().get(REQUEST_ID_HEADER);
if (requestId != null) {
StreamingHttpResponse response = responseFactory.ok();
response.headers().set(REQUEST_ID_HEADER, requestId);
return succeeded(response);
} else {
return succeeded(responseFactory.newResponse(BAD_REQUEST));
}
};
final int concurrency = 10;
final int numRequests = 10;
CompositeCloseable compositeCloseable = AsyncCloseables.newCompositeCloseable();
ServerContext ctx = compositeCloseable.append(HttpServers.forAddress(localAddress(0)).ioExecutor(serverContext.ioExecutor()).executor(serverContext.executor()).executionStrategy(defaultStrategy()).listenStreamingAndAwait(service));
ExecutorService executorService = Executors.newCachedThreadPool();
try {
AtomicReference<Throwable> causeRef = new AtomicReference<>();
CyclicBarrier barrier = new CyclicBarrier(concurrency);
CountDownLatch latch = new CountDownLatch(concurrency);
for (int i = 0; i < concurrency; ++i) {
final int finalI = i;
executorService.execute(() -> {
try {
StreamingHttpClient client = compositeCloseable.append(HttpClients.forResolvedAddress(serverHostAndPort(ctx)).protocols(h1().maxPipelinedRequests(numRequests).build()).ioExecutor(clientContext.ioExecutor()).executor(clientContext.executor()).executionStrategy(defaultStrategy()).buildStreaming());
ReservedStreamingHttpConnection connection = client.reserveConnection(client.get("/")).toFuture().get();
compositeCloseable.append(connection);
barrier.await();
for (int x = 0; x < numRequests; ++x) {
makeClientRequestWithId(connection, "thread=" + finalI + " request=" + x);
}
} catch (Throwable cause) {
causeRef.compareAndSet(null, cause);
} finally {
latch.countDown();
}
});
}
latch.await();
assertNull(causeRef.get());
} finally {
executorService.shutdown();
compositeCloseable.close();
}
}
use of io.servicetalk.concurrent.api.CompositeCloseable in project servicetalk by apple.
the class RetryingHttpRequesterFilterTest method tearDown.
@AfterEach
void tearDown() throws Exception {
CompositeCloseable closeable = AsyncCloseables.newCompositeCloseable();
if (normalClient != null) {
closeable.append(normalClient.asClient());
}
if (failingClient != null) {
closeable.append(failingClient.asClient());
}
closeable.append(svcCtx);
closeable.close();
}
use of io.servicetalk.concurrent.api.CompositeCloseable in project servicetalk by apple.
the class NettyServerContext method wrap.
/**
* Wrap the passed {@link NettyServerContext}.
*
* @param listenChannel {@link Channel} to wrap.
* @param channelSetCloseable {@link ChannelSet} to wrap.
* @param closeBefore {@link Completable} which needs to closed first before {@code listenChannel} will be closed.
* @param executionContext {@link ExecutionContext} used by this server.
* @return A new {@link NettyServerContext} instance.
*/
public static ServerContext wrap(Channel listenChannel, ListenableAsyncCloseable channelSetCloseable, @Nullable AsyncCloseable closeBefore, ExecutionContext<?> executionContext) {
final NettyChannelListenableAsyncCloseable channelCloseable = new NettyChannelListenableAsyncCloseable(listenChannel, executionContext.executionStrategy().isCloseOffloaded() ? executionContext.executor() : immediate());
final CompositeCloseable closeAsync = closeBefore == null ? newCompositeCloseable().appendAll(channelCloseable, channelSetCloseable) : newCompositeCloseable().appendAll(closeBefore, channelCloseable, channelSetCloseable);
return new NettyServerContext(listenChannel, toListenableAsyncCloseable(closeAsync), executionContext);
}
use of io.servicetalk.concurrent.api.CompositeCloseable in project servicetalk by apple.
the class HttpReporterTest method tearDown.
@AfterEach
public void tearDown() throws Exception {
CompositeCloseable closeable = AsyncCloseables.newCompositeCloseable();
if (reporter != null) {
closeable.append(reporter);
}
closeable.append(context);
closeable.closeGracefully();
}
use of io.servicetalk.concurrent.api.CompositeCloseable 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