Search in sources :

Example 46 with IdleStateHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.timeout.IdleStateHandler in project uploader by smoketurner.

the class UploadInitializer method initChannel.

@Override
public void initChannel(final SocketChannel ch) throws Exception {
    final ChannelPipeline p = ch.pipeline();
    // add the IP ACL filter first
    if (ipFilter != null) {
        p.addLast("acl", ipFilter);
    }
    if (sslCtx != null) {
        if (configuration.isClientAuth()) {
            final SSLEngine engine = sslCtx.newEngine(ch.alloc());
            engine.setUseClientMode(false);
            engine.setNeedClientAuth(true);
            p.addLast("ssl", new SslHandler(engine));
        } else {
            p.addLast("ssl", sslCtx.newHandler(ch.alloc()));
        }
    }
    // removes idle connections after READER_IDLE_SECONDS seconds
    p.addLast("idleStateHandler", new IdleStateHandler(READER_IDLE_SECONDS, 0, 0));
    // authenticate via an ACL and mutual certificates
    p.addLast("auth", new AuthHandler(configuration.isClientAuth()));
    // check to see if the data stream is gzipped or not
    // p.addLast("gzipDetector", new OptionalGzipHandler());
    // break each data chunk by newlines
    p.addLast("line", new LineBasedFrameDecoder(Ints.checkedCast(maxLength), true, true));
    // convert each data chunk into a byte array
    p.addLast("decoder", new ByteArrayDecoder());
    // batch and compress chunks of data up to maxUploadBytes
    p.addLast("batcher", new BatchHandler(maxUploadBytes));
    // upload the batch to S3
    p.addLast("uploader", uploadHandler);
}
Also used : SSLEngine(javax.net.ssl.SSLEngine) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) LineBasedFrameDecoder(io.netty.handler.codec.LineBasedFrameDecoder) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler) ByteArrayDecoder(io.netty.handler.codec.bytes.ByteArrayDecoder)

Example 47 with IdleStateHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.timeout.IdleStateHandler in project rocketmq by apache.

the class NettyRemotingClient method start.

@Override
public void start() {
    this.defaultEventExecutorGroup = new DefaultEventExecutorGroup(nettyClientConfig.getClientWorkerThreads(), new ThreadFactory() {

        private AtomicInteger threadIndex = new AtomicInteger(0);

        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, "NettyClientWorkerThread_" + this.threadIndex.incrementAndGet());
        }
    });
    Bootstrap handler = this.bootstrap.group(this.eventLoopGroupWorker).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_KEEPALIVE, false).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, nettyClientConfig.getConnectTimeoutMillis()).option(ChannelOption.SO_SNDBUF, nettyClientConfig.getClientSocketSndBufSize()).option(ChannelOption.SO_RCVBUF, nettyClientConfig.getClientSocketRcvBufSize()).handler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline pipeline = ch.pipeline();
            if (nettyClientConfig.isUseTLS()) {
                if (null != sslContext) {
                    pipeline.addFirst(defaultEventExecutorGroup, "sslHandler", sslContext.newHandler(ch.alloc()));
                    log.info("Prepend SSL handler");
                } else {
                    log.warn("Connections are insecure as SSLContext is null!");
                }
            }
            pipeline.addLast(defaultEventExecutorGroup, new NettyEncoder(), new NettyDecoder(), new IdleStateHandler(0, 0, nettyClientConfig.getClientChannelMaxIdleTimeSeconds()), new NettyConnectManageHandler(), new NettyClientHandler());
        }
    });
    this.timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            try {
                NettyRemotingClient.this.scanResponseTable();
            } catch (Throwable e) {
                log.error("scanResponseTable exception", e);
            }
        }
    }, 1000 * 3, 1000);
    if (this.channelEventListener != null) {
        this.nettyEventExecutor.start();
    }
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) SocketChannel(io.netty.channel.socket.SocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) DefaultEventExecutorGroup(io.netty.util.concurrent.DefaultEventExecutorGroup) RemotingConnectException(org.apache.rocketmq.remoting.exception.RemotingConnectException) RemotingSendRequestException(org.apache.rocketmq.remoting.exception.RemotingSendRequestException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) RemotingTooMuchRequestException(org.apache.rocketmq.remoting.exception.RemotingTooMuchRequestException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) ChannelPipeline(io.netty.channel.ChannelPipeline) TimerTask(java.util.TimerTask) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) Bootstrap(io.netty.bootstrap.Bootstrap)

