All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.karyon.transport.http.KaryonHttpModule Maven / Gradle / Ivy

There is a newer version: 2.1.00-RC6
Show newest version
package com.netflix.karyon.transport.http;

import com.google.inject.Key;
import com.google.inject.binder.LinkedBindingBuilder;
import com.google.inject.multibindings.MapBinder;
import com.netflix.karyon.transport.AbstractServerModule;
import com.netflix.karyon.transport.AbstractServerModule.ServerConfig;
import com.netflix.karyon.transport.AbstractServerModule.ServerConfigBuilder;
import com.netflix.karyon.transport.http.KaryonHttpModule.HttpServerConfigBuilder;
import io.reactivex.netty.protocol.http.server.HttpServer;
import io.reactivex.netty.server.RxServer;

import static com.netflix.karyon.utils.TypeUtils.keyFor;

/**
 * @author Tomasz Bak
 */
public abstract class KaryonHttpModule extends AbstractServerModule {

    protected final Key> routerKey;
    protected final Key> httpServerKey;
    protected final Key> interceptorSupportKey;

    private final GovernatorHttpInterceptorSupport interceptorSupportInstance = new GovernatorHttpInterceptorSupport();

    protected KaryonHttpModule(String moduleName, Class iType, Class oType) {
        super(moduleName, iType, oType);

        routerKey = keyFor(HttpRequestRouter.class, iType, oType, nameAnnotation);
        interceptorSupportKey = keyFor(GovernatorHttpInterceptorSupport.class, iType, oType, nameAnnotation);
        httpServerKey = keyFor(HttpServer.class, iType, oType, nameAnnotation);
    }

    @Override
    protected void configure() {
        configureServer();

        bind(serverConfigKey).toInstance(serverConfigBuilder.build());
        bind(interceptorSupportKey).toInstance(interceptorSupportInstance);

        MapBinder.newMapBinder(binder(), String.class, RxServer.class).addBinding(nameAnnotation.value()).toProvider(
                new HttpRxServerProvider>(nameAnnotation.value(), iType, oType)
        ).asEagerSingleton();
    }

    @Override
    protected HttpServerConfigBuilder newServerConfigBuilder() {
        return new HttpServerConfigBuilder();
    }

    protected LinkedBindingBuilder> bindRouter() {
        return bind(routerKey);
    }

    protected GovernatorHttpInterceptorSupport interceptorSupport() {
        return interceptorSupportInstance;
    }

    public static class HttpServerConfig extends ServerConfig {

        private final int threadPoolSize;

        public HttpServerConfig(int port, int threadPoolSize) {
            super(port);
            this.threadPoolSize = threadPoolSize;
        }

        public int getThreadPoolSize() {
            return threadPoolSize;
        }
    }

    public static class HttpServerConfigBuilder extends ServerConfigBuilder {

        protected int poolSize = Runtime.getRuntime().availableProcessors();

        public HttpServerConfigBuilder threadPoolSize(int poolSize) {
            this.poolSize = poolSize;
            return this;
        }

        @Override
        public HttpServerConfig build() {
            return new HttpServerConfig(port, poolSize);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy