Search in sources :

Example 1 with ConnectionObserver

use of io.servicetalk.transport.api.ConnectionObserver 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 ConnectionObserver

use of io.servicetalk.transport.api.ConnectionObserver in project servicetalk by apple.

the class FlushStrategyOnServerTest method setUp.

private void setUp(final Param param) {
    this.interceptor = new OutboundWriteEventsInterceptor();
    this.headersFactory = DefaultHttpHeadersFactory.INSTANCE;
    final StreamingHttpService service = (ctx, request, responseFactory) -> {
        StreamingHttpResponse resp = responseFactory.ok();
        if (request.headers().get(USE_EMPTY_RESP_BODY) == null) {
            resp.payloadBody(from("Hello", "World"), appSerializerUtf8FixLen());
        }
        if (request.headers().get(USE_AGGREGATED_RESP) != null) {
            return resp.toResponse().map(HttpResponse::toStreamingResponse);
        }
        return succeeded(resp);
    };
    final DefaultHttpExecutionContext httpExecutionContext = new DefaultHttpExecutionContext(DEFAULT_ALLOCATOR, globalExecutionContext().ioExecutor(), EXECUTOR_RULE.executor(), param.executionStrategy);
    final ReadOnlyHttpServerConfig config = new HttpServerConfig().asReadOnly();
    final ReadOnlyTcpServerConfig tcpReadOnly = new TcpServerConfig().asReadOnly();
    try {
        serverContext = TcpServerBinder.bind(localAddress(0), tcpReadOnly, true, httpExecutionContext, null, (channel, observer) -> {
            final ConnectionObserver connectionObserver = config.tcpConfig().transportObserver().onNewConnection(channel.localAddress(), channel.remoteAddress());
            return initChannel(channel, httpExecutionContext, config, new TcpServerChannelInitializer(tcpReadOnly, connectionObserver).andThen(channel1 -> channel1.pipeline().addLast(interceptor)), service, true, connectionObserver);
        }, connection -> connection.process(true)).map(delegate -> new NettyHttpServerContext(delegate, service, httpExecutionContext)).toFuture().get();
    } catch (Exception e) {
        fail(e);
    }
    client = HttpClients.forSingleAddress(serverHostAndPort(serverContext)).protocols(h1Default()).buildBlocking();
}
Also used : DefaultHttpExecutionContext(io.servicetalk.http.api.DefaultHttpExecutionContext) TcpServerChannelInitializer(io.servicetalk.tcp.netty.internal.TcpServerChannelInitializer) TcpServerConfig(io.servicetalk.tcp.netty.internal.TcpServerConfig) ReadOnlyTcpServerConfig(io.servicetalk.tcp.netty.internal.ReadOnlyTcpServerConfig) ChannelPromise(io.netty.channel.ChannelPromise) HttpExecutionStrategies.defaultStrategy(io.servicetalk.http.api.HttpExecutionStrategies.defaultStrategy) Executor(io.servicetalk.concurrent.api.Executor) GlobalExecutionContext.globalExecutionContext(io.servicetalk.transport.netty.internal.GlobalExecutionContext.globalExecutionContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) ExecutorExtension(io.servicetalk.concurrent.api.ExecutorExtension) HttpResponse(io.servicetalk.http.api.HttpResponse) TRANSFER_ENCODING(io.servicetalk.http.api.HttpHeaderNames.TRANSFER_ENCODING) BlockingQueue(java.util.concurrent.BlockingQueue) DefaultHttpHeadersFactory(io.servicetalk.http.api.DefaultHttpHeadersFactory) HttpExecutionStrategies.customStrategyBuilder(io.servicetalk.http.api.HttpExecutionStrategies.customStrategyBuilder) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) Assertions.fail(org.junit.jupiter.api.Assertions.fail) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse) DEFAULT_ALLOCATOR(io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR) StreamingHttpRequests.newTransportRequest(io.servicetalk.http.api.StreamingHttpRequests.newTransportRequest) HttpHeaders(io.servicetalk.http.api.HttpHeaders) EnumSource(org.junit.jupiter.params.provider.EnumSource) NettyHttpServer.initChannel(io.servicetalk.http.netty.NettyHttpServer.initChannel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ConnectionObserver(io.servicetalk.transport.api.ConnectionObserver) HttpProtocolConfigs.h1Default(io.servicetalk.http.netty.HttpProtocolConfigs.h1Default) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) HttpSerializers.appSerializerUtf8FixLen(io.servicetalk.http.api.HttpSerializers.appSerializerUtf8FixLen) HttpExecutionStrategy(io.servicetalk.http.api.HttpExecutionStrategy) StreamingHttpRequest(io.servicetalk.http.api.StreamingHttpRequest) AddressUtils.serverHostAndPort(io.servicetalk.transport.netty.internal.AddressUtils.serverHostAndPort) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Publisher.from(io.servicetalk.concurrent.api.Publisher.from) CHUNKED(io.servicetalk.http.api.HttpHeaderValues.CHUNKED) AddressUtils.localAddress(io.servicetalk.transport.netty.internal.AddressUtils.localAddress) ServerContext(io.servicetalk.transport.api.ServerContext) GET(io.servicetalk.http.api.HttpRequestMethod.GET) TcpServerBinder(io.servicetalk.tcp.netty.internal.TcpServerBinder) NettyHttpServerContext(io.servicetalk.http.netty.NettyHttpServer.NettyHttpServerContext) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) HttpHeadersFactory(io.servicetalk.http.api.HttpHeadersFactory) HTTP_1_1(io.servicetalk.http.api.HttpProtocolVersion.HTTP_1_1) HttpExecutionStrategies.offloadNever(io.servicetalk.http.api.HttpExecutionStrategies.offloadNever) NettyHttpServerContext(io.servicetalk.http.netty.NettyHttpServer.NettyHttpServerContext) TcpServerChannelInitializer(io.servicetalk.tcp.netty.internal.TcpServerChannelInitializer) DefaultHttpExecutionContext(io.servicetalk.http.api.DefaultHttpExecutionContext) TcpServerConfig(io.servicetalk.tcp.netty.internal.TcpServerConfig) ReadOnlyTcpServerConfig(io.servicetalk.tcp.netty.internal.ReadOnlyTcpServerConfig) ConnectionObserver(io.servicetalk.transport.api.ConnectionObserver) StreamingHttpService(io.servicetalk.http.api.StreamingHttpService) ReadOnlyTcpServerConfig(io.servicetalk.tcp.netty.internal.ReadOnlyTcpServerConfig) StreamingHttpResponse(io.servicetalk.http.api.StreamingHttpResponse)

