mirror of https://github.com/grpc/grpc-java.git
core: Simplify DnsNameResolver by using ObjectPool
ObjectPool is our standard solution for dealing with the sometimes-shutdown resources. This was implemented by a contributor not familiar with regular tools. There are wider changes that can be made here, but I chose to just do a smaller change because this class is used by GrpclbNameResolver.
This commit is contained in:
parent
210f9c083e
commit
6055adca5a
|
|
@ -134,10 +134,10 @@ public class DnsNameResolver extends NameResolver {
|
||||||
private final String host;
|
private final String host;
|
||||||
private final int port;
|
private final int port;
|
||||||
|
|
||||||
/** Executor that will be used if an Executor is not provide via {@link NameResolver.Args}. */
|
private final ObjectPool<Executor> executorPool;
|
||||||
private final Resource<Executor> executorResource;
|
|
||||||
private final long cacheTtlNanos;
|
private final long cacheTtlNanos;
|
||||||
private final SynchronizationContext syncContext;
|
private final SynchronizationContext syncContext;
|
||||||
|
private final ServiceConfigParser serviceConfigParser;
|
||||||
|
|
||||||
// Following fields must be accessed from syncContext
|
// Following fields must be accessed from syncContext
|
||||||
private final Stopwatch stopwatch;
|
private final Stopwatch stopwatch;
|
||||||
|
|
@ -145,10 +145,6 @@ public class DnsNameResolver extends NameResolver {
|
||||||
private boolean shutdown;
|
private boolean shutdown;
|
||||||
private Executor executor;
|
private Executor executor;
|
||||||
|
|
||||||
/** True if using an executor resource that should be released after use. */
|
|
||||||
private final boolean usingExecutorResource;
|
|
||||||
private final ServiceConfigParser serviceConfigParser;
|
|
||||||
|
|
||||||
private boolean resolving;
|
private boolean resolving;
|
||||||
|
|
||||||
// The field must be accessed from syncContext, although the methods on an Listener2 can be called
|
// The field must be accessed from syncContext, although the methods on an Listener2 can be called
|
||||||
|
|
@ -165,7 +161,7 @@ public class DnsNameResolver extends NameResolver {
|
||||||
checkNotNull(args, "args");
|
checkNotNull(args, "args");
|
||||||
// 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
|
||||||
this.executorResource = executorResource;
|
|
||||||
// Must prepend a "//" to the name when constructing a URI, otherwise it will be treated as an
|
// Must prepend a "//" to the name when constructing a URI, otherwise it will be treated as an
|
||||||
// opaque URI, thus the authority and host of the resulted URI would be null.
|
// opaque URI, thus the authority and host of the resulted URI would be null.
|
||||||
URI nameUri = URI.create("//" + checkNotNull(name, "name"));
|
URI nameUri = URI.create("//" + checkNotNull(name, "name"));
|
||||||
|
|
@ -179,11 +175,15 @@ public class DnsNameResolver extends NameResolver {
|
||||||
port = nameUri.getPort();
|
port = nameUri.getPort();
|
||||||
}
|
}
|
||||||
this.proxyDetector = checkNotNull(args.getProxyDetector(), "proxyDetector");
|
this.proxyDetector = checkNotNull(args.getProxyDetector(), "proxyDetector");
|
||||||
|
Executor offloadExecutor = args.getOffloadExecutor();
|
||||||
|
if (offloadExecutor != null) {
|
||||||
|
this.executorPool = new FixedObjectPool<>(offloadExecutor);
|
||||||
|
} else {
|
||||||
|
this.executorPool = SharedResourcePool.forResource(executorResource);
|
||||||
|
}
|
||||||
this.cacheTtlNanos = getNetworkAddressCacheTtlNanos(isAndroid);
|
this.cacheTtlNanos = getNetworkAddressCacheTtlNanos(isAndroid);
|
||||||
this.stopwatch = checkNotNull(stopwatch, "stopwatch");
|
this.stopwatch = checkNotNull(stopwatch, "stopwatch");
|
||||||
this.syncContext = checkNotNull(args.getSynchronizationContext(), "syncContext");
|
this.syncContext = checkNotNull(args.getSynchronizationContext(), "syncContext");
|
||||||
this.executor = args.getOffloadExecutor();
|
|
||||||
this.usingExecutorResource = executor == null;
|
|
||||||
this.serviceConfigParser = checkNotNull(args.getServiceConfigParser(), "serviceConfigParser");
|
this.serviceConfigParser = checkNotNull(args.getServiceConfigParser(), "serviceConfigParser");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -200,9 +200,7 @@ public class DnsNameResolver extends NameResolver {
|
||||||
@Override
|
@Override
|
||||||
public void start(Listener2 listener) {
|
public void start(Listener2 listener) {
|
||||||
Preconditions.checkState(this.listener == null, "already started");
|
Preconditions.checkState(this.listener == null, "already started");
|
||||||
if (usingExecutorResource) {
|
executor = executorPool.getObject();
|
||||||
executor = SharedResourceHolder.get(executorResource);
|
|
||||||
}
|
|
||||||
this.listener = checkNotNull(listener, "listener");
|
this.listener = checkNotNull(listener, "listener");
|
||||||
resolve();
|
resolve();
|
||||||
}
|
}
|
||||||
|
|
@ -413,8 +411,8 @@ public class DnsNameResolver extends NameResolver {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
shutdown = true;
|
shutdown = true;
|
||||||
if (executor != null && usingExecutorResource) {
|
if (executor != null) {
|
||||||
executor = SharedResourceHolder.release(executorResource, executor);
|
executor = executorPool.returnObject(executor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue