use of io.servicetalk.transport.api.ConnectionContext in project servicetalk by apple.
the class AbstractLBHttpConnectionFactory method newConnection.
@Override
public final Single<LoadBalancedStreamingHttpConnection> newConnection(final ResolvedAddress resolvedAddress, @Nullable final ContextMap context, @Nullable final TransportObserver observer) {
return filterableConnectionFactory.newConnection(resolvedAddress, context, observer).map(conn -> {
FilterableStreamingHttpConnection filteredConnection = connectionFilterFunction != null ? connectionFilterFunction.create(conn) : conn;
ConnectionContext ctx = filteredConnection.connectionContext();
Completable onClosing;
if (ctx instanceof NettyConnectionContext) {
// bondolo - I don't believe this is necessary, always the same as filteredConnection.onClose()
// perhaps this is only needed for testing where the mock doesn't implement onClose()?
onClosing = ((NettyConnectionContext) ctx).onClosing();
} else {
onClosing = filteredConnection.onClose();
}
final Publisher<? extends ConsumableEvent<Integer>> maxConcurrency = filteredConnection.transportEventStream(MAX_CONCURRENCY);
final ReservableRequestConcurrencyController concurrencyController = newConcurrencyController(maxConcurrency, onClosing);
return new LoadBalancedStreamingHttpConnection(protocolBinding.apply(filteredConnection), concurrencyController, connectStrategy instanceof HttpExecutionStrategy ? (HttpExecutionStrategy) connectStrategy : HttpExecutionStrategies.offloadNone());
});
}
use of io.servicetalk.transport.api.ConnectionContext in project servicetalk by apple.
the class ServerGracefulConnectionClosureHandlingTest method setUp.
@BeforeEach
void setUp() throws Exception {
AtomicReference<Runnable> serverClose = new AtomicReference<>();
serverContext = forAddress(localAddress(0)).ioExecutor(SERVER_CTX.ioExecutor()).executor(SERVER_CTX.executor()).executionStrategy(offloadNone()).appendConnectionAcceptorFilter(original -> new DelegatingConnectionAcceptor(original) {
@Override
public Completable accept(final ConnectionContext context) {
((NettyHttpServerConnection) context).onClosing().whenFinally(serverConnectionClosing::countDown).subscribe();
context.onClose().whenFinally(serverConnectionClosed::countDown).subscribe();
return completed();
}
}).listenStreamingAndAwait((ctx, request, responseFactory) -> succeeded(responseFactory.ok().addHeader(CONTENT_LENGTH, valueOf(RESPONSE_CONTENT.length())).payloadBody(request.payloadBody().ignoreElements().concat(from(RESPONSE_CONTENT)), RAW_STRING_SERIALIZER).transformMessageBody(payload -> payload.whenFinally(serverClose.get()))));
serverContext.onClose().whenFinally(serverContextClosed::countDown).subscribe();
serverClose.set(() -> serverContext.closeAsyncGracefully().subscribe());
serverAddress = (InetSocketAddress) serverContext.listenAddress();
}
use of io.servicetalk.transport.api.ConnectionContext in project servicetalk by apple.
the class AbstractLBHttpConnectionFactory method newConnection.
@Override
public final Single<LoadBalancedStreamingHttpConnection> newConnection(final ResolvedAddress resolvedAddress, @Nullable final TransportObserver observer) {
return filterableConnectionFactory.newConnection(resolvedAddress, observer).map(conn -> {
FilterableStreamingHttpConnection filteredConnection = connectionFilterFunction != null ? connectionFilterFunction.create(conn) : conn;
ConnectionContext ctx = filteredConnection.connectionContext();
Completable onClosing;
if (ctx instanceof NettyConnectionContext) {
// bondolo - I don't believe this is necessary, always the same as filteredConnection.onClose()
// perhaps this is only needed for testing where the mock doesn't implement onClose()?
onClosing = ((NettyConnectionContext) ctx).onClosing();
} else {
onClosing = filteredConnection.onClose();
}
final Publisher<? extends ConsumableEvent<Integer>> maxConcurrency = filteredConnection.transportEventStream(MAX_CONCURRENCY);
final ReservableRequestConcurrencyController concurrencyController = newConcurrencyController(maxConcurrency, onClosing);
return new LoadBalancedStreamingHttpConnection(protocolBinding.apply(filteredConnection), concurrencyController, connectStrategy instanceof HttpExecutionStrategy ? (HttpExecutionStrategy) connectStrategy : HttpExecutionStrategies.offloadNone());
});
}
use of io.servicetalk.transport.api.ConnectionContext in project FrameworkBenchmarks by TechEmpower.
the class Server method main.
public static void main(String[] args) throws Exception {
/*
* Disable AsyncContext
*/
AsyncContext.disable();
/*
* Factory to implement io pooling
*/
IoExecutor ioExecutor = NettyIoExecutors.createIoExecutor(Runtime.getRuntime().availableProcessors(), new IoThreadFactory("io-pool"));
/*
* Factory to disable headers validation
*/
DefaultHttpHeadersFactory headersFactory = new DefaultHttpHeadersFactory(false, false);
HttpSerializationProvider serializer = HttpSerializationProviders.jsonSerializer(new JacksonSerializationProvider());
// Create a custom server builder with performance enhancements
HttpServers.forPort(8080).executionStrategy(HttpExecutionStrategies.noOffloadsStrategy()).ioExecutor(ioExecutor).disableDrainingRequestPayloadBody().protocols(HttpProtocolConfigs.h1().headersFactory(headersFactory).build()).appendConnectionAcceptorFilter(delegate -> new ConnectionAcceptor() {
@Override
public Completable accept(ConnectionContext context) {
((NettyConnectionContext) context).updateFlushStrategy((current, isOrig) -> FlushStrategies.flushOnEnd());
return delegate.accept(context);
}
}).listenAndAwait((ctx, request, responseFactory) -> {
((NettyConnectionContext) ctx).updateFlushStrategy(((current, isCurrentOriginal) -> FlushStrategies.flushOnEach()));
if (request.path().equals("/json")) {
Map<String, String> obj = new HashMap<String, String>();
obj.put("message", "Hello, World!");
return succeeded(responseFactory.ok().payloadBody(obj, serializer.serializerFor(Map.class)).addHeader("Date", getCurrentTime()).addHeader("Server", "ServiceTalk"));
}
if (request.path().equals("/plaintext")) {
return succeeded(responseFactory.ok().payloadBody("Hello, World!", HttpSerializationProviders.textSerializer()).addHeader("Date", getCurrentTime()).addHeader("Server", "ServiceTalk"));
}
;
return null;
}).awaitShutdown();
}
Aggregations