io.netty.util.NettyRuntime Java Examples

The following examples show how to use io.netty.util.NettyRuntime. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Utils.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
DefaultEventLoopGroupResource(
    int numEventLoops, String name, EventLoopGroupType eventLoopGroupType) {
  this.name = name;
  // See the implementation of MultithreadEventLoopGroup.  DEFAULT_EVENT_LOOP_THREADS there
  // defaults to NettyRuntime.availableProcessors() * 2.  We don't think we need that many
  // threads.  The overhead of a thread includes file descriptors and at least one chunk
  // allocation from PooledByteBufAllocator.  Here we reduce the default number of threads by
  // half.
  if (numEventLoops == 0 && System.getProperty("io.netty.eventLoopThreads") == null) {
    this.numEventLoops = NettyRuntime.availableProcessors();
  } else {
    this.numEventLoops = numEventLoops;
  }
  this.eventLoopGroupType = eventLoopGroupType;
}
 
Example #2
Source File: Netty4Utils.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Set the number of available processors that Netty uses for sizing various resources (e.g., thread pools).
 *
 * @param availableProcessors the number of available processors
 * @throws IllegalStateException if available processors was set previously and the specified value does not match the already-set value
 */
public static void setAvailableProcessors(final int availableProcessors) {
    // we set this to false in tests to avoid tests that randomly set processors from stepping on each other
    final boolean set = Booleans.parseBoolean(System.getProperty("es.set.netty.runtime.available.processors", "true"));
    if (!set) {
        return;
    }

    /*
     * This can be invoked twice, once from Netty4Transport and another time from Netty4HttpServerTransport; however,
     * Netty4Runtime#availableProcessors forbids settings the number of processors twice so we prevent double invocation here.
     */
    if (isAvailableProcessorsSet.compareAndSet(false, true)) {
        NettyRuntime.setAvailableProcessors(availableProcessors);
    } else if (availableProcessors != NettyRuntime.availableProcessors()) {
        /*
         * We have previously set the available processors yet either we are trying to set it to a different value now or there is a bug
         * in Netty and our previous value did not take, bail.
         */
        final String message = String.format(
                Locale.ROOT,
                "available processors value [%d] did not match current value [%d]",
                availableProcessors,
                NettyRuntime.availableProcessors());
        throw new IllegalStateException(message);
    }
}
 
Example #3
Source File: AbstractServer.java    From pampas with Apache License 2.0 4 votes vote down vote up
public AbstractServer(String id, String groupName, String serverName, int port, ServerConfig config) {
        this.group = groupName;
        InetTools inetTools = new InetTools();
        InetAddress firstNonLoopbackAddress = inetTools.findFirstNonLoopbackAddress();
        inetTools.close();
//        this.address = getLocalHostLANAddress();
        this.address = firstNonLoopbackAddress;
        this.id = id == null ? group + "@" + address.getHostName() + ":" + port : id;
        this.startTimestamp = System.currentTimeMillis();
        this.version = CoreVersion.getVersion();
        this.serverName = serverName;
        this.port = port;
        this.config = config;
        serverStateRef = new AtomicReference<>(ServerState.Created);

        int bossThreads = ObjectUtils.defaultIfNull(config.getBoss(), 1);

        int defaultWorks = Math.max(1, SystemPropertyUtil.getInt(
                "io.netty.eventLoopThreads", NettyRuntime.availableProcessors() * 2));

        int workThreads = ObjectUtils.defaultIfNull(config.getWorker(), defaultWorks);

        boss = useEpoll() ? new EpollEventLoopGroup(bossThreads) : new NioEventLoopGroup(bossThreads);
        worker = useEpoll() ? new EpollEventLoopGroup(workThreads) : new NioEventLoopGroup(workThreads);
        bootstrap = new ServerBootstrap();

        bootstrap.group(boss, worker).channel(useEpoll() ? EpollServerSocketChannel.class : NioServerSocketChannel.class)
                // 启用Pool Bytebuf,http request的content将使用堆外pooled direct bytebuf,其它信息(header)仍然是堆内
                .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000)
                .option(ChannelOption.SO_BACKLOG, config.soBacklog)
                .childOption(ChannelOption.SO_KEEPALIVE, config.soKeepAlive)
                .childOption(ChannelOption.TCP_NODELAY, config.tcpNoDelay)
                .option(ChannelOption.SO_REUSEADDR, true)
                .option(ChannelOption.SO_RCVBUF, 24 * 1024)
                .childOption(ChannelOption.SO_SNDBUF, 24 * 1024)
                //ChannelOut boundBuffer 高水位线 低水位线
                .option(ChannelOption.WRITE_BUFFER_WATER_MARK, WriteBufferWaterMark.DEFAULT)
                .handler(new LoggingHandler(LogLevel.DEBUG)).childHandler(newChannelInitializer());

    }
 
Example #4
Source File: Server.java    From timely with Apache License 2.0 4 votes vote down vote up
public Server(Configuration conf) throws Exception {

        DEFAULT_EVENT_LOOP_THREADS = Math.max(1,
                SystemPropertyUtil.getInt("io.netty.eventLoopThreads", NettyRuntime.availableProcessors() * 2));
        this.config = conf;
    }
 
Example #5
Source File: NettyRuntimeWrapper.java    From metron with Apache License 2.0 4 votes vote down vote up
public static int availableProcessors() {
  return NettyRuntime.availableProcessors();
}