com.netflix.karyon.transport.http.KaryonHttpModule Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of karyon-governator Show documentation
Show all versions of karyon-governator Show documentation
karyon-governator developed by Netflix
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);
}
}
}