use of org.apache.flink.shaded.netty4.io.netty.channel.socket.nio.NioSocketChannel in project java-tron by tronprotocol.
the class TronChannelInitializer method initChannel.
@Override
public void initChannel(NioSocketChannel ch) throws Exception {
try {
if (isInbound() && channelManager.isRecentlyDisconnected(ch.remoteAddress().getAddress())) {
// avoid too frequent connection attempts
logger.info("Drop connection - the same IP was disconnected recently, channel: {}", ch.toString());
ch.disconnect();
return;
}
final Channel channel = ctx.getBean(PeerConnection.class);
channel.init(ch.pipeline(), remoteId, peerDiscoveryMode, channelManager, p2pNode);
if (!peerDiscoveryMode) {
channelManager.add(channel);
}
// limit the size of receiving buffer to 1024
ch.config().setRecvByteBufAllocator(new FixedRecvByteBufAllocator(256 * 1024));
ch.config().setOption(ChannelOption.SO_RCVBUF, 256 * 1024);
ch.config().setOption(ChannelOption.SO_BACKLOG, 1024);
// be aware of channel closing
ch.closeFuture().addListener((ChannelFutureListener) future -> {
logger.info("Close channel:" + channel.getNode());
if (!peerDiscoveryMode) {
channelManager.notifyDisconnect(channel);
}
});
} catch (Exception e) {
logger.error("Unexpected error: ", e);
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.socket.nio.NioSocketChannel in project rskj by rsksmart.
the class EthereumChannelInitializerTest method initChannel_AddressIsNotBanned_ShouldNotDisconnect.
@Test
public void initChannel_AddressIsNotBanned_ShouldNotDisconnect() {
InetSocketAddress address = Objects.requireNonNull(IpUtils.parseAddress("192.168.100.1:5555"));
PeerScoringManager peerScoringManager = mock(PeerScoringManager.class);
doReturn(false).when(peerScoringManager).isAddressBanned(eq(address.getAddress()));
ChannelManager channelManager = mock(ChannelManager.class);
doReturn(true).when(channelManager).isAddressBlockAvailable(any());
ChannelPipeline pipeline = mock(ChannelPipeline.class);
NioSocketChannel channel = mock(NioSocketChannel.class);
SocketChannelConfig config = mock(SocketChannelConfig.class);
ChannelFuture channelFuture = mock(ChannelFuture.class);
doReturn(address).when(channel).remoteAddress();
doReturn(pipeline).when(channel).pipeline();
doReturn(config).when(channel).config();
doReturn(channelFuture).when(channel).closeFuture();
EthereumChannelInitializer channelInitializer = new EthereumChannelInitializer("", mock(RskSystemProperties.class), channelManager, mock(CompositeEthereumListener.class), mock(ConfigCapabilities.class), mock(NodeManager.class), mock(RskWireProtocol.Factory.class), mock(Eth62MessageFactory.class), mock(StaticMessages.class), peerScoringManager);
channelInitializer.initChannel(channel);
verify(channel, never()).disconnect();
}
use of org.apache.flink.shaded.netty4.io.netty.channel.socket.nio.NioSocketChannel in project ambry by linkedin.
the class Http2MultiplexedChannelPoolTest method closeWaitsForConnectionToBeReleasedBeforeClosingConnectionPool.
/**
* Connection pool is released first and then close upon h2 pool close.
*/
@Test
public void closeWaitsForConnectionToBeReleasedBeforeClosingConnectionPool() {
SocketChannel channel = new NioSocketChannel();
try {
loopGroup.register(channel).awaitUninterruptibly();
ChannelPool connectionPool = mock(ChannelPool.class);
ArgumentCaptor<Promise> releasePromise = ArgumentCaptor.forClass(Promise.class);
when(connectionPool.release(eq(channel), releasePromise.capture())).thenAnswer(invocation -> {
Promise<?> promise = releasePromise.getValue();
promise.setSuccess(null);
return promise;
});
MultiplexedChannelRecord record = new MultiplexedChannelRecord(channel, 8, null, streamChannelInitializer);
Http2MultiplexedChannelPool h2Pool = new Http2MultiplexedChannelPool(connectionPool, loopGroup, Collections.singleton(record), null, http2ClientConfigForOneConnection, new Http2ClientMetrics(new MetricRegistry()), streamChannelInitializer);
h2Pool.close();
InOrder inOrder = Mockito.inOrder(connectionPool);
inOrder.verify(connectionPool).release(eq(channel), isA(Promise.class));
inOrder.verify(connectionPool).close();
} finally {
channel.close().awaitUninterruptibly();
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.socket.nio.NioSocketChannel in project ambry by linkedin.
the class Http2MultiplexedChannelPoolTest method releaseParentChannelsIfPoolIsClosed.
/**
* Channel acquire should fail if pool is closed.
*/
@Test
public void releaseParentChannelsIfPoolIsClosed() {
SocketChannel channel = new NioSocketChannel();
try {
loopGroup.register(channel).awaitUninterruptibly();
ChannelPool connectionPool = mock(ChannelPool.class);
ArgumentCaptor<Promise> releasePromise = ArgumentCaptor.forClass(Promise.class);
when(connectionPool.release(eq(channel), releasePromise.capture())).thenAnswer(invocation -> {
Promise<?> promise = releasePromise.getValue();
promise.setSuccess(null);
return promise;
});
MultiplexedChannelRecord record = new MultiplexedChannelRecord(channel, 8, null, streamChannelInitializer);
Http2MultiplexedChannelPool h2Pool = new Http2MultiplexedChannelPool(connectionPool, loopGroup, Collections.singleton(record), null, http2ClientConfigForOneConnection, new Http2ClientMetrics(new MetricRegistry()), streamChannelInitializer);
h2Pool.close();
InOrder inOrder = Mockito.inOrder(connectionPool);
inOrder.verify(connectionPool).release(eq(channel), isA(Promise.class));
inOrder.verify(connectionPool).close();
} finally {
channel.close().awaitUninterruptibly();
}
}
use of org.apache.flink.shaded.netty4.io.netty.channel.socket.nio.NioSocketChannel in project proxyee-down by monkeyWie.
the class HttpDownUtil method getResponse.
/**
* 取请求响应
*/
public static HttpResponse getResponse(HttpRequest httpRequest, ProxyConfig proxyConfig, SslContext clientSslCtx, NioEventLoopGroup loopGroup) throws Exception {
final HttpResponse[] httpResponses = new HttpResponse[1];
CountDownLatch cdl = new CountDownLatch(1);
HttpRequestInfo requestInfo = (HttpRequestInfo) httpRequest;
RequestProto requestProto = requestInfo.requestProto();
Bootstrap bootstrap = new Bootstrap();
// 注册线程池
bootstrap.group(loopGroup).channel(// 使用NioSocketChannel来作为连接用的channel类
NioSocketChannel.class).handler(new ChannelInitializer() {
@Override
protected void initChannel(Channel ch) throws Exception {
if (proxyConfig != null) {
ch.pipeline().addLast(ProxyHandleFactory.build(proxyConfig));
}
if (requestProto.getSsl()) {
ch.pipeline().addLast(clientSslCtx.newHandler(ch.alloc()));
}
ch.pipeline().addLast("httpCodec", new HttpClientCodec());
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx0, Object msg0) throws Exception {
if (msg0 instanceof HttpResponse) {
HttpResponse httpResponse = (HttpResponse) msg0;
httpResponses[0] = httpResponse;
ctx0.channel().close();
cdl.countDown();
}
}
});
}
});
if (proxyConfig != null) {
// 代理服务器解析DNS和连接
bootstrap.resolver(NoopAddressResolverGroup.INSTANCE);
}
ChannelFuture cf = bootstrap.connect(requestProto.getHost(), requestProto.getPort());
cf.addListener((ChannelFutureListener) future -> {
if (future.isSuccess()) {
httpRequest.headers().set(HttpHeaderNames.RANGE, "bytes=0-0");
cf.channel().writeAndFlush(httpRequest);
if (requestInfo.content() != null) {
HttpContent content = new DefaultLastHttpContent();
content.content().writeBytes(requestInfo.content());
cf.channel().writeAndFlush(content);
}
} else {
cdl.countDown();
}
});
cdl.await(30, TimeUnit.SECONDS);
if (httpResponses[0] == null) {
throw new TimeoutException("getResponse timeout");
}
return httpResponses[0];
}
Aggregations