From 4d7f1567bd6cc710a41a8c898b3b4903488af917 Mon Sep 17 00:00:00 2001 From: Volodymyr Kravets Date: Mon, 29 Jan 2024 13:56:50 +0200 Subject: [PATCH] Used fixed buffer allocator for udp channel --- .../java/co/rsk/net/discovery/UDPServer.java | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/rskj-core/src/main/java/co/rsk/net/discovery/UDPServer.java b/rskj-core/src/main/java/co/rsk/net/discovery/UDPServer.java index f7e1bf18d49..0ad594526fd 100644 --- a/rskj-core/src/main/java/co/rsk/net/discovery/UDPServer.java +++ b/rskj-core/src/main/java/co/rsk/net/discovery/UDPServer.java @@ -22,10 +22,9 @@ import co.rsk.util.ExecState; import com.google.common.annotations.VisibleForTesting; import io.netty.bootstrap.Bootstrap; -import io.netty.channel.Channel; -import io.netty.channel.ChannelInitializer; -import io.netty.channel.EventLoopGroup; +import io.netty.channel.*; import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.DatagramChannelConfig; import io.netty.channel.socket.nio.NioDatagramChannel; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -43,6 +42,8 @@ public class UDPServer implements InternalService { private static final Logger logger = LoggerFactory.getLogger(UDPServer.class); + private static final int BUFFER_SIZE = 8_192; + private final int port; private final String address; @@ -163,6 +164,29 @@ private Bootstrap createBootstrap(EventLoopGroup group) { .handler(new ChannelInitializer() { @Override public void initChannel(@Nonnull NioDatagramChannel ch) { + DatagramChannelConfig channelConfig = ch.config(); + channelConfig.setRecvByteBufAllocator(new FixedRecvByteBufAllocator(BUFFER_SIZE)); + + Integer defaultSndBuf = channelConfig.getOption(ChannelOption.SO_SNDBUF); + if (defaultSndBuf == null || defaultSndBuf < BUFFER_SIZE) { + logger.info("Default {} size of {} bytes is not sufficient. Changing to {}", + ChannelOption.SO_SNDBUF, defaultSndBuf, BUFFER_SIZE); + channelConfig.setOption(ChannelOption.SO_SNDBUF, BUFFER_SIZE); + } + + Integer defaultRcvBuf = channelConfig.getOption(ChannelOption.SO_RCVBUF); + if (defaultRcvBuf == null || defaultRcvBuf < BUFFER_SIZE) { + logger.info("Default {} size of {} bytes is not sufficient. Changing to {}", + ChannelOption.SO_RCVBUF, defaultRcvBuf, BUFFER_SIZE); + channelConfig.setOption(ChannelOption.SO_RCVBUF, BUFFER_SIZE); + } + + logger.info("Init channel with {}({}), {}={}, {}={}", + FixedRecvByteBufAllocator.class.getSimpleName(), + BUFFER_SIZE, + ChannelOption.SO_SNDBUF, channelConfig.getOption(ChannelOption.SO_SNDBUF), + ChannelOption.SO_RCVBUF, channelConfig.getOption(ChannelOption.SO_RCVBUF)); + ch.pipeline().addLast(new PacketDecoder()); UDPChannel udpChannel = new UDPChannel(ch, peerExplorer); peerExplorer.setUDPChannel(udpChannel);