问题描述
我正在使用Netty-4.1.0-Beta5,我正在使用DefaultEventExecutorGroup(具有2个线程)作为长时间运行的处理程序(解除阻塞I/O线程),在服务器运行了几个小时之后,我看到了大约2155个组每个都有1个线程
I am using Netty-4.1.0-Beta5, i am using DefaultEventExecutorGroup(with 2 threads) for long running handler (to unblock I/O thread), after running server for couple of hours, i am seeing around 2155 groups and each with 1 thread
defaultEventExecutorGroup-10-1 tid=134 [WAITING]
sun.misc.Unsafe.park(boolean, long) Unsafe.java
java.util.concurrent.locks.LockSupport.parkNanos(Object, long) LockSupport.java:215
java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(long) AbstractQueuedSynchronizer.java:2078
java.util.concurrent.LinkedBlockingQueue.poll(long, TimeUnit) LinkedBlockingQueue.java:467
io.netty.util.concurrent.SingleThreadEventExecutor.takeTask() SingleThreadEventExecutor.java:194
io.netty.util.concurrent.DefaultEventExecutor.run() DefaultEventExecutor.java:54
io.netty.util.concurrent.SingleThreadEventExecutor$4.run() SingleThreadEventExecutor.java:703
io.netty.util.concurrent.DefaultThreadFactory$DefaultRunnableDecorator.run() DefaultThreadFactory.java:137
java.lang.Thread.run() Thread.java:745
..
defaultEventExecutorGroup-1900-1 tid=2114 [WAITING]
大多数HTTP请求都需要20秒钟以上才能响应,这是我的代码:
and most of HTTP requests taking more than 20 seconds to respond, here is my code:
class NettyServer extends Logging {
val allChannels = new DefaultChannelGroup("enter", GlobalEventExecutor.INSTANCE)
val webSocketConnections = new WebSocketConnections("websockets")
val sslManager: Option[SslManager] = Some(new SslManager(this))
def start(): Unit = {
allChannels.clear()
val bootstrap = new ServerBootstrap()
bootstrap.group(new NioEventLoopGroup(), new NioEventLoopGroup())
bootstrap.channel(classOf[NioServerSocketChannel])
bootstrap.childOption[java.lang.Boolean](ChannelOption.TCP_NODELAY, true)
bootstrap.childHandler(new PipelineFactory(this))
val bindFutures = "0.0.0.0".split(",").map(address => {
address.trim match
case "0.0.0.0" =>
bootstrap.bind(9999)
case _ if (!address.isEmpty) =>
bootstrap.bind(address, 9999)
})
allChannels.addAll(bindFutures.map(_.channel).toList)
val latch = new CountDownLatch(bindFutures.length)
val bindFutureListener = new ChannelFutureListener {
def operationComplete(future: ChannelFuture) = {
latch.countDown
}
}
bindFutures.foreach(_.addListener(bindFutureListener))
latch.await
}
def stop(): Unit = {
val future = allChannels.close()
future.awaitUninterruptibly()
allChannels.clear()
}
}
class PipelineFactory(server: NettyServer) extends ChannelInitializer[SocketChannel] with Logging {
override def initChannel(ch: SocketChannel) {
val pipeline = ch.pipeline
pipeline.addLast("ssl", new SslHandler(server.sslManager.get.createSSLEngine()))
pipeline.addLast("httpRequestDecoder", new HttpRequestDecoder(4096, 8192, 8192, false))
pipeline.addLast("httpResponseEncoder", new HttpResponseEncoder())
pipeline.addLast("chunkAggregator", new HttpObjectAggregator(65536))
pipeline.addLast("idleStateHandler", new IdleStateHandler(0, 0, 0))
val corsConfig = CorsConfig.withAnyOrigin()
pipeline.addLast("corsHandler", new CorsHandler(corsConfig))
pipeline.addLast(new DefaultEventExecutorGroup(2), "handler", new MyHandler())
}
}
任何想法,我的代码有什么问题?如果我使用curl在紧密循环中发送10000个HTTP请求,那么我可以重新创建此问题.感谢您的帮助!
Any idea, what is wrong with my code? if i send 10000 HTTP requests in tight loop using curl then i could recreate this issue.Thanks for the help!
推荐答案
您只需创建一次DefaultEventExecutorGroup并共享该实例.目前,您为每个频道创建了
You need to create the DefaultEventExecutorGroup only one time and share the instance. At the moment you create it for each Channel
这篇关于DefaultEventExecutorGroup线程不断增长,服务器对请求的响应速度很慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!