Search in sources :

Example 1 with IdleStateEventHandler

use of com.wavefront.agent.channel.IdleStateEventHandler in project java by wavefrontHQ.

the class ProxyUtil method createInitializer.

/**
 * Create a {@link ChannelInitializer} with multiple dynamically created
 * {@link ChannelHandler} objects.
 *
 * @param channelHandlerSuppliers Suppliers of ChannelHandlers.
 * @param port                    port number.
 * @param idleTimeout             idle timeout in seconds.
 * @param sslContext              SSL context.
 * @return channel initializer
 */
static ChannelInitializer<SocketChannel> createInitializer(Iterable<Supplier<ChannelHandler>> channelHandlerSuppliers, int port, int idleTimeout, @Nullable SslContext sslContext) {
    String strPort = String.valueOf(port);
    ChannelHandler idleStateEventHandler = new IdleStateEventHandler(Metrics.newCounter(new TaggedMetricName("listeners", "connections.idle.closed", "port", strPort)));
    ChannelHandler connectionTracker = new ConnectionTrackingHandler(Metrics.newCounter(new TaggedMetricName("listeners", "connections.accepted", "port", strPort)), Metrics.newCounter(new TaggedMetricName("listeners", "connections.active", "port", strPort)));
    if (sslContext != null) {
        logger.info("TLS enabled on port: " + port);
    }
    return new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) {
            ChannelPipeline pipeline = ch.pipeline();
            if (sslContext != null) {
                pipeline.addLast(sslContext.newHandler(ch.alloc()));
            }
            pipeline.addFirst("idlehandler", new IdleStateHandler(idleTimeout, 0, 0));
            pipeline.addLast("idlestateeventhandler", idleStateEventHandler);
            pipeline.addLast("connectiontracker", connectionTracker);
            channelHandlerSuppliers.forEach(x -> pipeline.addLast(x.get()));
        }
    };
}
Also used : SocketChannel(io.netty.channel.socket.SocketChannel) IdleStateEventHandler(com.wavefront.agent.channel.IdleStateEventHandler) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) ChannelHandler(io.netty.channel.ChannelHandler) ChannelInitializer(io.netty.channel.ChannelInitializer) TaggedMetricName(com.wavefront.common.TaggedMetricName) ChannelPipeline(io.netty.channel.ChannelPipeline) ConnectionTrackingHandler(com.wavefront.agent.channel.ConnectionTrackingHandler)

Aggregations

ConnectionTrackingHandler (com.wavefront.agent.channel.ConnectionTrackingHandler)1 IdleStateEventHandler (com.wavefront.agent.channel.IdleStateEventHandler)1 TaggedMetricName (com.wavefront.common.TaggedMetricName)1 ChannelHandler (io.netty.channel.ChannelHandler)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 SocketChannel (io.netty.channel.socket.SocketChannel)1 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)1