Skip to content

Commit 981cae0

Browse files
wjoelhamnis
authored andcommitted
Add a parent/acceptor event loop group, use setIoRatio with epoll
Backport from main
1 parent 041d0c2 commit 981cae0

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

server/src/main/scala/org/http4s/netty/server/NettyServerBuilder.scala

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,36 @@ final class NettyServerBuilder[F[_]] private (
104104
transport match {
105105
case NettyTransport.Nio =>
106106
logger.info("Using NIO EventLoopGroup")
107-
new EventLoopHolder[NioServerSocketChannel](new NioEventLoopGroup(_))
107+
EventLoopHolder[NioServerSocketChannel](
108+
new NioEventLoopGroup(1),
109+
new NioEventLoopGroup(eventLoopThreads)
110+
)
108111
case NettyTransport.Native =>
109112
if (IOUring.isAvailable) {
110113
logger.info("Using IOUring")
111-
new EventLoopHolder[IOUringServerSocketChannel](new IOUringEventLoopGroup(_))
112-
} else if (Epoll.isAvailable) {
114+
EventLoopHolder[IOUringServerSocketChannel](
115+
new IOUringEventLoopGroup(1),
116+
new IOUringEventLoopGroup(eventLoopThreads))
117+
} else
118+
if (Epoll.isAvailable) {
113119
logger.info("Using Epoll")
114-
new EventLoopHolder[EpollServerSocketChannel](new EpollEventLoopGroup(_))
120+
val acceptorEventLoopGroup = new EpollEventLoopGroup(1)
121+
acceptorEventLoopGroup.setIoRatio(100)
122+
val workerEventLoopGroup = new EpollEventLoopGroup(eventLoopThreads)
123+
workerEventLoopGroup.setIoRatio(80)
124+
EventLoopHolder[EpollServerSocketChannel](
125+
acceptorEventLoopGroup,
126+
workerEventLoopGroup)
115127
} else if (KQueue.isAvailable) {
116128
logger.info("Using KQueue")
117-
new EventLoopHolder[KQueueServerSocketChannel](new KQueueEventLoopGroup(_))
129+
EventLoopHolder[KQueueServerSocketChannel](
130+
new KQueueEventLoopGroup(1),
131+
new KQueueEventLoopGroup(eventLoopThreads))
118132
} else {
119133
logger.info("Falling back to NIO EventLoopGroup")
120-
new EventLoopHolder[NioServerSocketChannel](new NioEventLoopGroup(_))
134+
EventLoopHolder[NioServerSocketChannel](
135+
new NioEventLoopGroup(1),
136+
new NioEventLoopGroup(eventLoopThreads))
121137
}
122138
}
123139

@@ -237,20 +253,18 @@ final class NettyServerBuilder[F[_]] private (
237253
engine
238254
}))
239255

240-
private class EventLoopHolder[A <: ServerChannel](make: Int => MultithreadEventLoopGroup)(implicit
241-
classTag: ClassTag[A]
242-
) {
243-
private val boss = make(1)
244-
private val worker = make(eventLoopThreads)
256+
case class EventLoopHolder[A <: ServerChannel](
257+
parent: MultithreadEventLoopGroup,
258+
eventLoop: MultithreadEventLoopGroup)(implicit classTag: ClassTag[A]) {
245259
def shutdown(): Unit = {
246-
worker.shutdownGracefully(1000, 1500, TimeUnit.MILLISECONDS)
247-
boss.shutdownGracefully(1000, 1500, TimeUnit.MILLISECONDS)
260+
eventLoop.shutdownGracefully(1000, 1500, TimeUnit.MILLISECONDS)
261+
parent.shutdownGracefully(1000, 1500, TimeUnit.MILLISECONDS)
248262
()
249263
}
250264
def runtimeClass: Class[A] = classTag.runtimeClass.asInstanceOf[Class[A]]
251265
def configure(bootstrap: ServerBootstrap) = {
252266
val configured = bootstrap
253-
.group(boss, worker)
267+
.group(parent, eventLoop)
254268
.channel(runtimeClass)
255269
.option(ChannelOption.SO_REUSEADDR, java.lang.Boolean.TRUE)
256270
.childOption(ChannelOption.SO_REUSEADDR, java.lang.Boolean.TRUE)

0 commit comments

Comments
 (0)