Skip to content

Instantly share code, notes, and snippets.

@Hc747
Last active May 14, 2019 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hc747/25477646e05e77a861a19bbf7611bece to your computer and use it in GitHub Desktop.
Save Hc747/25477646e05e77a861a19bbf7611bece to your computer and use it in GitHub Desktop.
import io.netty.bootstrap.ServerBootstrap
import io.netty.channel.EventLoopGroup
import io.netty.channel.epoll.Epoll
import io.netty.channel.epoll.EpollEventLoopGroup
import io.netty.channel.epoll.EpollServerSocketChannel
import io.netty.channel.kqueue.KQueue
import io.netty.channel.kqueue.KQueueEventLoopGroup
import io.netty.channel.kqueue.KQueueServerSocketChannel
import io.netty.channel.nio.NioEventLoopGroup
import io.netty.channel.socket.ServerSocketChannel
import io.netty.channel.socket.nio.NioServerSocketChannel
class EventLoopGroupProxy(val channel: KClass<out ServerSocketChannel>, group: EventLoopGroup) : EventLoopGroup by group {
companion object {
fun create(parallelism: Int): EventLoopGroupProxy {
return when {
KQueue.isAvailable() -> EventLoopGroupProxy(KQueueServerSocketChannel::class, KQueueEventLoopGroup(parallelism))
Epoll.isAvailable() -> EventLoopGroupProxy(EpollServerSocketChannel::class, EpollEventLoopGroup(parallelism))
else -> EventLoopGroupProxy(NioServerSocketChannel::class, NioEventLoopGroup(parallelism))
}
}
fun createBootstrap(proxy: EventLoopGroupProxy): ServerBootstrap {
return createBootstrap(proxy, proxy)
}
fun createBootstrap(parent: EventLoopGroupProxy, child: EventLoopGroupProxy): ServerBootstrap {
return ServerBootstrap().channel(parent.channel.java).group(parent, child)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment