Example usage for io.netty.channel ChannelOption MESSAGE_SIZE_ESTIMATOR

List of usage examples for io.netty.channel ChannelOption MESSAGE_SIZE_ESTIMATOR

Introduction

In this page you can find the example usage for io.netty.channel ChannelOption MESSAGE_SIZE_ESTIMATOR.

Prototype

ChannelOption MESSAGE_SIZE_ESTIMATOR

To view the source code for io.netty.channel ChannelOption MESSAGE_SIZE_ESTIMATOR.

Click Source Link

Usage

From source file:com.addthis.hydra.query.web.QueryServer.java

License:Apache License

public void run() throws Exception {
    bossGroup = new NioEventLoopGroup(1);
    workerGroup = new NioEventLoopGroup();
    executorGroup = new DefaultEventExecutorGroup(AggregateConfig.FRAME_READER_THREADS,
            new ThreadFactoryBuilder().setDaemon(true).setNameFormat("frame-reader-%d").build());
    for (EventExecutor executor : executorGroup) {
        executor.execute(new NextQueryTask(queryQueue, executor));
    }/*  w w  w .  java2  s.  c om*/
    ServerBootstrap b = new ServerBootstrap();
    b.option(ChannelOption.SO_BACKLOG, 1024);
    b.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    b.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    b.childOption(ChannelOption.MESSAGE_SIZE_ESTIMATOR, new DefaultMessageSizeEstimator(200));
    b.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 100000000);
    b.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 50000000);
    b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(queryServerInitializer);
    b.bind(webPort).sync();
}

From source file:com.baidu.jprotobuf.pbrpc.transport.RpcClient.java

License:Apache License

public RpcClient(Class<? extends Channel> clientChannelClass, RpcClientOptions rpcClientOptions) {
    this.workerGroup = new NioEventLoopGroup();
    this.group(workerGroup);
    this.channel(clientChannelClass);
    this.handler(new RpcClientPipelineinitializer(this));
    this.rpcClientOptions = rpcClientOptions;
    this.option(ChannelOption.SO_REUSEADDR, rpcClientOptions.isReuseAddress());
    this.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, rpcClientOptions.getConnectTimeout());
    this.option(ChannelOption.SO_SNDBUF, rpcClientOptions.getSendBufferSize());
    this.option(ChannelOption.SO_RCVBUF, rpcClientOptions.getSendBufferSize());
    this.option(ChannelOption.SO_KEEPALIVE, rpcClientOptions.isKeepAlive());
    this.option(ChannelOption.TCP_NODELAY, rpcClientOptions.getTcpNoDelay());
    this.option(ChannelOption.MESSAGE_SIZE_ESTIMATOR,
            new DefaultMessageSizeEstimator(rpcClientOptions.getReceiveBufferSize()));

    // add count/*  ww  w.j a va2 s.c  om*/
    INSTANCE_COUNT.incrementAndGet();
}

From source file:com.spotify.folsom.client.DefaultRawMemcacheClient.java

License:Apache License

public static ListenableFuture<RawMemcacheClient> connect(final HostAndPort address,
        final int outstandingRequestLimit, final boolean binary, final Executor executor,
        final long timeoutMillis) {

    final ChannelInboundHandler decoder;
    if (binary) {
        decoder = new BinaryMemcacheDecoder();
    } else {/*w  ww .j  av a2 s.  com*/
        decoder = new AsciiMemcacheDecoder();
    }

    final ChannelHandler initializer = new ChannelInitializer<Channel>() {
        @Override
        protected void initChannel(final Channel ch) throws Exception {
            ch.pipeline().addLast(new TcpTuningHandler(), decoder,

                    // Downstream
                    new MemcacheEncoder());
        }
    };

    final SettableFuture<RawMemcacheClient> clientFuture = SettableFuture.create();

    final Bootstrap bootstrap = new Bootstrap().group(EVENT_LOOP_GROUP).handler(initializer)
            .channel(NioSocketChannel.class)
            .option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, SimpleSizeEstimator.INSTANCE);

    final ChannelFuture connectFuture = bootstrap
            .connect(new InetSocketAddress(address.getHostText(), address.getPort()));

    connectFuture.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(final ChannelFuture future) throws Exception {
            if (future.isSuccess()) {
                // Create client
                final RawMemcacheClient client = new DefaultRawMemcacheClient(address, future.channel(),
                        outstandingRequestLimit, executor, timeoutMillis);
                clientFuture.set(client);
            } else {
                clientFuture.setException(future.cause());
            }
        }
    });

    return onExecutor(clientFuture, executor);
}

