Example usage for io.netty.channel ChannelOption SO_LINGER

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

Introduction

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

Prototype

ChannelOption SO_LINGER

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

Click Source Link

Usage

From source file:com.ancun.netty.httpserver.HttpServer.java

License:Apache License

private void setBootstrapOptions(ServerBootstrap bootstrap) {
    bootstrap.option(ChannelOption.SO_KEEPALIVE, useKeepAlive());
    bootstrap.option(ChannelOption.SO_BACKLOG, 1024);
    bootstrap.option(ChannelOption.TCP_NODELAY, useTcpNoDelay());
    bootstrap.option(ChannelOption.SO_KEEPALIVE, serverSettings.isKeepAlive());
    bootstrap.option(ChannelOption.SO_REUSEADDR, shouldReuseAddress());
    bootstrap.option(ChannelOption.SO_LINGER, getSoLinger());
    bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, getConnectTimeoutMillis());
    bootstrap.option(ChannelOption.SO_RCVBUF, getReceiveBufferSize());
    bootstrap.option(ChannelOption.MAX_MESSAGES_PER_READ, Integer.MAX_VALUE);

    bootstrap.childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(true));
    bootstrap.childOption(ChannelOption.MAX_MESSAGES_PER_READ, Integer.MAX_VALUE);
    bootstrap.childOption(ChannelOption.SO_RCVBUF, getReceiveBufferSize());
    bootstrap.childOption(ChannelOption.SO_REUSEADDR, shouldReuseAddress());
}

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

License:Apache License

public RpcServer(Class<? extends ServerChannel> serverChannelClass, RpcServerOptions serverOptions,
        RpcServiceRegistry rpcServiceRegistry) {
    if (rpcServiceRegistry == null) {
        throw new RuntimeException("protperty 'rpcServiceRegistry ' is null.");
    }//from  w  ww.  j a v a2  s .c o  m

    if (serverOptions == null) {
        serverOptions = new RpcServerOptions();
    }

    this.bossGroup = new NioEventLoopGroup(serverOptions.getAcceptorThreads());
    this.workerGroup = new NioEventLoopGroup(serverOptions.getWorkThreads());
    this.group(this.bossGroup, this.workerGroup);
    this.channel(serverChannelClass);

    this.option(ChannelOption.SO_BACKLOG, serverOptions.getBacklog());

    this.childOption(ChannelOption.SO_KEEPALIVE, serverOptions.isKeepAlive());
    this.childOption(ChannelOption.SO_REUSEADDR, true);
    this.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT);
    this.childOption(ChannelOption.TCP_NODELAY, serverOptions.isTcpNoDelay());
    this.childOption(ChannelOption.SO_LINGER, serverOptions.getSoLinger());
    this.childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, serverOptions.getConnectTimeout());
    this.childOption(ChannelOption.SO_RCVBUF, serverOptions.getReceiveBufferSize());
    this.childOption(ChannelOption.SO_SNDBUF, serverOptions.getSendBufferSize());

    this.rpcServiceRegistry = rpcServiceRegistry;
    // do register meta service
    rpcServiceRegistry.doRegisterMetaService();
    this.rpcServerOptions = serverOptions;
    this.rpcServerPipelineInitializer = new RpcServerPipelineInitializer(rpcServiceRegistry, rpcServerOptions);
    this.childHandler(rpcServerPipelineInitializer);
}

From source file:com.comphenix.protocol.compat.netty.independent.NettySocketAdapter.java

License:Open Source License

@Override
public int getSoLinger() throws SocketException {
    return ch.config().getOption(ChannelOption.SO_LINGER);
}

From source file:com.comphenix.protocol.compat.netty.independent.NettySocketAdapter.java

License:Open Source License

@Override
public void setSoLinger(boolean on, int linger) throws SocketException {
    ch.config().setOption(ChannelOption.SO_LINGER, linger);
}

From source file:com.corundumstudio.socketio.SocketIOServer.java

License:Apache License

