Class ConnectionHandler
- java.lang.Object
-
- io.netty.channel.ChannelHandlerAdapter
-
- io.netty.channel.ChannelInboundHandlerAdapter
-
- io.netty.channel.ChannelDuplexHandler
-
- org.logstash.beats.ConnectionHandler
-
- All Implemented Interfaces:
io.netty.channel.ChannelHandler
,io.netty.channel.ChannelInboundHandler
,io.netty.channel.ChannelOutboundHandler
public class ConnectionHandler extends io.netty.channel.ChannelDuplexHandler
Manages the connection state to the beats client.
-
-
Field Summary
Fields Modifier and Type Field Description static io.netty.util.AttributeKey<AtomicBoolean>
CHANNEL_SEND_KEEP_ALIVE
-
Constructor Summary
Constructors Constructor Description ConnectionHandler()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
channelActive(io.netty.channel.ChannelHandlerContext ctx)
void
channelRead(io.netty.channel.ChannelHandlerContext ctx, Object msg)
Sets the flag that the keep alive should be sent.boolean
sendKeepAlive(io.netty.channel.ChannelHandlerContext ctx)
Determine if this channel has finished processing it's payload.void
userEventTriggered(io.netty.channel.ChannelHandlerContext ctx, Object evt)
-
Methods inherited from class io.netty.channel.ChannelDuplexHandler
bind, close, connect, deregister, disconnect, flush, read, write
-
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelInactive, channelReadComplete, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, handlerRemoved, isSharable
-
-
-
-
Field Detail
-
CHANNEL_SEND_KEEP_ALIVE
public static final io.netty.util.AttributeKey<AtomicBoolean> CHANNEL_SEND_KEEP_ALIVE
-
-
Method Detail
-
channelActive
public void channelActive(io.netty.channel.ChannelHandlerContext ctx) throws Exception
- Specified by:
channelActive
in interfaceio.netty.channel.ChannelInboundHandler
- Overrides:
channelActive
in classio.netty.channel.ChannelInboundHandlerAdapter
- Throws:
Exception
-
channelRead
public void channelRead(io.netty.channel.ChannelHandlerContext ctx, Object msg) throws Exception
Sets the flag that the keep alive should be sent.BeatsHandler
will un-set it. It is important that this handler comes before theBeatsHandler
in the channel pipeline. Note - For large payloads, this method may be called many times more often then the BeatsHandler#channelRead due to decoder aggregating the payload.- Specified by:
channelRead
in interfaceio.netty.channel.ChannelInboundHandler
- Overrides:
channelRead
in classio.netty.channel.ChannelInboundHandlerAdapter
- Throws:
Exception
-
userEventTriggered
public void userEventTriggered(io.netty.channel.ChannelHandlerContext ctx, Object evt) throws Exception
IdleState.WRITER_IDLE
If no response has been issued after the configured write idle timeout viaIdleStateHandler
, then start to issue a TCP keep alive. This can happen when the pipeline is blocked. Pending (blocked) batches are in either in the EventLoop attempting to write to the queue, or may be in a taskPending queue waiting for the EventLoop to unblock. This keep alive holds open the TCP connection from the Beats client so that it will not timeout and retry which could result in duplicates.
IdleState.ALL_IDLE
If no read or write has been issued after the configured all idle timeout viaIdleStateHandler
, then close the connection. This is really only happens for beats that are sending sparse amounts of data, and helps to the keep the number of concurrent connections in check. Note that ChannelOption.SO_LINGER = 0 needs to be set to ensure we clean up quickly. Also note that the above keep alive counts as a not-idle, and thus the keep alive will prevent this logic from closing the connection. For this reason, we stop sending keep alives when there are no more pending batches to allow this idle close timer to take effect.- Specified by:
userEventTriggered
in interfaceio.netty.channel.ChannelInboundHandler
- Overrides:
userEventTriggered
in classio.netty.channel.ChannelInboundHandlerAdapter
- Throws:
Exception
-
sendKeepAlive
public boolean sendKeepAlive(io.netty.channel.ChannelHandlerContext ctx)
Determine if this channel has finished processing it's payload. If it has not, send a TCP keep alive. Note - for this to work, the following must be true:- This Handler comes before the
BeatsHandler
in the channel's pipeline - This Handler is associated to an
EventLoopGroup
that has guarantees that the associatedEventLoop
will never block. - The
BeatsHandler
un-sets only after it has processed this channel's payload.
- Parameters:
ctx
- theChannelHandlerContext
used to curry the flag.- Returns:
- Returns true if this channel/connection has NOT finished processing it's payload. False otherwise.
- This Handler comes before the
-
-