Search in sources :

Example 11 with IdleStateEvent

use of io.netty.handler.timeout.IdleStateEvent in project zuul by Netflix.

the class OriginResponseReceiver method userEventTriggered.

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    if (evt instanceof CompleteEvent) {
        final CompleteReason reason = ((CompleteEvent) evt).getReason();
        if ((reason != SESSION_COMPLETE) && (edgeProxy != null)) {
            LOG.error("Origin request completed with reason other than COMPLETE: {}, {}", reason.name(), ChannelUtils.channelInfoForLogging(ctx.channel()));
            final ZuulException ze = new ZuulException("CompleteEvent", reason.name(), true);
            edgeProxy.errorFromOrigin(ze);
        }
        // See channelWrite() where these vars are first set onto the channel.
        try {
            super.userEventTriggered(ctx, evt);
        } finally {
            postCompleteHook(ctx, evt);
        }
    } else if (evt instanceof SslHandshakeCompletionEvent && !((SslHandshakeCompletionEvent) evt).isSuccess()) {
        Throwable cause = ((SslHandshakeCompletionEvent) evt).cause();
        ctx.channel().attr(SSL_HANDSHAKE_UNSUCCESS_FROM_ORIGIN_THROWABLE).set(cause);
    } else if (evt instanceof IdleStateEvent) {
        if (edgeProxy != null) {
            LOG.error("Origin request received IDLE event: {}", ChannelUtils.channelInfoForLogging(ctx.channel()));
            edgeProxy.errorFromOrigin(new OutboundException(READ_TIMEOUT, edgeProxy.getRequestAttempts()));
        }
        super.userEventTriggered(ctx, evt);
    } else {
        super.userEventTriggered(ctx, evt);
    }
}
Also used : IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) SslHandshakeCompletionEvent(io.netty.handler.ssl.SslHandshakeCompletionEvent) CompleteEvent(com.netflix.netty.common.HttpLifecycleChannelHandler.CompleteEvent) ZuulException(com.netflix.zuul.exception.ZuulException) CompleteReason(com.netflix.netty.common.HttpLifecycleChannelHandler.CompleteReason) OutboundException(com.netflix.zuul.exception.OutboundException)

Example 12 with IdleStateEvent

use of io.netty.handler.timeout.IdleStateEvent in project dubbo by alibaba.

the class TelnetIdleEventHandler method userEventTriggered.

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    // server will close channel when server don't receive any request from client util timeout.
    if (evt instanceof IdleStateEvent) {
        Channel channel = ctx.channel();
        log.info("IdleStateEvent triggered, close channel " + channel);
        channel.close();
    } else {
        super.userEventTriggered(ctx, evt);
    }
}
Also used : IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) Channel(io.netty.channel.Channel)

Example 13 with IdleStateEvent

use of io.netty.handler.timeout.IdleStateEvent in project dubbo by alibaba.

the class NettyClientHandler method userEventTriggered.

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    // send heartbeat when read idle.
    if (evt instanceof IdleStateEvent) {
        try {
            NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler);
            if (logger.isDebugEnabled()) {
                logger.debug("IdleStateEvent triggered, send heartbeat to channel " + channel);
            }
            Request req = new Request();
            req.setVersion(Version.getProtocolVersion());
            req.setTwoWay(true);
            req.setEvent(HEARTBEAT_EVENT);
            channel.send(req);
        } finally {
            NettyChannel.removeChannelIfDisconnected(ctx.channel());
        }
    } else {
        super.userEventTriggered(ctx, evt);
    }
}
Also used : IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) Request(org.apache.dubbo.remoting.exchange.Request)

Example 14 with IdleStateEvent

use of io.netty.handler.timeout.IdleStateEvent in project rocketmq-externals by apache.

the class MqttConnectionHandlerTest method testHandleIdleStateEvent.

@Test
public void testHandleIdleStateEvent() throws Exception {
    IdleStateEvent idleStateEvent = IdleStateEvent.ALL_IDLE_STATE_EVENT;
    handler.userEventTriggered(ctx, idleStateEvent);
    Mockito.verify(clientManager, Mockito.times(1)).remove(channel);
    Mockito.verify(channel, Mockito.times(1)).close();
}
Also used : IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) Test(org.junit.Test)

Example 15 with IdleStateEvent

use of io.netty.handler.timeout.IdleStateEvent in project BRFS by zhangnianli.

the class AsyncTcpClientGroup method createClient.

@Override
public TcpClient<BaseMessage, BaseResponse> createClient(TcpClientConfig config, Executor executor) throws InterruptedException {
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(group);
    bootstrap.channel(NioSocketChannel.class);
    bootstrap.option(ChannelOption.TCP_NODELAY, true);
    bootstrap.option(ChannelOption.RCVBUF_ALLOCATOR, new AdaptiveRecvByteBufAllocator());
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.connectTimeoutMillis());
    if (executor == null) {
        executor = new Executor() {

            @Override
            public void execute(Runnable command) {
                command.run();
            }
        };
    }
    AsyncTcpClient client = new AsyncTcpClient(executor);
    bootstrap.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast(new IdleStateHandler(0, DEFAULT_WRITE_IDLE_TIMEOUT_SECONDS, 0)).addLast(new BaseMessageEncoder()).addLast(new BaseResponseDecoder()).addLast(new SimpleChannelInboundHandler<TokenMessage<BaseResponse>>() {

                @Override
                protected void channelRead0(ChannelHandlerContext ctx, TokenMessage<BaseResponse> msg) throws Exception {
                    client.handle(msg.messageToken(), msg.message());
                }

                @Override
                public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
                    if (evt instanceof IdleStateEvent) {
                        IdleStateEvent e = (IdleStateEvent) evt;
                        if (e.state() == IdleState.WRITER_IDLE) {
                            ctx.writeAndFlush(new BaseMessage(-1));
                        }
                    }
                }
            });
        }
    });
    ChannelFuture future = bootstrap.connect(config.remoteAddress()).sync();
    if (!future.isSuccess()) {
        return null;
    }
    Channel channel = future.channel();
    channelList.add(channel);
    channel.closeFuture().addListener(new ChannelFutureListener() {

        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            channelList.remove(channel);
        }
    });
    LOG.info("create tcp client for {}", config.remoteAddress());
    client.attach(channel);
    return client;
}
Also used : SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) TokenMessage(com.bonree.brfs.common.net.tcp.TokenMessage) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelFutureListener(io.netty.channel.ChannelFutureListener) IOException(java.io.IOException) IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) BaseResponse(com.bonree.brfs.common.net.tcp.BaseResponse) Executor(java.util.concurrent.Executor) AdaptiveRecvByteBufAllocator(io.netty.channel.AdaptiveRecvByteBufAllocator) BaseMessage(com.bonree.brfs.common.net.tcp.BaseMessage) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) Bootstrap(io.netty.bootstrap.Bootstrap)

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