protected void applyConnectionOptions(ServerBootstrap bootstrap) {
    SocketConfig config = configCopy.getSocketConfig();
    bootstrap.childOption(ChannelOption.TCP_NODELAY, config.isTcpNoDelay());
    if (config.getTcpSendBufferSize() != -1) {
        bootstrap.childOption(ChannelOption.SO_SNDBUF, config.getTcpSendBufferSize());
    }/*from   w  ww  . ja va2  s .c om*/
    if (config.getTcpReceiveBufferSize() != -1) {
        bootstrap.childOption(ChannelOption.SO_RCVBUF, config.getTcpReceiveBufferSize());
        bootstrap.childOption(ChannelOption.RCVBUF_ALLOCATOR,
                new FixedRecvByteBufAllocator(config.getTcpReceiveBufferSize()));
    }
    bootstrap.childOption(ChannelOption.SO_KEEPALIVE, config.isTcpKeepAlive());

    bootstrap.option(ChannelOption.SO_LINGER, config.getSoLinger());
    bootstrap.option(ChannelOption.SO_REUSEADDR, config.isReuseAddress());
    bootstrap.option(ChannelOption.SO_BACKLOG, config.getAcceptBackLog());
}

From source file:com.github.thinker0.mesos.MesosHealthCheckerServer.java

License:Apache License

/**
 * Initializes the server, socket, and channel.
 *
 * @param loopGroup          The event loop group.
 * @param serverChannelClass The socket channel class.
 * @throws InterruptedException on interruption.
 *//*from   ww  w.jav a2s. com*/
private void start(final EventLoopGroup loopGroup, final Class<? extends ServerChannel> serverChannelClass)
        throws InterruptedException {

    try {
        final InetSocketAddress inet = new InetSocketAddress(port);

        final ServerBootstrap b = new ServerBootstrap();
        b.option(ChannelOption.SO_BACKLOG, 1024);
        b.option(ChannelOption.SO_REUSEADDR, true);
        b.option(ChannelOption.SO_LINGER, 0);
        b.group(loopGroup).channel(serverChannelClass).childHandler(new WebServerInitializer());
        b.childOption(ChannelOption.ALLOCATOR, new PooledByteBufAllocator(true));
        b.childOption(ChannelOption.SO_REUSEADDR, true);

        // final Channel ch = b.bind(inet).sync().channel();
        // ch.closeFuture().sync();
        b.bind(inet);
        logger.info("Listening for Admin on {}", inet);
    } catch (Throwable t) {
        logger.warn(t.getMessage(), t);
    } finally {
        // loopGroup.shutdownGracefully().sync();
    }
}

From source file:com.linecorp.armeria.internal.ConnectionLimitingHandler.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    final Channel child = (Channel) msg;

    int conn = numConnections.incrementAndGet();
    if (conn > 0 && conn <= maxNumConnections) {
        childChannels.add(child);//w  ww  .j  a  v  a 2 s. c o  m
        child.closeFuture().addListener(future -> {
            childChannels.remove(child);
            numConnections.decrementAndGet();
        });
        super.channelRead(ctx, msg);
    } else {
        numConnections.decrementAndGet();

        // Set linger option to 0 so that the server doesn't get too many TIME_WAIT states.
        child.config().setOption(ChannelOption.SO_LINGER, 0);
        child.unsafe().closeForcibly();

        numDroppedConnections.increment();

        if (loggingScheduled.compareAndSet(false, true)) {
            ctx.executor().schedule(this::writeNumDroppedConnectionsLog, 1, TimeUnit.SECONDS);
        }
    }
}

From source file:com.navercorp.nbasearc.gcp.PhysicalConnection.java

License:Apache License

