mirror of https://github.com/grpc/grpc-java.git
core: narrow SharedResourceHolder types, and make the scheduler unconfigurable
This commit is contained in:
parent
21bd098d7b
commit
e7e88a9af8
|
|
@ -44,7 +44,7 @@ import java.util.Map;
|
||||||
import java.util.Map.Entry;
|
import java.util.Map.Entry;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
@ -135,11 +135,11 @@ final class DnsNameResolver extends NameResolver {
|
||||||
private final String authority;
|
private final String authority;
|
||||||
private final String host;
|
private final String host;
|
||||||
private final int port;
|
private final int port;
|
||||||
private final Resource<ExecutorService> executorResource;
|
private final Resource<Executor> executorResource;
|
||||||
@GuardedBy("this")
|
@GuardedBy("this")
|
||||||
private boolean shutdown;
|
private boolean shutdown;
|
||||||
@GuardedBy("this")
|
@GuardedBy("this")
|
||||||
private ExecutorService executor;
|
private Executor executor;
|
||||||
@GuardedBy("this")
|
@GuardedBy("this")
|
||||||
private boolean resolving;
|
private boolean resolving;
|
||||||
@GuardedBy("this")
|
@GuardedBy("this")
|
||||||
|
|
@ -148,7 +148,7 @@ final class DnsNameResolver extends NameResolver {
|
||||||
private final Runnable resolveRunnable;
|
private final Runnable resolveRunnable;
|
||||||
|
|
||||||
DnsNameResolver(@Nullable String nsAuthority, String name, Attributes params,
|
DnsNameResolver(@Nullable String nsAuthority, String name, Attributes params,
|
||||||
Resource<ExecutorService> executorResource, ProxyDetector proxyDetector,
|
Resource<Executor> executorResource, ProxyDetector proxyDetector,
|
||||||
Stopwatch stopwatch, boolean isAndroid) {
|
Stopwatch stopwatch, boolean isAndroid) {
|
||||||
// TODO: if a DNS server is provided as nsAuthority, use it.
|
// TODO: if a DNS server is provided as nsAuthority, use it.
|
||||||
// https://www.captechconsulting.com/blogs/accessing-the-dusty-corners-of-dns-with-java
|
// https://www.captechconsulting.com/blogs/accessing-the-dusty-corners-of-dns-with-java
|
||||||
|
|
|
||||||
|
|
@ -501,17 +501,17 @@ public final class GrpcUtil {
|
||||||
/**
|
/**
|
||||||
* Shared executor for channels.
|
* Shared executor for channels.
|
||||||
*/
|
*/
|
||||||
public static final Resource<ExecutorService> SHARED_CHANNEL_EXECUTOR =
|
public static final Resource<Executor> SHARED_CHANNEL_EXECUTOR =
|
||||||
new Resource<ExecutorService>() {
|
new Resource<Executor>() {
|
||||||
private static final String NAME = "grpc-default-executor";
|
private static final String NAME = "grpc-default-executor";
|
||||||
@Override
|
@Override
|
||||||
public ExecutorService create() {
|
public Executor create() {
|
||||||
return Executors.newCachedThreadPool(getThreadFactory(NAME + "-%d", true));
|
return Executors.newCachedThreadPool(getThreadFactory(NAME + "-%d", true));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close(ExecutorService instance) {
|
public void close(Executor instance) {
|
||||||
instance.shutdown();
|
((ExecutorService) instance).shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -549,7 +549,7 @@ public final class GrpcUtil {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
return service;
|
return Executors.unconfigurableScheduledExecutorService(service);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -58,7 +58,7 @@ import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
|
|
@ -96,15 +96,15 @@ public class DnsNameResolverTest {
|
||||||
private final FakeClock fakeClock = new FakeClock();
|
private final FakeClock fakeClock = new FakeClock();
|
||||||
private final FakeClock fakeExecutor = new FakeClock();
|
private final FakeClock fakeExecutor = new FakeClock();
|
||||||
|
|
||||||
private final Resource<ExecutorService> fakeExecutorResource =
|
private final Resource<Executor> fakeExecutorResource =
|
||||||
new Resource<ExecutorService>() {
|
new Resource<Executor>() {
|
||||||
@Override
|
@Override
|
||||||
public ExecutorService create() {
|
public Executor create() {
|
||||||
return fakeExecutor.getScheduledExecutorService();
|
return fakeExecutor.getScheduledExecutorService();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close(ExecutorService instance) {
|
public void close(Executor instance) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -124,16 +124,16 @@ public class OkHttpChannelBuilder extends
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
private static final long AS_LARGE_AS_INFINITE = TimeUnit.DAYS.toNanos(1000L);
|
private static final long AS_LARGE_AS_INFINITE = TimeUnit.DAYS.toNanos(1000L);
|
||||||
private static final Resource<ExecutorService> SHARED_EXECUTOR =
|
private static final Resource<Executor> SHARED_EXECUTOR =
|
||||||
new Resource<ExecutorService>() {
|
new Resource<Executor>() {
|
||||||
@Override
|
@Override
|
||||||
public ExecutorService create() {
|
public Executor create() {
|
||||||
return Executors.newCachedThreadPool(GrpcUtil.getThreadFactory("grpc-okhttp-%d", true));
|
return Executors.newCachedThreadPool(GrpcUtil.getThreadFactory("grpc-okhttp-%d", true));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close(ExecutorService executor) {
|
public void close(Executor executor) {
|
||||||
executor.shutdown();
|
((ExecutorService) executor).shutdown();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -606,7 +606,7 @@ public class OkHttpChannelBuilder extends
|
||||||
}
|
}
|
||||||
|
|
||||||
if (usingSharedExecutor) {
|
if (usingSharedExecutor) {
|
||||||
SharedResourceHolder.release(SHARED_EXECUTOR, (ExecutorService) executor);
|
SharedResourceHolder.release(SHARED_EXECUTOR, executor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue