use of org.apache.flink.shaded.netty4.io.netty.handler.timeout.IdleStateHandler 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;
}
use of org.apache.flink.shaded.netty4.io.netty.handler.timeout.IdleStateHandler in project BRFS by zhangnianli.
the class MessageChannelInitializer method initChannel.
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new IdleStateHandler(DEFAULT_READ_IDLE_TIMEOUT_SECONDS, 0, 0));
pipeline.addLast(new MessageResponseEncoder());
pipeline.addLast(new MessageProtocolDecoder());
pipeline.addLast(messageDispatcher);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.timeout.IdleStateHandler in project ambry by linkedin.
the class FrontendNettyChannelInitializer method initChannel.
@Override
protected void initChannel(SocketChannel ch) throws Exception {
// If channel handler implementations are not annotated with @Sharable, Netty creates a new instance of every class
// in the pipeline for every connection.
// i.e. if there are a 1000 active connections there will be a 1000 NettyMessageProcessor instances.
ChannelPipeline pipeline = ch.pipeline();
// connection stats handler to track connection related metrics
pipeline.addLast("connectionStatsHandler", connectionStatsHandler);
// if SSL is enabled, add an SslHandler before the HTTP codec
if (sslFactory != null) {
InetSocketAddress peerAddress = ch.remoteAddress();
String peerHost = peerAddress.getHostName();
int peerPort = peerAddress.getPort();
SslHandler sslHandler = new SslHandler(sslFactory.createSSLEngine(peerHost, peerPort, SSLFactory.Mode.SERVER));
pipeline.addLast("sslHandler", sslHandler);
}
pipeline.addLast("codec", new HttpServerCodec(nettyConfig.nettyServerMaxInitialLineLength, nettyConfig.nettyServerMaxHeaderSize, nettyConfig.nettyServerMaxChunkSize)).addLast("healthCheckHandler", new HealthCheckHandler(restServerState, nettyMetrics)).addLast("publicAccessLogHandler", new PublicAccessLogHandler(publicAccessLogger, nettyMetrics)).addLast("idleStateHandler", new IdleStateHandler(0, 0, nettyConfig.nettyServerIdleTimeSeconds)).addLast("chunker", new ChunkedWriteHandler());
if (addedChannelHandlers != null) {
pipeline.addLast(addedChannelHandlers.toArray(new ChannelHandler[0]));
}
// custom processing class that interfaces with a RestRequestService.
pipeline.addLast("processor", new NettyMessageProcessor(nettyMetrics, nettyConfig, performanceConfig, requestHandler));
}
use of org.apache.flink.shaded.netty4.io.netty.handler.timeout.IdleStateHandler 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()));
}
};
}
use of org.apache.flink.shaded.netty4.io.netty.handler.timeout.IdleStateHandler in project apollo by apollo-rsps.
the class ServiceChannelInitializer method initChannel.
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("handshakeDecoder", new HandshakeDecoder());
pipeline.addLast("timeout", new IdleStateHandler(NetworkConstants.IDLE_TIME, 0, 0));
pipeline.addLast("handler", handler);
}
Aggregations