Search in sources :

Example 1 with NettyHttpServerConnection

use of io.servicetalk.http.netty.NettyHttpServer.NettyHttpServerConnection in project servicetalk by apple.

the class DeferredServerChannelBinder method bind.

static Single<HttpServerContext> bind(final HttpExecutionContext executionContext, final ReadOnlyHttpServerConfig config, final SocketAddress listenAddress, @Nullable final InfluencerConnectionAcceptor connectionAcceptor, final StreamingHttpService service, final boolean drainRequestPayloadBody, final boolean sniOnly) {
    final ReadOnlyTcpServerConfig tcpConfig = config.tcpConfig();
    assert tcpConfig.sslContext() != null;
    final BiFunction<Channel, ConnectionObserver, Single<NettyConnectionContext>> channelInit = sniOnly ? (channel, connectionObserver) -> sniInitChannel(listenAddress, channel, config, executionContext, service, drainRequestPayloadBody, connectionObserver) : (channel, connectionObserver) -> alpnInitChannel(listenAddress, channel, config, executionContext, service, drainRequestPayloadBody, connectionObserver);
    // In case ALPN negotiates h2, h2 connection MUST enable auto read for its Channel.
    return TcpServerBinder.bind(listenAddress, tcpConfig, false, executionContext, connectionAcceptor, channelInit, serverConnection -> {
        // Start processing requests on http/1.1 connection:
        if (serverConnection instanceof NettyHttpServerConnection) {
            ((NettyHttpServerConnection) serverConnection).process(true);
        }
    // Nothing to do otherwise as h2 uses auto read on the parent channel
    }).map(delegate -> {
        LOGGER.debug("Started HTTP server with ALPN for address {}", delegate.listenAddress());
        // The ServerContext returned by TcpServerBinder takes care of closing the connectionAcceptor.
        return new NettyHttpServer.NettyHttpServerContext(delegate, service, executionContext);
    });
}
Also used : ConnectionObserver(io.servicetalk.transport.api.ConnectionObserver) NettyHttpServerConnection(io.servicetalk.http.netty.NettyHttpServer.NettyHttpServerConnection) Logger(org.slf4j.Logger) SocketAddress(java.net.SocketAddress) HTTP_2(io.servicetalk.http.netty.AlpnIds.HTTP_2) Single(io.servicetalk.concurrent.api.Single) BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) TcpServerChannelInitializer(io.servicetalk.tcp.netty.internal.TcpServerChannelInitializer) InfluencerConnectionAcceptor(io.servicetalk.transport.netty.internal.InfluencerConnectionAcceptor) TcpServerBinder(io.servicetalk.tcp.netty.internal.TcpServerBinder) ReadOnlyTcpServerConfig(io.servicetalk.tcp.netty.internal.ReadOnlyTcpServerConfig) Channel(io.netty.channel.Channel) ConnectionObserver(io.servicetalk.transport.api.ConnectionObserver) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) HTTP_1_1(io.servicetalk.http.netty.AlpnIds.HTTP_1_1) HttpServerContext(io.servicetalk.http.api.HttpServerContext) NoopChannelInitializer(io.servicetalk.http.netty.AlpnChannelSingle.NoopChannelInitializer) Single.failed(io.servicetalk.concurrent.api.Single.failed) NettyConnectionContext(io.servicetalk.transport.netty.internal.NettyConnectionContext) Nullable(javax.annotation.Nullable) HttpExecutionContext(io.servicetalk.http.api.HttpExecutionContext) NettyHttpServerConnection(io.servicetalk.http.netty.NettyHttpServer.NettyHttpServerConnection) Single(io.servicetalk.concurrent.api.Single) Channel(io.netty.channel.Channel) ReadOnlyTcpServerConfig(io.servicetalk.tcp.netty.internal.ReadOnlyTcpServerConfig)

Example 2 with NettyHttpServerConnection

