private HttpServer createHttpServer() { HttpServer server = HttpServer.create(); if (this.resourceFactory != null) { LoopResources resources = this.resourceFactory.getLoopResources(); Assert.notNull(resources, "No LoopResources: is ReactorResourceFactory not initialized yet?"); server = server.tcpConfiguration((tcpServer) -> tcpServer.runOn(resources) .addressSupplier(this::getListenAddress)); } else { server = server.tcpConfiguration( (tcpServer) -> tcpServer.addressSupplier(this::getListenAddress)); } if (getSsl() != null && getSsl().isEnabled()) { SslServerCustomizer sslServerCustomizer = new SslServerCustomizer(getSsl(), getHttp2(), getSslStoreProvider()); server = sslServerCustomizer.apply(server); } if (getCompression() != null && getCompression().getEnabled()) { CompressionCustomizer compressionCustomizer = new CompressionCustomizer( getCompression()); server = compressionCustomizer.apply(server); } server = server.protocol(listProtocols()).forwarded(this.useForwardHeaders); return applyCustomizers(server); }
/** * Run IO loops on a supplied {@link EventLoopGroup} from the {@link LoopResources} * container. Will prefer native (epoll/kqueue) implementation if available unless the * environment property {@code reactor.netty.native} is set to {@code false}. * * @param channelResources a {@link LoopResources} accepting native runtime * expectation and returning an eventLoopGroup * * @return a new {@link TcpServer} */ public final TcpServer runOn(LoopResources channelResources) { return runOn(channelResources, LoopResources.DEFAULT_NATIVE); }
/** * Run IO loops on a supplied {@link EventLoopGroup} from the {@link LoopResources} * container. Will prefer native (epoll/kqueue) implementation if available unless the * environment property {@code reactor.netty.native} is set to {@code false}. * * @param channelResources a {@link LoopResources} accepting native runtime * expectation and returning an eventLoopGroup * * @return a new {@link TcpServer} */ public final TcpServer runOn(LoopResources channelResources) { return runOn(channelResources, LoopResources.DEFAULT_NATIVE); }
/** * Run IO loops on the given {@link EventLoopGroup}. * * @param eventLoopGroup an eventLoopGroup to share * * @return a new {@link TcpServer} */ public final TcpServer runOn(EventLoopGroup eventLoopGroup) { Objects.requireNonNull(eventLoopGroup, "eventLoopGroup"); return runOn(preferNative -> eventLoopGroup); }
/** * Run IO loops on the given {@link EventLoopGroup}. * * @param eventLoopGroup an eventLoopGroup to share * * @return a new {@link TcpServer} */ public final TcpServer runOn(EventLoopGroup eventLoopGroup) { Objects.requireNonNull(eventLoopGroup, "eventLoopGroup"); return runOn(preferNative -> eventLoopGroup); }
@Override public Mono<InetSocketAddress> bind(int port, ServiceMethodRegistry methodRegistry) { return Mono.defer( () -> { TcpServer tcpServer = TcpServer.create() .runOn(loopResources) .addressSupplier(() -> new InetSocketAddress(port)) .doOnConnection( connection -> { LOGGER.info("Accepted connection on {}", connection.channel()); connection.onDispose( () -> { LOGGER.info("Connection closed on {}", connection.channel()); connections.remove(connection); }); connections.add(connection); }); return RSocketFactory.receive() .frameDecoder( frame -> ByteBufPayload.create( frame.sliceData().retain(), frame.sliceMetadata().retain())) .acceptor(new RSocketServiceAcceptor(codec, methodRegistry)) .transport(() -> TcpServerTransport.create(tcpServer)) .start() .map(server -> this.server = server) .map(CloseableChannel::address); }); }
@Override public Mono<InetSocketAddress> bind(int port, ServiceMethodRegistry methodRegistry) { return Mono.defer( () -> { TcpServer tcpServer = TcpServer.create() .runOn(loopResources) .addressSupplier(() -> new InetSocketAddress(port)) .doOnConnection( connection -> { LOGGER.info("Accepted connection on {}", connection.channel()); connection.onDispose( () -> { LOGGER.info("Connection closed on {}", connection.channel()); connections.remove(connection); }); connections.add(connection); }); return RSocketFactory.receive() .frameDecoder( frame -> ByteBufPayload.create( frame.sliceData().retain(), frame.sliceMetadata().retain())) .acceptor(new RSocketServiceAcceptor(codec, methodRegistry)) .transport(() -> TcpServerTransport.create(tcpServer)) .start() .map(server -> this.server = server) .map(CloseableChannel::address); }); }
@Override public Mono<InetSocketAddress> bind(int port, ServiceMethodRegistry methodRegistry) { return Mono.defer( () -> { TcpServer tcpServer = TcpServer.create() .runOn(loopResources) .addressSupplier(() -> new InetSocketAddress(port)) .doOnConnection( connection -> { LOGGER.info("Accepted connection on {}", connection.channel()); connection.onDispose( () -> { LOGGER.info("Connection closed on {}", connection.channel()); connections.remove(connection); }); connections.add(connection); }); return RSocketFactory.receive() .frameDecoder( frame -> ByteBufPayload.create( frame.sliceData().retain(), frame.sliceMetadata().retain())) .acceptor(new RSocketServiceAcceptor(codec, methodRegistry)) .transport(() -> TcpServerTransport.create(tcpServer)) .start() .map(server -> this.server = server) .map(CloseableChannel::address); }); }
/** * Builds generic http server with given parameters. * * @param loopResources loop resources * @param port listen port * @param metrics gateway metrics * @return http server */ protected final HttpServer prepareHttpServer( LoopResources loopResources, int port, GatewayMetrics metrics) { return HttpServer.create() .tcpConfiguration( tcpServer -> { if (loopResources != null) { tcpServer = tcpServer.runOn(loopResources); } if (metrics != null) { tcpServer = tcpServer.doOnConnection( connection -> { metrics.incConnection(); connection.onDispose(metrics::decConnection); }); } return tcpServer.addressSupplier(() -> new InetSocketAddress(port)); }); }
/** * Creates TcpServer. * * @return tcp server */ private TcpServer newTcpServer() { return TcpServer.create() .runOn(loopResources) .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.SO_KEEPALIVE, true) .option(ChannelOption.SO_REUSEADDR, true) .addressSupplier(() -> new InetSocketAddress(config.getPort())) .bootstrap(b -> BootstrapHandlers.updateConfiguration(b, "inbound", channelInitializer)); }