Skip to content

Commit ad4d9b5

Browse files
committed
Allow to disable native transport
1 parent 68e2d2a commit ad4d9b5

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

src/main/kotlin/net/ccbluex/netty/http/HttpServer.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff 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

src/main/kotlin/net/ccbluex/netty/http/util/TransportDetector.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package net.ccbluex.netty.http.util
2121

2222
import io.netty.bootstrap.ServerBootstrap
23+
import io.netty.channel.ChannelFactory
2324
import io.netty.channel.EventLoopGroup
2425
import io.netty.channel.ServerChannel
2526
import 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
}

0 commit comments

Comments
 (0)