Skip to content

Commit 8a7a6e0

Browse files
committed
custom thread factory support.
1 parent 4a58c04 commit 8a7a6e0

2 files changed

Lines changed: 23 additions & 7 deletions

File tree

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import io.netty.channel.ChannelOption
2525
import io.netty.channel.EventLoopGroup
2626
import io.netty.handler.logging.LogLevel
2727
import io.netty.handler.logging.LoggingHandler
28+
import io.netty.util.concurrent.DefaultThreadFactory
2829
import kotlinx.coroutines.runBlocking
2930
import kotlinx.coroutines.sync.Mutex
3031
import kotlinx.coroutines.sync.withLock
@@ -37,6 +38,7 @@ import net.ccbluex.netty.http.util.setup
3738
import net.ccbluex.netty.http.websocket.WebSocketController
3839
import org.apache.logging.log4j.LogManager
3940
import java.net.InetSocketAddress
41+
import java.util.concurrent.ThreadFactory
4042

4143

4244
/**
@@ -75,21 +77,30 @@ class HttpServer {
7577
*
7678
* @param port The port of HTTP server. `0` means to auto select one.
7779
* @param useNativeTransport Whether to use native transport (Epoll or KQueue).
80+
* @param threadFactory The thread factory to use for event loop groups.
81+
* @param loggingHandler The logging handler to use for the server bootstrap.
7882
*
7983
* @return actual port of server.
8084
*/
81-
suspend fun start(port: Int, useNativeTransport: Boolean = true): Int = lock.withLock {
85+
suspend fun start(
86+
port: Int,
87+
useNativeTransport: Boolean = true,
88+
threadFactory: ThreadFactory? = DefaultThreadFactory("NettyHttpServer"),
89+
loggingHandler: LoggingHandler? = LoggingHandler(LogLevel.INFO),
90+
): Int = lock.withLock {
8291
val b = ServerBootstrap()
8392

84-
val groups = b.setup(useNativeTransport)
93+
val groups = b.setup(useNativeTransport, threadFactory)
8594
bossGroup = groups.first
8695
workerGroup = groups.second
8796

8897
try {
8998
logger.info("Starting Netty server...")
9099
b.option(ChannelOption.SO_BACKLOG, 1024)
91-
.handler(LoggingHandler(LogLevel.INFO))
92-
.childHandler(HttpChannelInitializer(this))
100+
if (loggingHandler != null) {
101+
b.handler(loggingHandler)
102+
}
103+
b.childHandler(HttpChannelInitializer(this))
93104
val ch = b.bind(port).syncSuspend().channel()
94105
serverChannel = ch
95106
webSocketController = WebSocketController(ch)

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import io.netty.channel.socket.nio.NioServerSocketChannel
3636
import net.ccbluex.netty.http.util.TransportType.EPOLL
3737
import net.ccbluex.netty.http.util.TransportType.KQUEUE
3838
import net.ccbluex.netty.http.util.TransportType.NIO
39+
import java.util.concurrent.ThreadFactory
3940

4041
internal enum class TransportType(
4142
val serverChannelFactory: ChannelFactory<out ServerChannel>,
@@ -73,15 +74,19 @@ private val available by lazy {
7374
*
7475
* @receiver The server bootstrap to configure.
7576
* @param useNativeTransport Whether to use native transport (Epoll or KQueue).
77+
* @param threadFactory The thread factory to use for event loop groups.
7678
*
7779
* @return Parent and child group.
7880
*/
7981
@JvmOverloads
80-
fun ServerBootstrap.setup(useNativeTransport: Boolean = true): Pair<EventLoopGroup, EventLoopGroup> {
82+
fun ServerBootstrap.setup(
83+
useNativeTransport: Boolean = true,
84+
threadFactory: ThreadFactory? = null,
85+
): Pair<EventLoopGroup, EventLoopGroup> {
8186
val type = if (useNativeTransport) available else NIO
8287

83-
val parentGroup = MultiThreadIoEventLoopGroup(1, type.ioHandlerFactory)
84-
val childGroup = MultiThreadIoEventLoopGroup(type.ioHandlerFactory)
88+
val parentGroup = MultiThreadIoEventLoopGroup(1, threadFactory, type.ioHandlerFactory)
89+
val childGroup = MultiThreadIoEventLoopGroup(threadFactory, type.ioHandlerFactory)
8590
group(parentGroup, childGroup)
8691
.channelFactory(type.serverChannelFactory)
8792
return parentGroup to childGroup

0 commit comments

Comments
 (0)