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

avoid consuming receive buffers when blocked by queue #211

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
31 changes: 29 additions & 2 deletions src/main/java/org/logstash/tcp/InputLoop.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,17 @@ private static final class InputHandler extends ChannelInitializer<SocketChannel
protected void initChannel(final SocketChannel channel) throws Exception {
Decoder localCopy = decoder.copy();

// if SSL is enabled, the SSL handler must be added to the pipeline first
// if SSL is enabled, the SSL handler must be added to the pipeline FIRST
if (sslContext != null) {
channel.pipeline().addLast(SSL_HANDLER, sslContext.newHandler(channel.alloc()));
channel.pipeline().addFirst(SSL_HANDLER, sslContext.newHandler(channel.alloc()));
}

channel.pipeline().addLast(new DecoderAdapter(localCopy, logger));

// disable AUTO_READ and use ThrottleReleaseHandler as LAST handler
channel.config().setAutoRead(false);
channel.pipeline().addLast(new ThrottleReleaseHandler());

channel.closeFuture().addListener(new FlushOnCloseListener(localCopy));

if (logger.isDebugEnabled()) {
Expand All @@ -151,6 +156,28 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
super.exceptionCaught(ctx, cause);
}

/**
* This {@link ThrottleReleaseHandler} is a handler that marks the channel eligible for
* reading when the channel first becomes active or has completed a read operation, and
* is what enables this plugin to apply TCP back-pressure when it is blocked instead of
* reading bytes into buffers that will vanish when we OOM.
*
* <p>It requires the channel to be configured <em>without</em> {@code AUTO_READ}</p>
*/
private static final class ThrottleReleaseHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(final ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
ctx.channel().read();
}

@Override
public void channelReadComplete(final ChannelHandlerContext ctx) throws Exception {
super.channelReadComplete(ctx);
ctx.channel().read();
}
}

/**
* Listeners that flushes the the JRuby supplied {@link Decoder} when the socket is closed.
*/
Expand Down