Example 48 with IdleStateHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.timeout.IdleStateHandler in project rocketmq by apache.

the class NettyRemotingServer method start.

@Override
public void start() {
    this.defaultEventExecutorGroup = new DefaultEventExecutorGroup(nettyServerConfig.getServerWorkerThreads(), new ThreadFactory() {

        private AtomicInteger threadIndex = new AtomicInteger(0);

        @Override
        public Thread newThread(Runnable r) {
            return new Thread(r, "NettyServerCodecThread_" + this.threadIndex.incrementAndGet());
        }
    });
    ServerBootstrap childHandler = this.serverBootstrap.group(this.eventLoopGroupBoss, this.eventLoopGroupSelector).channel(useEpoll() ? EpollServerSocketChannel.class : NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 1024).option(ChannelOption.SO_REUSEADDR, true).option(ChannelOption.SO_KEEPALIVE, false).childOption(ChannelOption.TCP_NODELAY, true).childOption(ChannelOption.SO_SNDBUF, nettyServerConfig.getServerSocketSndBufSize()).childOption(ChannelOption.SO_RCVBUF, nettyServerConfig.getServerSocketRcvBufSize()).localAddress(new InetSocketAddress(this.nettyServerConfig.getListenPort())).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast(defaultEventExecutorGroup, HANDSHAKE_HANDLER_NAME, new HandshakeHandler(TlsSystemConfig.tlsMode)).addLast(defaultEventExecutorGroup, new NettyEncoder(), new NettyDecoder(), new IdleStateHandler(0, 0, nettyServerConfig.getServerChannelMaxIdleTimeSeconds()), new NettyConnectManageHandler(), new NettyServerHandler());
        }
    });
    if (nettyServerConfig.isServerPooledByteBufAllocatorEnable()) {
        childHandler.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    }
    try {
        ChannelFuture sync = this.serverBootstrap.bind().sync();
        InetSocketAddress addr = (InetSocketAddress) sync.channel().localAddress();
        this.port = addr.getPort();
    } catch (InterruptedException e1) {
        throw new RuntimeException("this.serverBootstrap.bind().sync() InterruptedException", e1);
    }
    if (this.channelEventListener != null) {
        this.nettyEventExecutor.start();
    }
    this.timer.scheduleAtFixedRate(new TimerTask() {

        @Override
        public void run() {
            try {
                NettyRemotingServer.this.scanResponseTable();
            } catch (Throwable e) {
                log.error("scanResponseTable exception", e);
            }
        }
    }, 1000 * 3, 1000);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ThreadFactory(java.util.concurrent.ThreadFactory) EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) DefaultEventExecutorGroup(io.netty.util.concurrent.DefaultEventExecutorGroup) InetSocketAddress(java.net.InetSocketAddress) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) RemotingTooMuchRequestException(org.apache.rocketmq.remoting.exception.RemotingTooMuchRequestException) RemotingSendRequestException(org.apache.rocketmq.remoting.exception.RemotingSendRequestException) NoSuchElementException(java.util.NoSuchElementException) RemotingTimeoutException(org.apache.rocketmq.remoting.exception.RemotingTimeoutException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) TimerTask(java.util.TimerTask) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler)

Example 49 with IdleStateHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.timeout.IdleStateHandler in project jim-framework by jiangmin168168.

the class RpcClientInitializer method initChannel.

