package io.micronaut.http.netty.channel;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import io.micronaut.context.annotation.BootstrapContextCompatible;
import io.micronaut.context.annotation.Primary;
import io.micronaut.context.annotation.Property;
import io.micronaut.core.util.ArgumentUtils;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.socket.ServerSocketChannel;
import io.netty.channel.socket.SocketChannel;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadFactory;
@Primary
@Singleton
@BootstrapContextCompatible
public class DefaultEventLoopGroupFactory implements EventLoopGroupFactory {
private boolean useNativeTransport = false;
private final EventLoopGroupFactory nativeFactory;
private final EventLoopGroupFactory defaultFactory;
public DefaultEventLoopGroupFactory(
NioEventLoopGroupFactory nioEventLoopGroupFactory,
@Nullable @Named(EventLoopGroupFactory.NATIVE) EventLoopGroupFactory nativeFactory) {
this.defaultFactory = nioEventLoopGroupFactory;
this.nativeFactory = nativeFactory != null ? nativeFactory : defaultFactory;
}
@Deprecated
@Inject
protected void setUseNativeTransport(@Property(name = "micronaut.server.netty.use-native-transport") @Nullable Boolean useNativeTransport) {
if (useNativeTransport != null) {
this.useNativeTransport = useNativeTransport;
}
}
@Override
public EventLoopGroup createEventLoopGroup(EventLoopGroupConfiguration configuration, ThreadFactory threadFactory) {
ArgumentUtils.requireNonNull("configuration", configuration);
ArgumentUtils.requireNonNull("threadFactory", threadFactory);
if (useNativeTransport || configuration.isPreferNativeTransport()) {
return this.nativeFactory.createEventLoopGroup(configuration, threadFactory);
} else {
return this.defaultFactory.createEventLoopGroup(configuration, threadFactory);
}
}
@Override
public EventLoopGroup createEventLoopGroup(int threads, Executor executor, @Nullable Integer ioRatio) {
return nativeFactory.createEventLoopGroup(threads, executor, ioRatio);
}
@Override
public EventLoopGroup createEventLoopGroup(int threads, @Nullable ThreadFactory threadFactory, @Nullable Integer ioRatio) {
return nativeFactory.createEventLoopGroup(threads, threadFactory, ioRatio);
}
@Override
public Class<? extends ServerSocketChannel> serverSocketChannelClass() {
return nativeFactory.serverSocketChannelClass();
}
@NonNull
@Override
public Class<? extends ServerSocketChannel> serverSocketChannelClass(EventLoopGroupConfiguration configuration) {
if (useNativeTransport || configuration != null && configuration.isPreferNativeTransport()) {
return this.nativeFactory.serverSocketChannelClass(configuration);
} else {
return this.defaultFactory.serverSocketChannelClass(configuration);
}
}
@NonNull
@Override
public Class<? extends SocketChannel> clientSocketChannelClass(@Nullable EventLoopGroupConfiguration configuration) {
if (useNativeTransport || configuration != null && configuration.isPreferNativeTransport()) {
return this.nativeFactory.clientSocketChannelClass(configuration);
} else {
return this.defaultFactory.clientSocketChannelClass(configuration);
}
}
}