Example usage for io.netty.channel ChannelOption ALLOCATOR

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

Introduction

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

Prototype

ChannelOption ALLOCATOR

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

Click Source Link

Usage

From source file:reactor.ipc.netty.options.NettyOptions.java

License:Open Source License

static void defaultNettyOptions(AbstractBootstrap<?, ?> bootstrap) {
    bootstrap.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
}

From source file:reactor.ipc.netty.options.ServerOptions.java

License:Open Source License

static void defaultServerOptions(ServerBootstrap bootstrap) {
    bootstrap.localAddress(LOCALHOST_AUTO_PORT).option(ChannelOption.SO_REUSEADDR, true)
            .option(ChannelOption.SO_BACKLOG, 1000)
            .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .childOption(ChannelOption.SO_RCVBUF, 1024 * 1024).childOption(ChannelOption.SO_SNDBUF, 1024 * 1024)
            .childOption(ChannelOption.AUTO_READ, false).childOption(ChannelOption.SO_KEEPALIVE, true)
            .childOption(ChannelOption.SO_LINGER, 0).childOption(ChannelOption.TCP_NODELAY, true)
            .childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 30000);
}

From source file:ru.releng.shameonyou.core.graphite.GraphiteClient.java

License:Apache License

private Bootstrap configureBootstrap(Bootstrap bootstrap, EventLoopGroup eventLoopGroup) {
    return bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class)
            .remoteAddress(graphiteHost, graphitePort).option(ChannelOption.SO_KEEPALIVE, true)
            .option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 1024 * 1024)
            .option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 128 * 1024)
            .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override//from  w  w w. java 2s. com
                public void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast("decoder", decoder);
                    ch.pipeline().addLast("encoder", encoder);
                    ch.pipeline().addLast("writeTimeoutHandler", new WriteTimeoutHandler(1));
                    ch.pipeline().addLast("handler", handler);
                }
            });
}

From source file:rxweb.engine.server.netty.NettyServer.java

License:Apache License

private void init() {
    this.bootstrap.option(ChannelOption.SO_KEEPALIVE, true).childOption(ChannelOption.SO_KEEPALIVE, true)
            .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT).group(new NioEventLoopGroup())
            .channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<Channel>() {
                @Override/*from   w w  w .  j a v a2s .  c o m*/
                protected void initChannel(Channel ch) throws Exception {
                    ch.pipeline().addLast(new LoggingHandler()).addLast(new HttpServerCodec())
                            .addLast(new NettyServerCodecHandlerAdapter())
                            .addLast(new ChannelInboundHandlerAdapter() {

                                private CompletableFuture<Channel> headersSent;

                                private final Logger logger = LoggerFactory.getLogger(getClass());

                                @Override
                                public void channelRead(ChannelHandlerContext ctx, Object msg)
                                        throws Exception {
                                    if (msg instanceof ServerRequest) {
                                        handleRequest(ctx, (ServerRequest) msg);
                                    } else {
                                        super.channelRead(ctx, msg);
                                    }
                                }

                                private void handleRequest(ChannelHandlerContext ctx, ServerRequest request) {
                                    List<ServerHandler> handlers = handlerResolver.resolve(request);
                                    if (handlers.isEmpty()) {
                                        this.logger.info("No handler found for request " + request.getUri());
                                    }
                                    // In order to keep simple, we take the first handler
                                    ServerHandler handler = handlers.get(0);
                                    ServerResponse response = new NettyServerResponseAdapter(request);
                                    handler.handle(request, response);

                                    response.getContent().subscribe(new Subscriber<ByteBuffer>() {

                                        @Override
                                        public void onNext(ByteBuffer buffer) {
                                            if (headersSent != null) {
                                                ctx.writeAndFlush(buffer);
                                            } else {
                                                headersSent = CompletableFutureUtils
                                                        .fromChannelFuture(ctx.write(response));

                                                headersSent.handle((channel, t) -> {
                                                    if (channel != null) {
                                                        response.setStatusAndHeadersSent(true);
                                                        ctx.write(buffer);
                                                    } else {
                                                        logger.error(t.toString());
                                                    }
                                                    return channel;
                                                });
                                            }
                                        }

                                        @Override
                                        public void onError(Throwable e) {
                                            logger.error("Error in response content observable: " + e);
                                        }

                                        @Override
                                        public void onCompleted() {
                                            if (response.isStatusAndHeadersSent()) {
                                                ctx.write(new DefaultLastHttpContent());
                                                ctx.flush();
                                                ctx.close();
                                            } else if (headersSent != null) {
                                                headersSent.thenRun(() -> {
                                                    ctx.write(new DefaultLastHttpContent());
                                                    ctx.flush();
                                                    ctx.close();
                                                });
                                            } else {
                                                CompletableFutureUtils.fromChannelFuture(ctx.write(response))
                                                        .thenRun(() -> {
                                                            response.setStatusAndHeadersSent(true);
                                                            ctx.write(new DefaultLastHttpContent());
                                                            ctx.flush();
                                                            ctx.close();
                                                        });
                                            }
                                        }

                                    });
                                }

                            });
                }

            });
}

