Search in sources :

Example 1 with TurboChannelInitializer

use of rpc.turbo.transport.client.handler.TurboChannelInitializer in project turbo-rpc by hank-whu.

the class NettyClientConnector method connect.

void connect() throws InterruptedException {
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(eventLoopGroup);
    bootstrap.option(ChannelOption.SO_REUSEADDR, true);
    bootstrap.option(ChannelOption.SO_RCVBUF, 256 * 1024);
    bootstrap.option(ChannelOption.SO_SNDBUF, 256 * 1024);
    // 
    bootstrap.option(// 
    ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(1024 * 1024, 2048 * 1024));
    if (eventLoopGroup instanceof EpollEventLoopGroup) {
        bootstrap.option(EpollChannelOption.SO_REUSEPORT, true);
        bootstrap.channel(EpollSocketChannel.class);
    } else if (eventLoopGroup instanceof NioEventLoopGroup) {
        bootstrap.channel(NioSocketChannel.class);
    }
    bootstrap.handler(new TurboChannelInitializer(serializer));
    Sender[] newSenders = new Sender[connectCount];
    for (int i = 0; i < connectCount; i++) {
        Channel channel = bootstrap.connect(serverAddress.host, serverAddress.port).sync().channel();
        newSenders[i] = new BatchSender(channel);
        if (logger.isInfoEnabled()) {
            logger.info(serverAddress + " connect " + i + "/" + connectCount);
        }
        if (i == 0) {
            InetSocketAddress insocket = (InetSocketAddress) channel.localAddress();
            clientAddress = new HostPort(insocket.getAddress().getHostAddress(), 0);
        }
    }
    Sender[] old = senders;
    senders = newSenders;
    if (old != null) {
        for (int i = 0; i < old.length; i++) {
            try {
                old[i].close();
            } catch (Exception e) {
                if (logger.isWarnEnabled()) {
                    logger.warn("关闭出错", e);
                }
            }
        }
    }
}
Also used : BatchSender(rpc.turbo.transport.client.sender.BatchSender) InetSocketAddress(java.net.InetSocketAddress) TurboChannelInitializer(rpc.turbo.transport.client.handler.TurboChannelInitializer) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) HostPort(rpc.turbo.config.HostPort) IOException(java.io.IOException) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) BatchSender(rpc.turbo.transport.client.sender.BatchSender) Sender(rpc.turbo.transport.client.sender.Sender) EpollEventLoopGroup(io.netty.channel.epoll.EpollEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) WriteBufferWaterMark(io.netty.channel.WriteBufferWaterMark) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

Bootstrap (io.netty.bootstrap.Bootstrap)1 Channel (io.netty.channel.Channel)1 WriteBufferWaterMark (io.netty.channel.WriteBufferWaterMark)1 EpollEventLoopGroup (io.netty.channel.epoll.EpollEventLoopGroup)1 EpollSocketChannel (io.netty.channel.epoll.EpollSocketChannel)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 HostPort (rpc.turbo.config.HostPort)1 TurboChannelInitializer (rpc.turbo.transport.client.handler.TurboChannelInitializer)1 BatchSender (rpc.turbo.transport.client.sender.BatchSender)1 Sender (rpc.turbo.transport.client.sender.Sender)1