@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
    ChannelPipeline cp = socketChannel.pipeline();
    cp.addLast(new RpcEncoder(RpcRequest.class));
    cp.addLast(new RpcDecoder(RpcResponse.class));
    cp.addLast(new IdleStateHandler(0, 0, Constants.ALLIDLE_TIME_SECONDS));
    cp.addLast(new ClientHeartbeatHandler());
    cp.addLast(new RpcClientInvoker(this.getFilterMap()));
}
Also used : RpcEncoder(com.jim.framework.rpc.codec.RpcEncoder) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) RpcRequest(com.jim.framework.rpc.common.RpcRequest) ClientHeartbeatHandler(com.jim.framework.rpc.keepalive.ClientHeartbeatHandler) RpcDecoder(com.jim.framework.rpc.codec.RpcDecoder) RpcResponse(com.jim.framework.rpc.common.RpcResponse) ChannelPipeline(io.netty.channel.ChannelPipeline)

Example 50 with IdleStateHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.timeout.IdleStateHandler in project cassandra by apache.

the class PipelineConfigurator method configureInitialPipeline.

public void configureInitialPipeline(Channel channel, Connection.Factory connectionFactory) {
    ChannelPipeline pipeline = channel.pipeline();
    // Add the ConnectionLimitHandler to the pipeline if configured to do so.
    if (DatabaseDescriptor.getNativeTransportMaxConcurrentConnections() > 0 || DatabaseDescriptor.getNativeTransportMaxConcurrentConnectionsPerIp() > 0) {
        // Add as first to the pipeline so the limit is enforced as first action.
        pipeline.addFirst(CONNECTION_LIMIT_HANDLER, connectionLimitHandler);
    }
    long idleTimeout = DatabaseDescriptor.nativeTransportIdleTimeout();
    if (idleTimeout > 0) {
        pipeline.addLast(IDLE_STATE_HANDLER, new IdleStateHandler(false, 0, 0, idleTimeout, TimeUnit.MILLISECONDS) {

            @Override
            protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) {
                logger.info("Closing client connection {} after timeout of {}ms", channel.remoteAddress(), idleTimeout);
                ctx.close();
            }
        });
    }
    if (DEBUG)
        pipeline.addLast(DEBUG_HANDLER, new LoggingHandler(LogLevel.INFO));
    pipeline.addLast(ENVELOPE_ENCODER, Envelope.Encoder.instance);
    pipeline.addLast(INITIAL_HANDLER, new InitialConnectionHandler(new Envelope.Decoder(), connectionFactory, this));
    // The exceptionHandler will take care of handling exceptionCaught(...) events while still running
    // on the same EventLoop as all previous added handlers in the pipeline. This is important as the used
    // eventExecutorGroup may not enforce strict ordering for channel events.
    // As the exceptionHandler runs in the EventLoop as the previous handlers we are sure all exceptions are
    // correctly handled before the handler itself is removed.
    // See https://issues.apache.org/jira/browse/CASSANDRA-13649
    pipeline.addLast(EXCEPTION_HANDLER, PreV5Handlers.ExceptionHandler.instance);
    onInitialPipelineReady(pipeline);
}
Also used : IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) LoggingHandler(io.netty.handler.logging.LoggingHandler) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) ByteToMessageDecoder(io.netty.handler.codec.ByteToMessageDecoder)

Aggregations

IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)61 ChannelPipeline (io.netty.channel.ChannelPipeline)29 SocketChannel (io.netty.channel.socket.SocketChannel)16 LoggingHandler (io.netty.handler.logging.LoggingHandler)15 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)12 SslHandler (io.netty.handler.ssl.SslHandler)11 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)11 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)10 EventLoopGroup (io.netty.channel.EventLoopGroup)8 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)8 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)8 Bootstrap (io.netty.bootstrap.Bootstrap)7 ChannelFuture (io.netty.channel.ChannelFuture)7 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)6 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)6 InetSocketAddress (java.net.InetSocketAddress)6 StringDecoder (io.netty.handler.codec.string.StringDecoder)5 IdleStateEvent (io.netty.handler.timeout.IdleStateEvent)5 DefaultEventExecutorGroup (io.netty.util.concurrent.DefaultEventExecutorGroup)5 ThreadFactory (java.util.concurrent.ThreadFactory)5