From source file:sailfish.remoting.channel.AbstractConfigurableExchangeChannelGroup.java

License:Apache License

private Bootstrap newBootstrap() {
    Bootstrap boot = new Bootstrap();
    boot.channel(NettyPlatformIndependent.channelClass());
    boot.option(ChannelOption.TCP_NODELAY, true);
    // replace by heart beat
    boot.option(ChannelOption.SO_KEEPALIVE, false);
    // default is pooled direct
    // ByteBuf(io.netty.util.internal.PlatformDependent.DIRECT_BUFFER_PREFERRED)
    boot.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    // 32kb(for massive long connections, See
    // http://www.infoq.com/cn/articles/netty-million-level-push-service-design-points)
    // 64kb(RocketMq remoting default value)
    boot.option(ChannelOption.SO_SNDBUF, 32 * 1024);
    boot.option(ChannelOption.SO_RCVBUF, 32 * 1024);
    // temporary settings, need more tests
    boot.option(ChannelOption.WRITE_BUFFER_WATER_MARK, new WriteBufferWaterMark(8 * 1024, 32 * 1024));
    //default is true, reduce thread context switching
    boot.option(ChannelOption.SINGLE_EVENTEXECUTOR_PER_GROUP, true);
    return boot;//  w  w w .  jav a  2  s. c o  m
}

From source file:sailfish.remoting.DefaultServer.java

License:Apache License

private ServerBootstrap newServerBootstrap() {
    ServerBootstrap serverBoot = new ServerBootstrap();
    serverBoot.channel(NettyPlatformIndependent.serverChannelClass());
    // connections wait for accept
    serverBoot.option(ChannelOption.SO_BACKLOG, 1024);
    serverBoot.option(ChannelOption.SO_REUSEADDR, true);
    // replace by heart beat
    serverBoot.childOption(ChannelOption.SO_KEEPALIVE, false);
    serverBoot.childOption(ChannelOption.TCP_NODELAY, true);
    serverBoot.childOption(ChannelOption.SO_SNDBUF, 32 * 1024);
    serverBoot.childOption(ChannelOption.SO_RCVBUF, 32 * 1024);
    // temporary settings, need more tests
    serverBoot.childOption(ChannelOption.WRITE_BUFFER_WATER_MARK,
            new WriteBufferWaterMark(8 * 1024, 32 * 1024));
    serverBoot.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    //default is true, reduce thread context switching
    serverBoot.childOption(ChannelOption.SINGLE_EVENTEXECUTOR_PER_GROUP, true);
    return serverBoot;
}

From source file:tachyon.worker.netty.NettyDataServer.java

License:Apache License

