native.net.io to enable native kqueue for macOS 10.12+

This commit is contained in:
Vladimir Krivosheev
2017-07-06 16:42:38 +02:00
parent 515654e114
commit cb66cf35b8
4 changed files with 43 additions and 9 deletions
@@ -84,7 +84,6 @@ import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.oio.OioEventLoopGroup;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
@@ -120,6 +119,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static com.intellij.util.io.NettyKt.MultiThreadEventLoopGroup;
import static org.jetbrains.jps.api.CmdlineRemoteProto.Message.ControllerMessage.ParametersMessage.TargetTypeBuildScope;
/**
@@ -1397,7 +1397,7 @@ public class BuildManager implements Disposable {
BuiltInServer mainServer = StartupUtil.getServer();
boolean isOwnEventLoopGroup = !Registry.is("compiler.shared.event.group", true) || mainServer == null || mainServer.getEventLoopGroup() instanceof OioEventLoopGroup;
EventLoopGroup group = isOwnEventLoopGroup
? new NioEventLoopGroup(1, ConcurrencyUtil.newNamedThreadFactory("External compiler"))
? MultiThreadEventLoopGroup(1, ConcurrencyUtil.newNamedThreadFactory("External compiler"))
: mainServer.getEventLoopGroup();
final ServerBootstrap bootstrap = NettyKt.serverBootstrap(group);
bootstrap.childHandler(new ChannelInitializer() {
@@ -16,8 +16,11 @@
package com.intellij.util.io
import com.google.common.net.InetAddresses
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.util.Condition
import com.intellij.openapi.util.Conditions
import com.intellij.openapi.util.SystemInfo
import com.intellij.util.SystemProperties
import com.intellij.util.Url
import com.intellij.util.Urls
import com.intellij.util.net.NetUtils
@@ -26,6 +29,7 @@ import io.netty.bootstrap.BootstrapUtil
import io.netty.bootstrap.ServerBootstrap
import io.netty.buffer.ByteBuf
import io.netty.channel.*
import io.netty.channel.kqueue.KQueueEventLoopGroup
import io.netty.channel.nio.NioEventLoopGroup
import io.netty.channel.oio.OioEventLoopGroup
import io.netty.channel.socket.nio.NioServerSocketChannel
@@ -40,12 +44,14 @@ import io.netty.util.concurrent.GenericFutureListener
import org.jetbrains.concurrency.AsyncPromise
import org.jetbrains.concurrency.Promise
import org.jetbrains.ide.PooledThreadExecutor
import org.jetbrains.io.BuiltInServer
import org.jetbrains.io.NettyUtil
import java.io.IOException
import java.net.InetAddress
import java.net.InetSocketAddress
import java.net.NetworkInterface
import java.net.Socket
import java.util.concurrent.ThreadFactory
import java.util.concurrent.TimeUnit
// used in Go
@@ -304,4 +310,31 @@ fun HttpRequest.isWriteFromBrowserWithoutOrigin(): Boolean {
fun ByteBuf.readUtf8() = toString(Charsets.UTF_8)
fun ByteBuf.writeUtf8(data: CharSequence) = writeCharSequence(data, Charsets.UTF_8)
fun ByteBuf.writeUtf8(data: CharSequence) = writeCharSequence(data, Charsets.UTF_8)
fun MultiThreadEventLoopGroup(workerCount: Int, threadFactory: ThreadFactory): MultithreadEventLoopGroup {
if (SystemInfo.isMacOSSierra && SystemProperties.getBooleanProperty("native.net.io", false)) {
try {
return KQueueEventLoopGroup(workerCount, threadFactory)
}
catch (e: Throwable) {
logger<BuiltInServer>().warn("Cannot use native event loop group", e);
}
}
return NioEventLoopGroup(workerCount, threadFactory)
}
fun MultiThreadEventLoopGroup(workerCount: Int): MultithreadEventLoopGroup {
if (SystemInfo.isMacOSSierra && SystemProperties.getBooleanProperty("native.net.io", false)) {
try {
return KQueueEventLoopGroup(workerCount, PooledThreadExecutor.INSTANCE)
}
catch (e: Throwable) {
// error instead of warn to easy spot it
logger<BuiltInServer>().error("Cannot use native event loop group", e);
}
}
return NioEventLoopGroup(workerCount, PooledThreadExecutor.INSTANCE)
}
@@ -23,7 +23,6 @@ import com.intellij.util.NotNullProducer;
import com.intellij.util.SystemProperties;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.oio.OioEventLoopGroup;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -34,6 +33,7 @@ import java.util.Random;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import static com.intellij.util.io.NettyKt.MultiThreadEventLoopGroup;
import static com.intellij.util.io.NettyKt.serverBootstrap;
public class BuiltInServer implements Disposable {
@@ -84,7 +84,7 @@ public class BuiltInServer implements Disposable {
int portsCount,
boolean tryAnyPort,
@Nullable NotNullProducer<ChannelHandler> handler) throws Exception {
return start(new NioEventLoopGroup(workerCount, new BuiltInServerThreadFactory()), true, firstPort, portsCount, tryAnyPort, handler);
return start(MultiThreadEventLoopGroup(workerCount, new BuiltInServerThreadFactory()), true, firstPort, portsCount, tryAnyPort, handler);
}
@NotNull
@@ -96,7 +96,7 @@ public class BuiltInServer implements Disposable {
BuiltInServerThreadFactory threadFactory = new BuiltInServerThreadFactory();
EventLoopGroup loopGroup;
try {
loopGroup = new NioEventLoopGroup(workerCount, threadFactory);
loopGroup = MultiThreadEventLoopGroup(workerCount, threadFactory);
}
catch (IllegalStateException e) {
Logger.getInstance(BuiltInServer.class).warn(e);
@@ -16,9 +16,9 @@
package org.jetbrains.io;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.util.io.NettyKt;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpObjectAggregator;
@@ -31,7 +31,6 @@ import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.util.concurrent.GlobalEventExecutor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.TestOnly;
import org.jetbrains.ide.PooledThreadExecutor;
import java.io.IOException;
import java.net.BindException;
@@ -82,10 +81,12 @@ public final class NettyUtil {
(message.startsWith("Connection reset") || message.equals("Operation timed out") || message.equals("Connection timed out"));
}
@NotNull
public static Bootstrap nioClientBootstrap() {
return nioClientBootstrap(new NioEventLoopGroup(1, PooledThreadExecutor.INSTANCE));
return nioClientBootstrap(NettyKt.MultiThreadEventLoopGroup(2));
}
@NotNull
public static Bootstrap nioClientBootstrap(@NotNull EventLoopGroup eventLoopGroup) {
Bootstrap bootstrap = new Bootstrap().group(eventLoopGroup).channel(NioSocketChannel.class);
bootstrap.option(ChannelOption.TCP_NODELAY, true).option(ChannelOption.SO_KEEPALIVE, true);