use of org.apache.flink.shaded.netty4.io.netty.handler.timeout.IdleStateEvent in project lightconf by haifeiWu.
the class ClientHandler method userEventTriggered.
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent) {
IdleStateEvent e = (IdleStateEvent) evt;
switch(e.state()) {
case WRITER_IDLE:
PingMsg pingMsg = new PingMsg();
ctx.writeAndFlush(pingMsg);
break;
default:
break;
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.timeout.IdleStateEvent in project java by wavefrontHQ.
the class ConnectionHandler method userEventTriggered.
/**
* {@inheritDoc}
* <br/>
* <p>
* <strong>IdleState.WRITER_IDLE <br/></strong>
* If no response has been issued after the configured write idle timeout via {@link io.netty.handler.timeout.IdleStateHandler}, 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.
* <br/>
* </p><p>
* <strong>IdleState.ALL_IDLE <br/></strong>
* If no read or write has been issued after the configured all idle timeout via {@link io.netty.handler.timeout.IdleStateHandler}, 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.
* </p>
*/
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
IdleStateEvent e;
if (evt instanceof IdleStateEvent) {
e = (IdleStateEvent) evt;
if (e.state() == IdleState.WRITER_IDLE) {
if (sendKeepAlive(ctx)) {
ChannelFuture f = ctx.writeAndFlush(new Ack(Protocol.VERSION_2, 0));
if (logger.isTraceEnabled()) {
logger.trace("{}: sending keep alive ack to libbeat", ctx.channel().id().asShortText());
f.addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
logger.trace("{}: acking was successful", ctx.channel().id().asShortText());
} else {
logger.trace("{}: acking failed", ctx.channel().id().asShortText());
}
});
}
}
} else if (e.state() == IdleState.ALL_IDLE) {
logger.debug("{}: reader and writer are idle, closing remote connection", ctx.channel().id().asShortText());
ctx.flush();
ChannelFuture f = ctx.close();
if (logger.isTraceEnabled()) {
f.addListener((future) -> {
if (future.isSuccess()) {
logger.trace("closed ctx successfully");
} else {
logger.trace("could not close ctx");
}
});
}
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.timeout.IdleStateEvent in project neo4j by neo4j.
the class IdleChannelReaperHandler method userEventTriggered.
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent && evt == IdleStateEvent.ALL_IDLE_STATE_EVENT) {
final InetSocketAddress socketAddress = (InetSocketAddress) ctx.channel().remoteAddress();
final AdvertisedSocketAddress address = new AdvertisedSocketAddress(socketAddress.getHostName(), socketAddress.getPort());
nonBlockingChannels.remove(address);
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.timeout.IdleStateEvent in project swift by luastar.
the class HttpChannelHandler method userEventTriggered.
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
super.userEventTriggered(ctx, evt);
if (evt instanceof IdleStateEvent) {
IdleStateEvent event = (IdleStateEvent) evt;
if (ctx.channel().isActive() && IdleState.ALL_IDLE.equals(event.state())) {
logger.info("close timeout channel {}", ctx.channel().id());
ctx.channel().close();
}
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.timeout.IdleStateEvent in project xian by happyyangyuan.
the class RpcServerInitializer method initChannel.
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
if (sslCtx != null) {
pipeline.addLast(sslCtx.newHandler(ch.alloc()));
}
// Add the text line codec combination first,分隔符就是"\r\n$end!"
pipeline.addLast(new DelimiterBasedFrameDecoder(Integer.MAX_VALUE, new ByteBuf[] { Unpooled.wrappedBuffer(Constant.RPC_DELIMITER.getBytes()) }));
// the encoder and decoder are static as these are sharable
pipeline.addLast(DECODER);
pipeline.addLast(ENCODER);
pipeline.addLast(/*IDLE_EVENT_HANDLER*/
new RpcServerIdleStateHandler());
pipeline.addLast(new ChannelDuplexHandler() {
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent) {
IdleStateEvent e = (IdleStateEvent) evt;
if (e.state() == IdleState.ALL_IDLE) {
LOG.info(new JSONObject() {
{
put("type", "rpcIdle");
put("description", String.format("关闭闲置连接: timeOut=%sms", RpcServerIdleStateHandler.IDLE_TIMEOUT_IN_MILLI));
put("detail", ctx.channel().remoteAddress());
}
});
ctx.close();
} else {
LOG.info("Nothing need to do: " + e.state().name());
}
}
}
});
// 注意:readHandler必须在idleHandler监听器之后!readHandler必须在idleHandler监听器之后!readHandler必须在idleHandler监听器之后!
// 接上:否则,就没法触发idleHandler了
pipeline.addLast(new RpcServerJsonDecoder());
pipeline.addLast(new RpcServerStreamHandler());
pipeline.addLast(new RpcServerDefaultHandler());
}
Aggregations