execute long-running builtin server in a separate thread to make it distinguishable in a stacktrace

This commit is contained in:
Alexey Kudravtsev
2015-10-27 14:20:33 +03:00
parent e2f947baaa
commit 9a2829dab0
@@ -18,6 +18,7 @@ package org.jetbrains.io;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.util.ArrayUtil;
import com.intellij.util.ConcurrencyUtil;
import com.intellij.util.ExceptionUtil;
import com.intellij.util.NotNullProducer;
import com.intellij.util.net.NetUtils;
@@ -26,10 +27,11 @@ import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.ide.PooledThreadExecutor;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class BuiltInServer implements Disposable {
// Some antiviral software detect viruses by the fact of accessing these ports so we should not touch them to appear innocent.
@@ -75,7 +77,11 @@ public class BuiltInServer implements Disposable {
int portsCount,
boolean tryAnyPort,
@Nullable NotNullProducer<ChannelHandler> handler) throws Exception {
return start(new NioEventLoopGroup(workerCount, PooledThreadExecutor.INSTANCE), true, firstPort, portsCount, tryAnyPort, handler);
return start(new NioEventLoopGroup(workerCount, createExecutor()), true, firstPort, portsCount, tryAnyPort, handler);
}
private static ExecutorService createExecutor() {
return Executors.newCachedThreadPool(ConcurrencyUtil.newNamedThreadFactory("Netty Builtin Server"));
}
@NotNull
@@ -94,11 +100,11 @@ public class BuiltInServer implements Disposable {
static void configureChildHandler(@NotNull ServerBootstrap bootstrap,
@NotNull final ChannelRegistrar channelRegistrar,
final @Nullable NotNullProducer<ChannelHandler> channelHandler) {
@Nullable final NotNullProducer<ChannelHandler> channelHandler) {
final PortUnificationServerHandler portUnificationServerHandler = channelHandler == null ? new PortUnificationServerHandler() : null;
bootstrap.childHandler(new ChannelInitializer() {
@Override
protected void initChannel(Channel channel) throws Exception {
protected void initChannel(@NotNull Channel channel) throws Exception {
channel.pipeline().addLast(channelRegistrar, channelHandler == null ? portUnificationServerHandler : channelHandler.produce());
}
});
@@ -123,7 +129,7 @@ public class BuiltInServer implements Disposable {
channelRegistrar.add(future.channel());
return port;
}
else if (!tryAnyPort && i == (portsCount - 1)) {
else if (!tryAnyPort && i == portsCount - 1) {
ExceptionUtil.rethrowAll(future.cause());
}
}
@@ -134,9 +140,7 @@ public class BuiltInServer implements Disposable {
channelRegistrar.add(future.channel());
return ((InetSocketAddress)future.channel().localAddress()).getPort();
}
else {
ExceptionUtil.rethrowAll(future.cause());
}
ExceptionUtil.rethrowAll(future.cause());
return -1; // unreachable
}