Search in sources :

Example 11 with DefaultChannelGroup

use of io.netty.channel.group.DefaultChannelGroup in project cdap by caskdata.

the class NettyRouter method startUp.

@Override
protected void startUp() throws Exception {
    tokenValidator.startAndWait();
    ChannelGroup channelGroup = new DefaultChannelGroup(ImmediateEventExecutor.INSTANCE);
    serverCancellable = startServer(createServerBootstrap(channelGroup), channelGroup);
}
Also used : DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) ChannelGroup(io.netty.channel.group.ChannelGroup)

Example 12 with DefaultChannelGroup

use of io.netty.channel.group.DefaultChannelGroup in project reactor-netty by reactor.

the class NettyOptionsTest method channelIsAddedToChannelGroup.

@Test
public void channelIsAddedToChannelGroup() {
    // create a ChannelGroup that never removes disconnected channels
    ChannelGroup group = new DefaultChannelGroup(null) {

        @Override
        public boolean remove(Object o) {
            System.err.println("removed " + o);
            return false;
        }
    };
    NettyContext nettyContext = HttpServer.create(opt -> opt.channelGroup(group)).start((req, resp) -> resp.sendNotFound()).getContext();
    HttpClientResponse resp = HttpClient.create(opt -> opt.connectAddress(() -> nettyContext.address())).get("/", req -> req.failOnClientError(false).send()).block();
    assertThat((Iterable<Channel>) group).doesNotContain(nettyContext.channel()).hasSize(1);
    resp.dispose();
    nettyContext.dispose();
}
Also used : DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) ChannelGroup(io.netty.channel.group.ChannelGroup) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) ArrayList(java.util.ArrayList) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) List(java.util.List) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NettyContext(reactor.ipc.netty.NettyContext) HttpClient(reactor.ipc.netty.http.client.HttpClient) HttpServer(reactor.ipc.netty.http.server.HttpServer) HttpClientResponse(reactor.ipc.netty.http.client.HttpClientResponse) Channel(io.netty.channel.Channel) ChannelGroup(io.netty.channel.group.ChannelGroup) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) NettyContext(reactor.ipc.netty.NettyContext) Test(org.junit.Test)

Example 13 with DefaultChannelGroup

use of io.netty.channel.group.DefaultChannelGroup in project cdap by caskdata.

the class ServiceSocksProxy method startUp.

@Override
protected void startUp() throws Exception {
    ServerBootstrap bootstrap = new ServerBootstrap();
    // We don't perform any blocking task in the proxy, only IO relying, hence doesn't need large amount of threads.
    eventLoopGroup = new NioEventLoopGroup(10, Threads.createDaemonThreadFactory("service-socks-proxy-%d"));
    bootstrap.group(eventLoopGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(SocketChannel ch) {
            channelGroup.add(ch);
            ch.pipeline().addLast(new SocksPortUnificationServerHandler()).addLast(new ServiceSocksServerHandler(discoveryServiceClient, authenticator));
        }
    });
    Channel serverChannel = bootstrap.bind(InetAddress.getLoopbackAddress(), 0).sync().channel();
    bindAddress = (InetSocketAddress) serverChannel.localAddress();
    channelGroup = new DefaultChannelGroup(ImmediateEventExecutor.INSTANCE);
    channelGroup.add(serverChannel);
    LOG.info("Runtime service socks proxy started on {}", bindAddress);
}
Also used : DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) SocketChannel(io.netty.channel.socket.SocketChannel) SocksPortUnificationServerHandler(io.netty.handler.codec.socksx.SocksPortUnificationServerHandler) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 14 with DefaultChannelGroup

use of io.netty.channel.group.DefaultChannelGroup in project cdap by caskdata.

the class ServiceSocksServerConnectHandler method createForwardingChannelHandler.