From source file:com.spotify.netty4.handler.codec.zmtp.benchmarks.CustomReqRepBenchmark.java

License:Apache License

public static void main(final String... args) throws InterruptedException {
    final ProgressMeter meter = new ProgressMeter("requests", true);

    // Codecs/*w w  w .  j a v  a2 s  .  c  o m*/
    final ZMTPCodec serverCodec = ZMTPCodec.builder().socketType(ROUTER).encoder(ReplyEncoder.class)
            .decoder(RequestDecoder.class).build();

    final ZMTPCodec clientCodec = ZMTPCodec.builder().socketType(DEALER).encoder(RequestEncoder.class)
            .decoder(ReplyDecoder.class).build();

    // Server
    final Executor serverExecutor = new ForkJoinPool(1, ForkJoinPool.defaultForkJoinWorkerThreadFactory,
            UNCAUGHT_EXCEPTION_HANDLER, true);
    final ServerBootstrap serverBootstrap = new ServerBootstrap()
            .group(new NioEventLoopGroup(1), new NioEventLoopGroup()).channel(NioServerSocketChannel.class)
            .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .childOption(ChannelOption.MESSAGE_SIZE_ESTIMATOR, ByteBufSizeEstimator.INSTANCE)
            .childHandler(new ChannelInitializer<NioSocketChannel>() {
                @Override
                protected void initChannel(final NioSocketChannel ch) throws Exception {
                    ch.pipeline().addLast(serverCodec);
                    ch.pipeline().addLast(new ServerRequestTracker());
                    ch.pipeline().addLast(new ServerHandler(serverExecutor));
                }
            });
    final Channel server = serverBootstrap.bind(ANY_PORT).awaitUninterruptibly().channel();

    // Client
    final Executor clientExecutor = new ForkJoinPool(1, ForkJoinPool.defaultForkJoinWorkerThreadFactory,
            UNCAUGHT_EXCEPTION_HANDLER, true);
    final SocketAddress address = server.localAddress();
    final Bootstrap clientBootstrap = new Bootstrap().group(new NioEventLoopGroup())
            .channel(NioSocketChannel.class).option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, ByteBufSizeEstimator.INSTANCE)
            .handler(new ChannelInitializer<NioSocketChannel>() {
                @Override
                protected void initChannel(final NioSocketChannel ch) throws Exception {
                    ch.pipeline().addLast(clientCodec);
                    ch.pipeline().addLast(new ClientRequestTracker());
                    ch.pipeline().addLast(new ClientHandler(meter, clientExecutor));
                }
            });
    final Channel client = clientBootstrap.connect(address).awaitUninterruptibly().channel();

    // Run until client is closed
    client.closeFuture().await();
}

From source file:com.spotify.netty4.handler.codec.zmtp.benchmarks.ThroughputBenchmark.java

License:Apache License