Aggregations

StreamingHttpService (io.servicetalk.http.api.StreamingHttpService)2 ReadOnlyTcpServerConfig (io.servicetalk.tcp.netty.internal.ReadOnlyTcpServerConfig)2 TcpServerBinder (io.servicetalk.tcp.netty.internal.TcpServerBinder)2 TcpServerChannelInitializer (io.servicetalk.tcp.netty.internal.TcpServerChannelInitializer)2 ConnectionObserver (io.servicetalk.transport.api.ConnectionObserver)2 Channel (io.netty.channel.Channel)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelOutboundHandlerAdapter (io.netty.channel.ChannelOutboundHandlerAdapter)1 ChannelPromise (io.netty.channel.ChannelPromise)1 DEFAULT_ALLOCATOR (io.servicetalk.buffer.netty.BufferAllocators.DEFAULT_ALLOCATOR)1 Executor (io.servicetalk.concurrent.api.Executor)1 ExecutorExtension (io.servicetalk.concurrent.api.ExecutorExtension)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 BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)1 DefaultHttpExecutionContext (io.servicetalk.http.api.DefaultHttpExecutionContext)1 DefaultHttpHeadersFactory (io.servicetalk.http.api.DefaultHttpHeadersFactory)1 HttpExecutionContext (io.servicetalk.http.api.HttpExecutionContext)1