File tree Expand file tree Collapse file tree
src/main/kotlin/net/ccbluex/netty/http Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -65,13 +65,14 @@ class HttpServer {
6565 * Starts the Netty server on the specified port.
6666 *
6767 * @param port The port of HTTP server. `0` means to auto select one.
68+ * @param useNativeTransport Whether to use native transport (Epoll or KQueue).
6869 *
6970 * @return actual port of server.
7071 */
71- fun start (port : Int ): Int = lock.withLock {
72+ fun start (port : Int , useNativeTransport : Boolean = true ): Int = lock.withLock {
7273 val b = ServerBootstrap ()
7374
74- val groups = TransportType .apply (b)
75+ val groups = TransportType .apply (b, useNativeTransport )
7576 bossGroup = groups.first
7677 workerGroup = groups.second
7778
Original file line number Diff line number Diff line change 2020package net.ccbluex.netty.http.util
2121
2222import io.netty.bootstrap.ServerBootstrap
23+ import io.netty.channel.ChannelFactory
2324import io.netty.channel.EventLoopGroup
2425import io.netty.channel.ServerChannel
2526import io.netty.channel.epoll.EpollEventLoopGroup
@@ -81,13 +82,17 @@ internal sealed interface TransportType {
8182 * Set the channel factory and event loop groups for the given server bootstrap.
8283 *
8384 * @param bootstrap The server bootstrap to configure.
85+ * @param useNativeTransport Whether to use native transport (Epoll or KQueue).
86+ *
8487 * @return Parent and child group.
8588 */
86- fun apply (bootstrap : ServerBootstrap ): Pair <EventLoopGroup , EventLoopGroup > {
87- val parentGroup = available.newParentGroup()
88- val childGroup = available.newChildGroup()
89+ fun apply (bootstrap : ServerBootstrap , useNativeTransport : Boolean ): Pair <EventLoopGroup , EventLoopGroup > {
90+ val type = if (useNativeTransport) available else Nio
91+
92+ val parentGroup = type.newParentGroup()
93+ val childGroup = type.newChildGroup()
8994 bootstrap.group(parentGroup, childGroup)
90- .channelFactory(available ::newServerChannel)
95+ .channelFactory(ChannelFactory (type ::newServerChannel) )
9196 return parentGroup to childGroup
9297 }
9398 }
You can’t perform that action at this time.
0 commit comments