Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Used fixed buffer allocator for udp channel #2248

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions rskj-core/src/main/java/co/rsk/net/discovery/UDPServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -163,6 +164,29 @@ private Bootstrap createBootstrap(EventLoopGroup group) {
.handler(new ChannelInitializer<NioDatagramChannel>() {
@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);
Expand Down
Loading