Search in sources :

Example 21 with IdleStateEvent

use of io.netty.handler.timeout.IdleStateEvent in project cdap by caskdata.

the class OutboundHandler method userEventTriggered.

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (!(evt instanceof IdleStateEvent)) {
        ctx.fireUserEventTriggered(evt);
        return;
    }
    if (IdleState.ALL_IDLE == ((IdleStateEvent) evt).state()) {
        if (requestInProgress) {
            LOG.trace("Request is in progress, so not closing channel.");
        } else {
            // No data has been sent or received for a while. Close channel.
            Channel channel = ctx.channel();
            channel.close();
            LOG.trace("No data has been sent or received for channel '{}' for more than the configured idle timeout. " + "Closing the channel. Local Address: {}, Remote Address: {}", channel, channel.localAddress(), channel.remoteAddress());
        }
    }
}
Also used : IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) Channel(io.netty.channel.Channel)

Example 22 with IdleStateEvent

use of io.netty.handler.timeout.IdleStateEvent in project java by wavefrontHQ.

the class Ingester method addIdleTimeoutHandler.

/**
 * Adds an idle timeout handler to the given pipeline
 *
 * @param pipeline the pipeline to add the idle timeout handler
 */
protected void addIdleTimeoutHandler(final ChannelPipeline pipeline) {
    // Shared across all reports for proper batching
    pipeline.addLast("idleStateHandler", new IdleStateHandler(CHANNEL_IDLE_TIMEOUT_IN_SECS_DEFAULT, 0, 0));
    pipeline.addLast("idleChannelTerminator", new ChannelDuplexHandler() {

        @Override
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
            if (evt instanceof IdleStateEvent) {
                if (((IdleStateEvent) evt).state() == IdleState.READER_IDLE) {
                    logger.warning("terminating connection to graphite client due to inactivity after " + CHANNEL_IDLE_TIMEOUT_IN_SECS_DEFAULT + "s: " + ctx.channel());
                    ctx.close();
                }
            }
        }
    });
}
Also used : IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext)

Example 23 with IdleStateEvent

use of io.netty.handler.timeout.IdleStateEvent in project java by wavefrontHQ.

the class HistogramLineIngester method initChannel.

@Override
protected void initChannel(Channel ch) throws Exception {
    // Round robin channel to handler assignment.
    int idx = (int) (Math.abs(connectionId.getAndIncrement()) % handlers.size());
    ChannelHandler handler = handlers.get(idx);
    // Add decoders and timeout, add handler()
    ChannelPipeline pipeline = ch.pipeline();
    pipeline.addLast(new LineBasedFrameDecoder(MAXIMUM_FRAME_LENGTH, true, false), new StringDecoder(Charsets.UTF_8), new IdleStateHandler(CHANNEL_IDLE_TIMEOUT_IN_SECS_DEFAULT, 0, 0), new ChannelDuplexHandler() {

        @Override
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
            if (evt instanceof IdleStateEvent) {
                if (((IdleStateEvent) evt).state() == IdleState.READER_IDLE) {
                    logger.warning("terminating connection to histogram client due to inactivity after " + CHANNEL_IDLE_TIMEOUT_IN_SECS_DEFAULT + "s: " + ctx.channel());
                    ctx.close();
                }
            }
        }
    }, handler);
}
Also used : IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) StringDecoder(io.netty.handler.codec.string.StringDecoder) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) ChannelPipeline(io.netty.channel.ChannelPipeline) BindException(java.net.BindException)

Example 24 with IdleStateEvent

use of io.netty.handler.timeout.IdleStateEvent in project web3sdk by FISCO-BCOS.

the class ChannelHandler method userEventTriggered.

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    String host = ((SocketChannel) ctx.channel()).remoteAddress().getAddress().getHostAddress();
    Integer port = ((SocketChannel) ctx.channel()).remoteAddress().getPort();
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent e = (IdleStateEvent) evt;
        switch(e.state()) {
            case READER_IDLE:
            case WRITER_IDLE:
            case ALL_IDLE:
                logger.error("事件:{} 连接{}:{} 长时间没有活动,断连", e.state(), host, port);
                channelInactive(ctx);
                ctx.disconnect();
                ctx.close();
                break;
            default:
                break;
        }
    }
}
Also used : IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) SocketChannel(io.netty.channel.socket.SocketChannel)

Example 25 with IdleStateEvent

use of io.netty.handler.timeout.IdleStateEvent in project web3sdk by FISCO-BCOS.

the class ChannelHandler method userEventTriggered.

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    String host = ((SocketChannel) ctx.channel()).remoteAddress().getAddress().getHostAddress();
    Integer port = ((SocketChannel) ctx.channel()).remoteAddress().getPort();
    if (evt instanceof IdleStateEvent) {
        IdleStateEvent e = (IdleStateEvent) evt;
        switch(e.state()) {
            case READER_IDLE:
            case WRITER_IDLE:
            case ALL_IDLE:
                logger.error(" idle state event:{} connect{}:{} long time Inactive,disconnect", e.state(), host, port);
                channelInactive(ctx);
                ctx.disconnect();
                ctx.close();
                break;
            default:
                break;
        }
    } else if (evt instanceof SslHandshakeCompletionEvent) {
        SslHandshakeCompletionEvent e = (SslHandshakeCompletionEvent) evt;
        if (e.isSuccess()) {
            logger.info(" handshake success, host: {}, port: {}, ctx: {}", host, port, System.identityHashCode(ctx));
            ChannelHandlerContext oldCtx = connections.setAndGetNetworkConnectionByHost(host, port, ctx);
            connections.getCallback().onConnect(ctx);
            if (Objects.nonNull(oldCtx)) {
                oldCtx.close();
                oldCtx.disconnect();
                logger.warn(" disconnect old connection, host: {}, port: {}, ctx: {}", host, port, System.identityHashCode(ctx));
            }
        } else {
            logger.error(" handshake failed, host: {}, port: {}, message: {}, cause: {} ", host, port, e.cause().getMessage(), e.cause());
            ctx.disconnect();
            ctx.close();
        }
    } else if (evt instanceof SslCloseCompletionEvent) {
        logger.info(" ssl close completion event, host: {}, port: {}, ctx: {} ", host, port, System.identityHashCode(ctx));
    } else {
        logger.info(" userEventTriggered event, host: {}, port: {}, evt: {}, ctx: {} ", host, port, evt, System.identityHashCode(ctx));
    }
}
Also used : IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) SocketChannel(io.netty.channel.socket.SocketChannel) SslCloseCompletionEvent(io.netty.handler.ssl.SslCloseCompletionEvent) SslHandshakeCompletionEvent(io.netty.handler.ssl.SslHandshakeCompletionEvent) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext)

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