@Override
protected Future<RelayChannelHandler> createForwardingChannelHandler(Channel inboundChannel, String destAddress, int destPort) {
    Promise<RelayChannelHandler> promise = new DefaultPromise<>(inboundChannel.eventLoop());
    // Creates a bootstrap for connecting to the target service
    ChannelGroup channels = new DefaultChannelGroup(inboundChannel.eventLoop());
    Bootstrap bootstrap = new Bootstrap().group(inboundChannel.eventLoop()).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true).handler(new ChannelInboundHandlerAdapter() {

        @Override
        public void channelActive(ChannelHandlerContext ctx) {
            channels.add(ctx.channel());
            // When the outbound connection is active, adds the relay channel handler for the current pipeline,
            // which is for relaying traffic coming back from outbound connection.
            // Also complete the relay channel handler future, which is for relaying traffic from inbound to outbound.
            ctx.pipeline().addLast(new SimpleRelayChannelHandler(inboundChannel));
            promise.setSuccess(new SimpleRelayChannelHandler(ctx.channel()));
        }
    });
    // Discover the target address
    Promise<Discoverable> discoverablePromise = new DefaultPromise<>(inboundChannel.eventLoop());
    Cancellable cancellable = discoveryServiceClient.discover(destAddress).watchChanges(serviceDiscovered -> {
        // If it is discovered, make a connection and complete the channel handler future
        Discoverable discoverable = new RandomEndpointStrategy(() -> serviceDiscovered).pick();
        if (discoverable != null) {
            discoverablePromise.setSuccess(discoverable);
        }
    }, inboundChannel.eventLoop());
    // When discovery completed successfully, connect to the destination
    discoverablePromise.addListener((GenericFutureListener<Future<Discoverable>>) discoverableFuture -> {
        cancellable.cancel();
        if (discoverableFuture.isSuccess()) {
            Discoverable discoverable = discoverableFuture.get();
            bootstrap.connect(discoverable.getSocketAddress()).addListener((ChannelFutureListener) channelFuture -> {
                if (!channelFuture.isSuccess()) {
                    promise.setFailure(channelFuture.cause());
                }
            });
        } else {
            promise.setFailure(discoverableFuture.cause());
        }
    });
    // On inbound channel close, close all outbound channels.
    // Also cancel the watch since it is no longer needed.
    // This is to handle case where discovery never return an endpoint before client connection timeout
    inboundChannel.closeFuture().addListener((ChannelFutureListener) future -> {
        cancellable.cancel();
        channels.close();
    });
    return promise;
}
Also used : DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) ChannelGroup(io.netty.channel.group.ChannelGroup) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) ChannelOption(io.netty.channel.ChannelOption) Promise(io.netty.util.concurrent.Promise) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) GenericFutureListener(io.netty.util.concurrent.GenericFutureListener) Channel(io.netty.channel.Channel) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) DefaultPromise(io.netty.util.concurrent.DefaultPromise) Discoverable(org.apache.twill.discovery.Discoverable) DiscoveryServiceClient(org.apache.twill.discovery.DiscoveryServiceClient) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Cancellable(org.apache.twill.common.Cancellable) Future(io.netty.util.concurrent.Future) Discoverable(org.apache.twill.discovery.Discoverable) Cancellable(org.apache.twill.common.Cancellable) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelFutureListener(io.netty.channel.ChannelFutureListener) DefaultPromise(io.netty.util.concurrent.DefaultPromise) Bootstrap(io.netty.bootstrap.Bootstrap) Future(io.netty.util.concurrent.Future) ChannelGroup(io.netty.channel.group.ChannelGroup) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy)

Example 15 with DefaultChannelGroup

use of io.netty.channel.group.DefaultChannelGroup in project activemq-artemis by apache.

the class NettyAcceptor method start.

