Search in sources :

Example 16 with IdleStateEvent

use of 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;
        }
    }
}
Also used : IdleStateEvent(io.netty.handler.timeout.IdleStateEvent)

Example 17 with IdleStateEvent

use of 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");
                    }
                });
            }
        }
    }
}
Also used : IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) ChannelFuture(io.netty.channel.ChannelFuture) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) AttributeKey(io.netty.util.AttributeKey) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) Logger(org.apache.logging.log4j.Logger) IdleState(io.netty.handler.timeout.IdleState) ChannelFutureListener(io.netty.channel.ChannelFutureListener) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LogManager(org.apache.logging.log4j.LogManager) ChannelFuture(io.netty.channel.ChannelFuture)

Example 18 with IdleStateEvent

use of 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);
    }
}
Also used : IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) InetSocketAddress(java.net.InetSocketAddress) AdvertisedSocketAddress(org.neo4j.helpers.AdvertisedSocketAddress)

Example 19 with IdleStateEvent

use of 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();
        }
    }
}
Also used : IdleStateEvent(io.netty.handler.timeout.IdleStateEvent)

Example 20 with IdleStateEvent

use of 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());
}
Also used : DelimiterBasedFrameDecoder(io.netty.handler.codec.DelimiterBasedFrameDecoder) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) ChannelPipeline(io.netty.channel.ChannelPipeline) IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) JSONObject(com.alibaba.fastjson.JSONObject) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) JSONObject(com.alibaba.fastjson.JSONObject)

Aggregations

IdleStateEvent (io.netty.handler.timeout.IdleStateEvent)30 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)7 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)6 ChannelDuplexHandler (io.netty.channel.ChannelDuplexHandler)4 Channel (io.netty.channel.Channel)3 SocketChannel (io.netty.channel.socket.SocketChannel)3 SslHandshakeCompletionEvent (io.netty.handler.ssl.SslHandshakeCompletionEvent)3 CompleteEvent (com.netflix.netty.common.HttpLifecycleChannelHandler.CompleteEvent)2 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelFutureListener (io.netty.channel.ChannelFutureListener)2 ChannelPipeline (io.netty.channel.ChannelPipeline)2 SslHandler (io.netty.handler.ssl.SslHandler)2 InetSocketAddress (java.net.InetSocketAddress)2 JSONObject (com.alibaba.fastjson.JSONObject)1 BaseMessage (com.bonree.brfs.common.net.tcp.BaseMessage)1 BaseResponse (com.bonree.brfs.common.net.tcp.BaseResponse)1 TokenMessage (com.bonree.brfs.common.net.tcp.TokenMessage)1 CompleteReason (com.netflix.netty.common.HttpLifecycleChannelHandler.CompleteReason)1 HttpRequestReadTimeoutEvent (com.netflix.netty.common.HttpRequestReadTimeoutEvent)1 OutboundException (com.netflix.zuul.exception.OutboundException)1