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.Random;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.logging.Level;
|
||||
|
|
@ -135,11 +135,11 @@ final class DnsNameResolver extends NameResolver {
|
|||
private final String authority;
|
||||
private final String host;
|
||||
private final int port;
|
||||
private final Resource<ExecutorService> executorResource;
|
||||
private final Resource<Executor> executorResource;
|
||||
@GuardedBy("this")
|
||||
private boolean shutdown;
|
||||
@GuardedBy("this")
|
||||
private ExecutorService executor;
|
||||
private Executor executor;
|
||||
@GuardedBy("this")
|
||||
private boolean resolving;
|
||||
@GuardedBy("this")
|
||||
|
|
@ -148,7 +148,7 @@ final class DnsNameResolver extends NameResolver {
|
|||
private final Runnable resolveRunnable;
|
||||
|
||||
DnsNameResolver(@Nullable String nsAuthority, String name, Attributes params,
|
||||
Resource<ExecutorService> executorResource, ProxyDetector proxyDetector,
|
||||
Resource<Executor> executorResource, ProxyDetector proxyDetector,
|
||||
Stopwatch stopwatch, boolean isAndroid) {
|
||||
// TODO: if a DNS server is provided as nsAuthority, use it.
|
||||
// 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.
|
||||
*/
|
||||
public static final Resource<ExecutorService> SHARED_CHANNEL_EXECUTOR =
|
||||
new Resource<ExecutorService>() {
|
||||
public static final Resource<Executor> SHARED_CHANNEL_EXECUTOR =
|
||||
new Resource<Executor>() {
|
||||
private static final String NAME = "grpc-default-executor";
|
||||
@Override
|
||||
public ExecutorService create() {
|
||||
public Executor create() {
|
||||
return Executors.newCachedThreadPool(getThreadFactory(NAME + "-%d", true));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close(ExecutorService instance) {
|
||||
instance.shutdown();
|
||||
public void close(Executor instance) {
|
||||
((ExecutorService) instance).shutdown();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -549,7 +549,7 @@ public final class GrpcUtil {
|
|||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return service;
|
||||
return Executors.unconfigurableScheduledExecutorService(service);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ import java.util.LinkedHashMap;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javax.annotation.Nullable;
|
||||
import org.junit.After;
|
||||
|
|
@ -96,15 +96,15 @@ public class DnsNameResolverTest {
|
|||
private final FakeClock fakeClock = new FakeClock();
|
||||
private final FakeClock fakeExecutor = new FakeClock();
|
||||
|
||||
private final Resource<ExecutorService> fakeExecutorResource =
|
||||
new Resource<ExecutorService>() {
|
||||
private final Resource<Executor> fakeExecutorResource =
|
||||
new Resource<Executor>() {
|
||||
@Override
|
||||
public ExecutorService create() {
|
||||
public Executor create() {
|
||||
return fakeExecutor.getScheduledExecutorService();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close(ExecutorService instance) {
|
||||
public void close(Executor instance) {
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -124,16 +124,16 @@ public class OkHttpChannelBuilder extends
|
|||
.build();
|
||||
|
||||
private static final long AS_LARGE_AS_INFINITE = TimeUnit.DAYS.toNanos(1000L);
|
||||
private static final Resource<ExecutorService> SHARED_EXECUTOR =
|
||||
new Resource<ExecutorService>() {
|
||||
private static final Resource<Executor> SHARED_EXECUTOR =
|
||||
new Resource<Executor>() {
|
||||
@Override
|
||||
public ExecutorService create() {
|
||||
public Executor create() {
|
||||
return Executors.newCachedThreadPool(GrpcUtil.getThreadFactory("grpc-okhttp-%d", true));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close(ExecutorService executor) {
|
||||
executor.shutdown();
|
||||
public void close(Executor executor) {
|
||||
((ExecutorService) executor).shutdown();
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -606,7 +606,7 @@ public class OkHttpChannelBuilder extends
|
|||
}
|
||||
|
||||
if (usingSharedExecutor) {
|
||||
SharedResourceHolder.release(SHARED_EXECUTOR, (ExecutorService) executor);
|
||||
SharedResourceHolder.release(SHARED_EXECUTOR, executor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue