Search in sources :

Example 1 with GatewayConfig

use of com.accenture.trac.config.GatewayConfig in project tracdap by finos.

the class TracPlatformGateway method doStartup.

@Override
protected void doStartup(Duration startupTimeout) throws InterruptedException {
    GatewayConfig gatewayConfig;
    short proxyPort;
    List<Route> routes;
    try {
        log.info("Preparing gateway config...");
        var rawGatewayConfig = configManager.loadRootConfigObject(GatewayConfig.class);
        gatewayConfig = ConfigTranslator.translateServiceRoutes(rawGatewayConfig);
        proxyPort = (short) gatewayConfig.getPort();
        routes = RouteBuilder.buildAll(gatewayConfig.getRoutesList());
        log.info("Gateway config looks ok");
    } catch (Exception e) {
        var errorMessage = "There was an error preparing the gateway config: " + e.getMessage();
        log.error(errorMessage, e);
        throw new EStartup(errorMessage, e);
    }
    try {
        log.info("Starting the gateway server on port {}...", proxyPort);
        // The protocol negotiator is the top level initializer for new inbound connections
        var protocolNegotiator = new HttpProtocolNegotiator(gatewayConfig, routes);
        // TODO: Review configuration of thread pools and channel options
        bossGroup = new NioEventLoopGroup(2, new DefaultThreadFactory("boss"));
        workerGroup = new NioEventLoopGroup(6, new DefaultThreadFactory("worker"));
        var bootstrap = new ServerBootstrap().group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(protocolNegotiator).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);
        // Bind and start to accept incoming connections.
        var startupFuture = bootstrap.bind(proxyPort);
        // Block until the server channel is ready - it's just easier this way!
        // The sync call will rethrow any errors, so they can be handled before leaving the start() method
        startupFuture.await();
        if (startupFuture.isSuccess()) {
            var socket = startupFuture.channel().localAddress();
            log.info("Server socket open: {}", socket);
        } else {
            var cause = startupFuture.cause();
            var message = "Server socket could not be opened: " + cause.getMessage();
            log.error(message);
            throw new EStartup(message, cause);
        }
    // No need to keep a reference to the server channel
    // Shutdown is managed using the event loop groups
    } catch (Exception e) {
        if (workerGroup != null)
            workerGroup.shutdownGracefully();
        if (bossGroup != null)
            bossGroup.shutdownGracefully();
        if (Set.of(RuntimeException.class, InterruptedException.class).contains(e.getClass()))
            throw e;
        else
            throw new EStartup(e.getMessage(), e);
    }
}
Also used : DefaultThreadFactory(io.netty.util.concurrent.DefaultThreadFactory) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) EStartup(com.accenture.trac.common.exception.EStartup) Route(com.accenture.trac.gateway.exec.Route) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) GatewayConfig(com.accenture.trac.config.GatewayConfig)

Aggregations

EStartup (com.accenture.trac.common.exception.EStartup)1 GatewayConfig (com.accenture.trac.config.GatewayConfig)1 Route (com.accenture.trac.gateway.exec.Route)1 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)1 DefaultThreadFactory (io.netty.util.concurrent.DefaultThreadFactory)1