【发布时间】:2019-09-16 18:18:46
【问题描述】:
按照其他地方的建议,我正在尝试在 Netty 管道中并行化我的最终入站处理程序
public final class EchoServer {
private EventLoopGroup group = new NioEventLoopGroup();
private UnorderedThreadPoolEventExecutor workers = new UnorderedThreadPoolEventExecutor(10);
public void start(int port) throws InterruptedException {
try {
Bootstrap b = new Bootstrap();
b.group(group).channel(NioDatagramChannel.class).option(ChannelOption.SO_BROADCAST, true)
.handler(new ChannelInitializer<NioDatagramChannel>() {
@Override
protected void initChannel(NioDatagramChannel channel) throws Exception {
channel.pipeline().addLast(workers, new SimpleChannelInboundHandler<DatagramPacket>() {
@Override
public void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
System.err.println(packet);
// Simulated database delay that I have to wait to occur before repsonding
Thread.sleep(1000);
ctx.write(new DatagramPacket(Unpooled.copiedBuffer("goodbye", StandardCharsets.ISO_8859_1), packet.sender()));
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
}
});
}
});
b.bind(port).sync().channel().closeFuture().await();
} finally {
group.shutdownGracefully();
}
}
public void stop() {
group.shutdownGracefully();
}
}
作为测试,我有十个同时连接的客户端,我正在测量处理所有请求的执行时间。正如预期的那样,1 秒的延迟和顺序执行只需要 10 多秒。我正在尝试将执行时间缩短到 2 秒以下以证明并行处理。
据我了解,将处理程序添加到具有显式分配的执行程序的管道应该并行化处理程序在执行程序中跨线程工作。
我发现在我添加并行处理时我的客户没有收到响应,而不是看到性能提高。线程睡眠用于模拟将传入数据写入数据库所需的潜在时间。我在这里做错了什么吗?
【问题讨论】:
-
显然这是 Netty 在 UDP 通道方面的一个缺点。 github.com/netty/netty/issues/1706
标签: parallel-processing netty blocking