static PhysicalConnection create(String ip, int port, SingleThreadEventLoop eventLoop, Gateway gw,
        int reconnectInterval) {
    final PhysicalConnection pc = new PhysicalConnection(ip, port, eventLoop, gw, reconnectInterval);

    pc.b.group(eventLoop.getEventLoopGroup()).channel(NioSocketChannel.class)
            .option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_REUSEADDR, true)
            .option(ChannelOption.SO_KEEPALIVE, true).option(ChannelOption.SO_LINGER, 0)
            .option(ChannelOption.SO_SNDBUF, SOCKET_BUFFER).option(ChannelOption.SO_RCVBUF, SOCKET_BUFFER)
            .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, CONNECT_TIMEOUT)
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override//from w w w.j a v  a  2  s .c  om
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(pc.new PhysicalConnectionHandler());
                }
            });

    return pc;
}

From source file:com.twocater.diamond.core.netty.NettyConnector.java

@Override
public void bind() throws Exception {
    bossGroup = new NioEventLoopGroup();
    workerGroup = new NioEventLoopGroup();

    serverBootstrap = new ServerBootstrap();
    serverBootstrap.group(bossGroup, workerGroup);
    ChannelHandlerAdapter serverHandler = this.nettyHandlerFactory.createServerHandler();
    if (serverHandler != null) {
        serverBootstrap.handler(serverHandler);
    }//from www.  j  a  va 2s  .c o m
    serverBootstrap.channel(NioServerSocketChannel.class);
    serverBootstrap.childHandler(this.nettyHandlerFactory.createChildHandler(connectorConfig.getTimeout()));

    serverBootstrap.option(ChannelOption.SO_BACKLOG, connectorConfig.getSo_backlog_parent());
    serverBootstrap.option(ChannelOption.SO_REUSEADDR, connectorConfig.isSo_reuseaddr_parent());

    //         serverBootstrap.childOption(ChannelOption.CONNECT_TIMEOUT_MILLIS, 1000);//??? netty
    //        serverBootstrap.childOption(ChannelOption.SO_TIMEOUT, 5000);//??? ?timeout??
    serverBootstrap.childOption(ChannelOption.TCP_NODELAY, connectorConfig.isTcp_nodelay());
    serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, connectorConfig.isKeepAlive());

    serverBootstrap.childOption(ChannelOption.SO_REUSEADDR, connectorConfig.isSo_reuseaddr());
    serverBootstrap.childOption(ChannelOption.SO_LINGER, connectorConfig.getSo_linger());
    serverBootstrap.childOption(ChannelOption.SO_SNDBUF, connectorConfig.getSo_sndbuf());
    serverBootstrap.childOption(ChannelOption.SO_RCVBUF, connectorConfig.getSo_rcvbuf());

    channelFuture = serverBootstrap.bind(connectorConfig.getPort()).sync();
}

From source file:com.vmware.dcp.common.http.netty.NettyHttpListener.java

License:Open Source License

public void start(int port, String bindAddress) throws Throwable {
    ExecutorServiceFactory f = (tc) -> {
        return Executors.newFixedThreadPool(EVENT_LOOP_THREAD_COUNT,
                r -> new Thread(r, this.host.getUri().toString() + "/netty-listener/" + this.host.getId()));
    };// w  w w  .ja va 2 s. c  om

    this.parentGroup = new NioEventLoopGroup(EVENT_LOOP_THREAD_COUNT, f);
    this.childGroup = new NioEventLoopGroup(EVENT_LOOP_THREAD_COUNT, f);

    if (this.childChannelHandler == null) {
        this.childChannelHandler = new NettyHttpServerInitializer(this.host, this.sslContext);
    }

    ServerBootstrap b = new ServerBootstrap();
    b.group(this.parentGroup, this.childGroup).channel(NioServerSocketChannel.class)
            .childHandler(this.childChannelHandler);

    InetSocketAddress addr = null;
    if (bindAddress != null) {
        addr = new InetSocketAddress(bindAddress, port);
    } else {
        this.host.log(Level.WARNING, "*** Binding to all interfaces, please supply a bindAddress instead ***");
        addr = new InetSocketAddress(port);
    }
    this.serverChannel = b.bind(addr).sync().channel();
    this.serverChannel.config().setOption(ChannelOption.SO_LINGER, 0);
    this.port = ((InetSocketAddress) this.serverChannel.localAddress()).getPort();
}