Example usage for io.netty.channel.socket SocketChannelConfig setTcpNoDelay

List of usage examples for io.netty.channel.socket SocketChannelConfig setTcpNoDelay

Introduction

In this page you can find the example usage for io.netty.channel.socket SocketChannelConfig setTcpNoDelay.

Prototype

SocketChannelConfig setTcpNoDelay(boolean tcpNoDelay);

Source Link

Document

Sets the StandardSocketOptions#TCP_NODELAY option.

Usage

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

License:Apache License

@Override
public void channelActive(final ChannelHandlerContext ctx) throws Exception {
    final SocketChannelConfig config = (SocketChannelConfig) ctx.channel().config();

    // Disable Nagle's algorithm
    config.setTcpNoDelay(true);

    // Setup TCP keepalive
    config.setKeepAlive(true);/*from  w ww  .  j a v  a  2 s  . co  m*/

    super.channelActive(ctx);

    // Our work is done
    ctx.channel().pipeline().remove(this);
}

From source file:reactor.io.net.impl.netty.tcp.NettyTcpServer.java

License:Apache License

@Override
protected Promise<Void> doStart(final ReactorChannelHandler<IN, OUT, ChannelStream<IN, OUT>> handler) {

    bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
        @Override//from w w w  .  ja v  a 2 s . c  o m
        public void initChannel(final SocketChannel ch) throws Exception {
            if (nettyOptions != null) {
                SocketChannelConfig config = ch.config();
                config.setReceiveBufferSize(nettyOptions.rcvbuf());
                config.setSendBufferSize(nettyOptions.sndbuf());
                config.setKeepAlive(nettyOptions.keepAlive());
                config.setReuseAddress(nettyOptions.reuseAddr());
                config.setSoLinger(nettyOptions.linger());
                config.setTcpNoDelay(nettyOptions.tcpNoDelay());
            }

            if (log.isDebugEnabled()) {
                log.debug("CONNECT {}", ch);
            }

            if (null != getSslOptions()) {
                SSLEngine ssl = new SSLEngineSupplier(getSslOptions(), false).get();
                if (log.isDebugEnabled()) {
                    log.debug("SSL enabled using keystore {}",
                            (null != getSslOptions().keystoreFile() ? getSslOptions().keystoreFile()
                                    : "<DEFAULT>"));
                }
                ch.pipeline().addLast(new SslHandler(ssl));
            }

            if (null != nettyOptions && null != nettyOptions.pipelineConfigurer()) {
                nettyOptions.pipelineConfigurer().accept(ch.pipeline());
            }

            bindChannel(handler, ch);
        }
    });

    ChannelFuture bindFuture = bootstrap.bind();

    final Promise<Void> promise = Promises.prepare();
    bindFuture.addListener(new ChannelFutureListener() {
        @Override
        public void operationComplete(ChannelFuture future) throws Exception {
            log.info("BIND {}", future.channel().localAddress());
            if (future.isSuccess()) {
                if (listenAddress.getPort() == 0) {
                    listenAddress = (InetSocketAddress) future.channel().localAddress();
                }
                promise.onComplete();
            } else {
                promise.onError(future.cause());
            }
        }
    });

    return promise;
}

From source file:reactor.io.net.netty.tcp.NettyTcpServer.java

License:Apache License

protected NettyTcpServer(@Nonnull Environment env, @Nonnull EventBus reactor,
        @Nullable InetSocketAddress listenAddress, final ServerSocketOptions options,
        final SslOptions sslOptions, @Nullable Codec<Buffer, IN, OUT> codec,
        @Nonnull Collection<Consumer<NetChannel<IN, OUT>>> consumers) {
    super(env, reactor, listenAddress, options, sslOptions, codec, consumers);

    if (options instanceof NettyServerSocketOptions) {
        this.nettyOptions = (NettyServerSocketOptions) options;
    } else {//w w w. j  a v  a 2  s .  c om
        this.nettyOptions = null;
    }

    int selectThreadCount = env.getProperty("reactor.tcp.selectThreadCount", Integer.class,
            Environment.PROCESSORS / 2);
    int ioThreadCount = env.getProperty("reactor.tcp.ioThreadCount", Integer.class, Environment.PROCESSORS);
    this.selectorGroup = new NioEventLoopGroup(selectThreadCount,
            new NamedDaemonThreadFactory("reactor-tcp-select"));
    if (null != nettyOptions && null != nettyOptions.eventLoopGroup()) {
        this.ioGroup = nettyOptions.eventLoopGroup();
    } else {
        this.ioGroup = new NioEventLoopGroup(ioThreadCount, new NamedDaemonThreadFactory("reactor-tcp-io"));
    }

    this.bootstrap = new ServerBootstrap().group(selectorGroup, ioGroup).channel(NioServerSocketChannel.class)
            .option(ChannelOption.SO_BACKLOG, options.backlog())
            .option(ChannelOption.SO_RCVBUF, options.rcvbuf()).option(ChannelOption.SO_SNDBUF, options.sndbuf())
            .option(ChannelOption.SO_REUSEADDR, options.reuseAddr())
            .localAddress((null == listenAddress ? new InetSocketAddress(3000) : listenAddress))
            .childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                public void initChannel(final SocketChannel ch) throws Exception {
                    SocketChannelConfig config = ch.config();
                    config.setReceiveBufferSize(options.rcvbuf());
                    config.setSendBufferSize(options.sndbuf());
                    config.setKeepAlive(options.keepAlive());
                    config.setReuseAddress(options.reuseAddr());
                    config.setSoLinger(options.linger());
                    config.setTcpNoDelay(options.tcpNoDelay());

                    if (log.isDebugEnabled()) {
                        log.debug("CONNECT {}", ch);
                    }

                    if (null != sslOptions) {
                        SSLEngine ssl = new SSLEngineSupplier(sslOptions, false).get();
                        if (log.isDebugEnabled()) {
                            log.debug("SSL enabled using keystore {}",
                                    (null != sslOptions.keystoreFile() ? sslOptions.keystoreFile()
                                            : "<DEFAULT>"));
                        }
                        ch.pipeline().addLast(new SslHandler(ssl));
                    }
                    if (null != nettyOptions && null != nettyOptions.pipelineConfigurer()) {
                        nettyOptions.pipelineConfigurer().accept(ch.pipeline());
                    }
                    ch.pipeline().addLast(createChannelHandlers(ch));
                    ch.closeFuture().addListener(new ChannelFutureListener() {
                        @Override
                        public void operationComplete(ChannelFuture future) throws Exception {
                            if (log.isDebugEnabled()) {
                                log.debug("CLOSE {}", ch);
                            }
                            close(ch);
                        }
                    });
                }
            });
}