public static void main(final String... args) throws InterruptedException {
    final ProgressMeter meter = new ProgressMeter("messages");

    // Server//from  w w  w  .ja  va2  s . c o  m
    final ServerBootstrap serverBootstrap = new ServerBootstrap()
            .group(new NioEventLoopGroup(1), new NioEventLoopGroup(1)).channel(NioServerSocketChannel.class)
            .childOption(ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .childHandler(new ChannelInitializer<NioSocketChannel>() {
                @Override
                protected void initChannel(final NioSocketChannel ch) throws Exception {
                    ch.pipeline().addLast(ZMTPCodec.of(ROUTER));
                    ch.pipeline().addLast(new ServerHandler(meter));
                }
            });
    final Channel server = serverBootstrap.bind(ANY_PORT).awaitUninterruptibly().channel();

    // Client
    final SocketAddress address = server.localAddress();
    final Bootstrap clientBootstrap = new Bootstrap().group(new NioEventLoopGroup(1))
            .channel(NioSocketChannel.class).option(ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, ByteBufSizeEstimator.INSTANCE)
            .handler(new ChannelInitializer<NioSocketChannel>() {
                @Override
                protected void initChannel(final NioSocketChannel ch) throws Exception {
                    ch.pipeline().addLast(ZMTPCodec.of(DEALER));
                    ch.pipeline().addLast(new ClientHandler());
                }
            });
    final Channel client = clientBootstrap.connect(address).awaitUninterruptibly().channel();

    // Run until client is closed
    client.closeFuture().await();
}

From source file:org.ethereum.net.server.PeerServerImpl.java

License:Open Source License

public void start(int port) {
    // TODO review listening use
    listening = true;/*from  w  w  w  .  j a v a 2 s .  c om*/

    EventLoopGroup bossGroup = new NioEventLoopGroup(1);
    EventLoopGroup workerGroup = new NioEventLoopGroup();

    ethereumChannelInitializer = ctx.getBean(EthereumChannelInitializer.class, "");

    ethereumListener.trace("Listening on port " + port);

    try {
        ServerBootstrap b = new ServerBootstrap();

        b.group(bossGroup, workerGroup);
        b.channel(NioServerSocketChannel.class);

        b.option(ChannelOption.SO_KEEPALIVE, true);
        b.option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, DefaultMessageSizeEstimator.DEFAULT);
        b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.peerConnectionTimeout());

        b.handler(new LoggingHandler());
        b.childHandler(ethereumChannelInitializer);

        // Start the client.
        logger.info("Listening for incoming connections, port: [{}] ", port);
        logger.info("NodeId: [{}] ", Hex.toHexString(config.nodeId()));

        ChannelFuture f = b.bind(port).sync();

        // Wait until the connection is closed.
        f.channel().closeFuture().sync();
        logger.debug("Connection is closed");

        // TODO review listening use
        listening = false;
    } catch (Exception e) {
        logger.debug("Exception: {} ({})", e.getMessage(), e.getClass().getName());
        throw new Error("Server Disconnected");
    } finally {
        workerGroup.shutdownGracefully();

    }
}

From source file:org.jupiter.transport.netty.NettyAcceptor.java

License:Apache License

protected void setOptions() {
    JConfig parent = configGroup().parent(); // parent options
    JConfig child = configGroup().child(); // child options

    setIoRatio(parent.getOption(JOption.IO_RATIO), child.getOption(JOption.IO_RATIO));

    boolean direct = child.getOption(JOption.PREFER_DIRECT);
    if (child.getOption(JOption.USE_POOLED_ALLOCATOR)) {
        if (direct) {
            allocator = new PooledByteBufAllocator(PlatformDependent.directBufferPreferred());
        } else {/*from   w w  w .  j a  v a2s  . c om*/
            allocator = new PooledByteBufAllocator(false);
        }
    } else {
        if (direct) {
            allocator = new UnpooledByteBufAllocator(PlatformDependent.directBufferPreferred());
        } else {
            allocator = new UnpooledByteBufAllocator(false);
        }
    }
    bootstrap.childOption(ChannelOption.ALLOCATOR, allocator).childOption(ChannelOption.MESSAGE_SIZE_ESTIMATOR,
            JMessageSizeEstimator.DEFAULT);
}

From source file:org.jupiter.transport.netty.NettyConnector.java

License:Apache License

protected void setOptions() {
    JConfig child = config();/*from   w ww . j  a va  2 s.  c  om*/

    setIoRatio(child.getOption(JOption.IO_RATIO));

    boolean direct = child.getOption(JOption.PREFER_DIRECT);
    if (child.getOption(JOption.USE_POOLED_ALLOCATOR)) {
        if (direct) {
            allocator = new PooledByteBufAllocator(PlatformDependent.directBufferPreferred());
        } else {
            allocator = new PooledByteBufAllocator(false);
        }
    } else {
        if (direct) {
            allocator = new UnpooledByteBufAllocator(PlatformDependent.directBufferPreferred());
        } else {
            allocator = new UnpooledByteBufAllocator(false);
        }
    }
    bootstrap.option(ChannelOption.ALLOCATOR, allocator).option(ChannelOption.MESSAGE_SIZE_ESTIMATOR,
            JMessageSizeEstimator.DEFAULT);
}