use of io.servicetalk.http.netty.NettyHttpServer.NettyHttpServerConnection 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(offloadNever()).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();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Socket(java.net.Socket) HttpSerializers.stringStreamingSerializer(io.servicetalk.http.api.HttpSerializers.stringStreamingSerializer) HttpServers.forAddress(io.servicetalk.http.netty.HttpServers.forAddress) AtomicReference(java.util.concurrent.atomic.AtomicReference) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Publisher.from(io.servicetalk.concurrent.api.Publisher.from) ConnectionContext(io.servicetalk.transport.api.ConnectionContext) OutputStream(java.io.OutputStream) NettyHttpServerConnection(io.servicetalk.http.netty.NettyHttpServer.NettyHttpServerConnection) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ServerContext(io.servicetalk.transport.api.ServerContext) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Completable(io.servicetalk.concurrent.api.Completable) ExecutionContextExtension(io.servicetalk.transport.netty.internal.ExecutionContextExtension) CONTENT_LENGTH(io.servicetalk.http.api.HttpHeaderNames.CONTENT_LENGTH) InetSocketAddress(java.net.InetSocketAddress) Test(org.junit.jupiter.api.Test) US_ASCII(java.nio.charset.StandardCharsets.US_ASCII) CountDownLatch(java.util.concurrent.CountDownLatch) AfterEach(org.junit.jupiter.api.AfterEach) String.valueOf(java.lang.String.valueOf) DelegatingConnectionAcceptor(io.servicetalk.transport.api.DelegatingConnectionAcceptor) Completable.completed(io.servicetalk.concurrent.api.Completable.completed) Matchers.is(org.hamcrest.Matchers.is) HttpStreamingSerializer(io.servicetalk.http.api.HttpStreamingSerializer) InputStream(java.io.InputStream) HttpExecutionStrategies.offloadNever(io.servicetalk.http.api.HttpExecutionStrategies.offloadNever) Completable(io.servicetalk.concurrent.api.Completable) DelegatingConnectionAcceptor(io.servicetalk.transport.api.DelegatingConnectionAcceptor) AtomicReference(java.util.concurrent.atomic.AtomicReference) ConnectionContext(io.servicetalk.transport.api.ConnectionContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

NettyHttpServerConnection (io.servicetalk.http.netty.NettyHttpServer.NettyHttpServerConnection)2 Channel (io.netty.channel.Channel)1 Completable (io.servicetalk.concurrent.api.Completable)1 Completable.completed (io.servicetalk.concurrent.api.Completable.completed)1 Publisher.from (io.servicetalk.concurrent.api.Publisher.from)1 Single (io.servicetalk.concurrent.api.Single)1 Single.failed (io.servicetalk.concurrent.api.Single.failed)1 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)1 HttpExecutionContext (io.servicetalk.http.api.HttpExecutionContext)1 HttpExecutionStrategies.offloadNever (io.servicetalk.http.api.HttpExecutionStrategies.offloadNever)1 CONTENT_LENGTH (io.servicetalk.http.api.HttpHeaderNames.CONTENT_LENGTH)1 HttpSerializers.stringStreamingSerializer (io.servicetalk.http.api.HttpSerializers.stringStreamingSerializer)1 HttpServerContext (io.servicetalk.http.api.HttpServerContext)1 HttpStreamingSerializer (io.servicetalk.http.api.HttpStreamingSerializer)1 StreamingHttpService (io.servicetalk.http.api.StreamingHttpService)1 NoopChannelInitializer (io.servicetalk.http.netty.AlpnChannelSingle.NoopChannelInitializer)1 HTTP_1_1 (io.servicetalk.http.netty.AlpnIds.HTTP_1_1)1 HTTP_2 (io.servicetalk.http.netty.AlpnIds.HTTP_2)1 HttpServers.forAddress (io.servicetalk.http.netty.HttpServers.forAddress)1 ReadOnlyTcpServerConfig (io.servicetalk.tcp.netty.internal.ReadOnlyTcpServerConfig)1