private ServerBootstrap createBootstrap() {
    final ServerBootstrap boot = createBootstrapOfType(
            mTachyonConf.getEnum(Constants.WORKER_NETWORK_NETTY_CHANNEL, ChannelType.class));

    // use pooled buffers
    boot.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    boot.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);

    // set write buffer
    // this is the default, but its recommended to set it in case of change in future netty.
    boot.childOption(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK,
            (int) mTachyonConf.getBytes(Constants.WORKER_NETTY_WATERMARK_HIGH));
    boot.childOption(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK,
            (int) mTachyonConf.getBytes(Constants.WORKER_NETTY_WATERMARK_LOW));

    // more buffer settings on Netty socket option, one can tune them by specifying
    // properties, e.g.:
    // tachyon.worker.network.netty.backlog=50
    // tachyon.worker.network.netty.buffer.send=64KB
    // tachyon.worker.network.netty.buffer.receive=64KB
    if (mTachyonConf.containsKey(Constants.WORKER_NETTY_BACKLOG)) {
        boot.option(ChannelOption.SO_BACKLOG, mTachyonConf.getInt(Constants.WORKER_NETTY_BACKLOG));
    }//from  w w  w .j  a  v a2  s .c om
    if (mTachyonConf.containsKey(Constants.WORKER_NETTY_SEND_BUFFER)) {
        boot.option(ChannelOption.SO_SNDBUF, (int) mTachyonConf.getBytes(Constants.WORKER_NETTY_SEND_BUFFER));
    }
    if (mTachyonConf.containsKey(Constants.WORKER_NETTY_RECEIVE_BUFFER)) {
        boot.option(ChannelOption.SO_RCVBUF,
                (int) mTachyonConf.getBytes(Constants.WORKER_NETTY_RECEIVE_BUFFER));
    }
    return boot;
}

From source file:tk.jomp16.rcon.internal.RconServer.java

License:Open Source License

/**
 * Creates a new instance of RconServer//from   w  ww .jav a  2s  .c o  m
 *
 * @param host         the IP that the server will bind on
 * @param port         the port that the server will bind on
 * @param rconPassword the password to authenticate the users
 * @param epoll        if Netty server will run on epoll
 */
public RconServer(final String host, final int port, final String rconPassword, final boolean epoll) {
    this.host = host;
    this.port = port;
    this.rconPassword = rconPassword;

    if (this.rconPassword == null || this.rconPassword.isEmpty()) {
        log.error("Source RCON password not set!");

        return;
    }

    this.rconEvents = new LinkedList<>();

    this.bossGroup = epoll ? new EpollEventLoopGroup() : new NioEventLoopGroup();
    this.workerGroup = epoll ? new EpollEventLoopGroup() : new NioEventLoopGroup();

    this.serverBootstrap = new ServerBootstrap();

    this.serverBootstrap.group(this.bossGroup, this.workerGroup)
            .channel(epoll ? EpollServerSocketChannel.class : NioServerSocketChannel.class)
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(final SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new RconNettyDecoder(), new RconNettyEncoder(),
                            new RconNettyHandler(RconServer.this));
                }
            }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true)
            .childOption(ChannelOption.ALLOCATOR, UnpooledByteBufAllocator.DEFAULT);

    this.canStartServer = true;
}

From source file:tonivade.redis.RedisClient.java

License:Open Source License

public void start() {
    workerGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors());
    initHandler = new RedisInitializerHandler(this);
    connectionHandler = new RedisConnectionHandler(this);

    bootstrap = new Bootstrap().group(workerGroup).channel(NioSocketChannel.class)
            .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .option(ChannelOption.SO_RCVBUF, BUFFER_SIZE).option(ChannelOption.SO_SNDBUF, BUFFER_SIZE)
            .option(ChannelOption.SO_KEEPALIVE, true).handler(initHandler);

    try {/*  w w w.  ja v a 2s. co m*/
        connect();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:tonivade.redis.RedisServer.java

License:Open Source License

public void start() {
    bossGroup = new NioEventLoopGroup();
    workerGroup = new NioEventLoopGroup(Runtime.getRuntime().availableProcessors() * 2);
    acceptHandler = new RedisInitializerHandler(this);
    connectionHandler = new RedisConnectionHandler(this);

    bootstrap = new ServerBootstrap();
    bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(acceptHandler)
            .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
            .option(ChannelOption.SO_RCVBUF, BUFFER_SIZE).option(ChannelOption.SO_SNDBUF, BUFFER_SIZE)
            .childOption(ChannelOption.SO_KEEPALIVE, true)
            .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);

    future = bootstrap.bind(host, port);
    // Bind and start to accept incoming connections.
    future.syncUninterruptibly();//from  w ww  .  ja  va2  s.c o  m

    LOGGER.info(() -> "server started: " + host + ":" + port);
}