@Override
public synchronized void start() throws Exception {
    if (channelClazz != null) {
        // Already started
        return;
    }
    String acceptorType;
    if (useInvm) {
        acceptorType = INVM_ACCEPTOR_TYPE;
        channelClazz = LocalServerChannel.class;
        eventLoopGroup = new DefaultEventLoopGroup();
    } else {
        if (remotingThreads == -1) {
            // Default to number of cores * 3
            remotingThreads = Runtime.getRuntime().availableProcessors() * 3;
        }
        if (useEpoll && CheckDependencies.isEpollAvailable()) {
            channelClazz = EpollServerSocketChannel.class;
            eventLoopGroup = new EpollEventLoopGroup(remotingThreads, AccessController.doPrivileged(new PrivilegedAction<ActiveMQThreadFactory>() {

                @Override
                public ActiveMQThreadFactory run() {
                    return new ActiveMQThreadFactory("activemq-netty-threads", true, ClientSessionFactoryImpl.class.getClassLoader());
                }
            }));
            acceptorType = EPOLL_ACCEPTOR_TYPE;
            logger.debug("Acceptor using native epoll");
        } else if (useKQueue && CheckDependencies.isKQueueAvailable()) {
            channelClazz = KQueueServerSocketChannel.class;
            eventLoopGroup = new KQueueEventLoopGroup(remotingThreads, AccessController.doPrivileged(new PrivilegedAction<ActiveMQThreadFactory>() {

                @Override
                public ActiveMQThreadFactory run() {
                    return new ActiveMQThreadFactory("activemq-netty-threads", true, ClientSessionFactoryImpl.class.getClassLoader());
                }
            }));
            acceptorType = KQUEUE_ACCEPTOR_TYPE;
            logger.debug("Acceptor using native kqueue");
        } else {
            channelClazz = NioServerSocketChannel.class;
            eventLoopGroup = new NioEventLoopGroup(remotingThreads, AccessController.doPrivileged(new PrivilegedAction<ActiveMQThreadFactory>() {

                @Override
                public ActiveMQThreadFactory run() {
                    return new ActiveMQThreadFactory("activemq-netty-threads", true, ClientSessionFactoryImpl.class.getClassLoader());
                }
            }));
            acceptorType = NIO_ACCEPTOR_TYPE;
            logger.debug("Acceptor using nio");
        }
    }
    bootstrap = new ServerBootstrap();
    bootstrap.group(eventLoopGroup);
    bootstrap.channel(channelClazz);
    ChannelInitializer<Channel> factory = new ChannelInitializer<Channel>() {

        @Override
        public void initChannel(Channel channel) throws Exception {
            ChannelPipeline pipeline = channel.pipeline();
            if (sslEnabled) {
                final Pair<String, Integer> peerInfo = getPeerInfo(channel);
                try {
                    pipeline.addLast("sni", new NettySNIHostnameHandler());
                    pipeline.addLast("ssl", getSslHandler(channel.alloc(), peerInfo.getA(), peerInfo.getB()));
                    pipeline.addLast("sslHandshakeExceptionHandler", new SslHandshakeExceptionHandler());
                } catch (Exception e) {
                    Throwable rootCause = getRootCause(e);
                    ActiveMQServerLogger.LOGGER.gettingSslHandlerFailed(channel.remoteAddress().toString(), rootCause.getClass().getName() + ": " + rootCause.getMessage());
                    if (ActiveMQServerLogger.LOGGER.isDebugEnabled()) {
                        ActiveMQServerLogger.LOGGER.debug("Getting SSL handler failed", e);
                    }
                    throw e;
                }
            }
            pipeline.addLast(protocolHandler.getProtocolDecoder());
        }

        private Pair<String, Integer> getPeerInfo(Channel channel) {
            try {
                String[] peerInfo = channel.remoteAddress().toString().replace("/", "").split(":");
                return new Pair<>(peerInfo[0], Integer.parseInt(peerInfo[1]));
            } catch (Exception e) {
                logger.debug("Failed to parse peer info for SSL engine initialization", e);
            }
            return new Pair<>(null, 0);
        }
    };
    bootstrap.childHandler(factory);
    // Bind
    bootstrap.childOption(ChannelOption.TCP_NODELAY, tcpNoDelay);
    if (tcpReceiveBufferSize != -1) {
        bootstrap.childOption(ChannelOption.SO_RCVBUF, tcpReceiveBufferSize);
    }
    if (tcpSendBufferSize != -1) {
        bootstrap.childOption(ChannelOption.SO_SNDBUF, tcpSendBufferSize);
    }
    final int writeBufferLowWaterMark = this.writeBufferLowWaterMark != -1 ? this.writeBufferLowWaterMark : WriteBufferWaterMark.DEFAULT.low();
    final int writeBufferHighWaterMark = this.writeBufferHighWaterMark != -1 ? this.writeBufferHighWaterMark : WriteBufferWaterMark.DEFAULT.high();
    final WriteBufferWaterMark writeBufferWaterMark = new WriteBufferWaterMark(writeBufferLowWaterMark, writeBufferHighWaterMark);
    bootstrap.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK, writeBufferWaterMark);
    if (backlog != -1) {
        bootstrap.option(ChannelOption.SO_BACKLOG, backlog);
    }
    bootstrap.option(ChannelOption.SO_REUSEADDR, true);
    bootstrap.childOption(ChannelOption.SO_REUSEADDR, true);
    bootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
    channelGroup = new DefaultChannelGroup("activemq-accepted-channels", GlobalEventExecutor.INSTANCE);
    serverChannelGroup = new DefaultChannelGroup("activemq-acceptor-channels", GlobalEventExecutor.INSTANCE);
    if (httpUpgradeEnabled) {
    // the channel will be bound by the Web container and hand over after the HTTP Upgrade
    // handshake is successful
    } else {
        startServerChannels();
        paused = false;
        if (notificationService != null) {
            TypedProperties props = new TypedProperties();
            props.putSimpleStringProperty(new SimpleString("factory"), new SimpleString(NettyAcceptorFactory.class.getName()));
            props.putSimpleStringProperty(new SimpleString("host"), new SimpleString(host));
            props.putIntProperty(new SimpleString("port"), actualPort);
            Notification notification = new Notification(null, CoreNotificationType.ACCEPTOR_STARTED, props);
            notificationService.sendNotification(notification);
        }
        ActiveMQServerLogger.LOGGER.startedAcceptor(acceptorType, host, actualPort, protocolsString);
    }
    if (batchDelay > 0) {
        flusher = new BatchFlusher();
        batchFlusherFuture = scheduledThreadPool.scheduleWithFixedDelay(flusher, batchDelay, batchDelay, TimeUnit.MILLISECONDS);
    }
}
Also used : ActiveMQThreadFactory(org.apache.activemq.artemis.utils.ActiveMQThreadFactory) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) Notification(org.apache.activemq.artemis.core.server.management.Notification) ClientSessionFactoryImpl(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl) PrivilegedAction(java.security.PrivilegedAction) WriteBufferWaterMark(io.netty.channel.WriteBufferWaterMark) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Pair(org.apache.activemq.artemis.api.core.Pair) KQueueServerSocketChannel(io.netty.channel.kqueue.KQueueServerSocketChannel) DefaultChannelGroup(io.netty.channel.group.DefaultChannelGroup) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerChannel(io.netty.channel.ServerChannel) KQueueServerSocketChannel(io.netty.channel.kqueue.KQueueServerSocketChannel) EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) Channel(io.netty.channel.Channel) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) TypedProperties(org.apache.activemq.artemis.utils.collections.TypedProperties) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelPipeline(io.netty.channel.ChannelPipeline) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) KQueueEventLoopGroup(io.netty.channel.kqueue.KQueueEventLoopGroup)

Aggregations

DefaultChannelGroup (io.netty.channel.group.DefaultChannelGroup)42 ChannelGroup (io.netty.channel.group.ChannelGroup)26 Channel (io.netty.channel.Channel)20 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)13 InetSocketAddress (java.net.InetSocketAddress)11 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)10 Test (org.junit.jupiter.api.Test)9 List (java.util.List)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 ChannelOption (io.netty.channel.ChannelOption)7 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 ChannelInitializer (io.netty.channel.ChannelInitializer)6 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)6 GlobalEventExecutor (io.netty.util.concurrent.GlobalEventExecutor)6 IOException (java.io.IOException)6 NoopRegistry (com.netflix.spectator.api.NoopRegistry)5 Bootstrap (io.netty.bootstrap.Bootstrap)5 ByteBuf (io.netty.buffer.ByteBuf)5 Unpooled (io.netty.buffer.Unpooled)5