Class DnsNameResolver

java.lang.Object
io.grpc.NameResolver
io.grpc.internal.DnsNameResolver
Direct Known Subclasses:
GrpclbNameResolver

public class DnsNameResolver extends NameResolver
A DNS-based NameResolver.

Each A or AAAA record emits an EquivalentAddressGroup in the list passed to NameResolver.Listener2.onResult2(ResolutionResult).

See Also:
  • Field Details

    • logger

      private static final Logger logger
    • SERVICE_CONFIG_CHOICE_CLIENT_LANGUAGE_KEY

      private static final String SERVICE_CONFIG_CHOICE_CLIENT_LANGUAGE_KEY
      See Also:
    • SERVICE_CONFIG_CHOICE_PERCENTAGE_KEY

      private static final String SERVICE_CONFIG_CHOICE_PERCENTAGE_KEY
      See Also:
    • SERVICE_CONFIG_CHOICE_CLIENT_HOSTNAME_KEY

      private static final String SERVICE_CONFIG_CHOICE_CLIENT_HOSTNAME_KEY
      See Also:
    • SERVICE_CONFIG_CHOICE_SERVICE_CONFIG_KEY

      private static final String SERVICE_CONFIG_CHOICE_SERVICE_CONFIG_KEY
      See Also:
    • SERVICE_CONFIG_PREFIX

      static final String SERVICE_CONFIG_PREFIX
      See Also:
    • SERVICE_CONFIG_CHOICE_KEYS

      private static final Set<String> SERVICE_CONFIG_CHOICE_KEYS
    • SERVICE_CONFIG_NAME_PREFIX

      private static final String SERVICE_CONFIG_NAME_PREFIX
      See Also:
    • JNDI_PROPERTY

      private static final String JNDI_PROPERTY
    • JNDI_LOCALHOST_PROPERTY

      private static final String JNDI_LOCALHOST_PROPERTY
    • JNDI_TXT_PROPERTY

      private static final String JNDI_TXT_PROPERTY
    • NETWORKADDRESS_CACHE_TTL_PROPERTY

      static final String NETWORKADDRESS_CACHE_TTL_PROPERTY
      Java networking system properties name for caching DNS result.

      Default value is -1 (cache forever) if security manager is installed. If security manager is not installed, the ttl value is null which falls back to gRPC default value.

      For android, gRPC doesn't attempt to cache; this property value will be ignored.

      See Also:
    • DEFAULT_NETWORK_CACHE_TTL_SECONDS

      static final long DEFAULT_NETWORK_CACHE_TTL_SECONDS
      Default DNS cache duration if network cache ttl value is not specified (null).
      See Also:
    • enableJndi

      static boolean enableJndi
    • enableJndiLocalhost

      static boolean enableJndiLocalhost
    • enableTxt

      protected static boolean enableTxt
    • resourceResolverFactory

      private static final DnsNameResolver.ResourceResolverFactory resourceResolverFactory
    • proxyDetector

      final ProxyDetector proxyDetector
    • localHostname

      private static String localHostname
      Access through getLocalHostname().
    • random

      private final Random random
    • addressResolver

      protected volatile DnsNameResolver.AddressResolver addressResolver
    • resourceResolver

      private final AtomicReference<DnsNameResolver.ResourceResolver> resourceResolver
    • authority

      private final String authority
    • host

      private final String host
    • port

      private final int port
    • executorResource

      private final SharedResourceHolder.Resource<Executor> executorResource
      Executor that will be used if an Executor is not provide via NameResolver.Args.
    • cacheTtlNanos

      private final long cacheTtlNanos
    • syncContext

      private final SynchronizationContext syncContext
    • stopwatch

      private final com.google.common.base.Stopwatch stopwatch
    • resolved

      protected boolean resolved
    • shutdown

      private boolean shutdown
    • executor

      private Executor executor
    • usingExecutorResource

      private final boolean usingExecutorResource
      True if using an executor resource that should be released after use.
    • serviceConfigParser

      private final NameResolver.ServiceConfigParser serviceConfigParser
    • resolving

      private boolean resolving
    • listener

      private NameResolver.Listener2 listener
  • Constructor Details

  • Method Details

    • getServiceAuthority

      public String getServiceAuthority()
      Description copied from class: NameResolver
      Returns the authority used to authenticate connections to servers. It must be from a trusted source, because if the authority is tampered with, RPCs may be sent to the attackers which may leak sensitive user data.

      An implementation must generate it without blocking, typically in line, and must keep it unchanged. NameResolvers created from the same factory with the same argument must return the same authority.

      Specified by:
      getServiceAuthority in class NameResolver
    • getHost

      protected String getHost()
    • start

      public void start(NameResolver.Listener2 listener)
      Description copied from class: NameResolver
      Starts the resolution. The method is not supposed to throw any exceptions. That might cause the Channel that the name resolver is serving to crash. Errors should be propagated through NameResolver.Listener2.onError(io.grpc.Status).

      An instance may not be started more than once, by any overload of this method, even after an intervening call to NameResolver.shutdown().

      Overrides:
      start in class NameResolver
      Parameters:
      listener - used to receive updates on the target
    • refresh

      public void refresh()
      Description copied from class: NameResolver
      Re-resolve the name.

      Can only be called after NameResolver.start(io.grpc.NameResolver.Listener) has been called.

      This is only a hint. Implementation takes it as a signal but may not start resolution immediately. It should never throw.

      The default implementation is no-op.

      Overrides:
      refresh in class NameResolver
    • resolveAddresses

      private List<EquivalentAddressGroup> resolveAddresses()
    • resolveServiceConfig

      @Nullable private NameResolver.ConfigOrError resolveServiceConfig()
    • detectProxy

      @Nullable private EquivalentAddressGroup detectProxy() throws IOException
      Throws:
      IOException
    • doResolve

      protected DnsNameResolver.InternalResolutionResult doResolve(boolean forceTxt)
      Main logic of name resolution.
    • parseServiceConfig

      @Nullable static NameResolver.ConfigOrError parseServiceConfig(List<String> rawTxtRecords, Random random, String localHostname)
    • resolve

      private void resolve()
    • cacheRefreshRequired

      private boolean cacheRefreshRequired()
    • shutdown

      public void shutdown()
      Description copied from class: NameResolver
      Stops the resolution. Updates to the Listener will stop.
      Specified by:
      shutdown in class NameResolver
    • getPort

      final int getPort()
    • parseTxtResults

      static List<Map<String,?>> parseTxtResults(List<String> txtRecords) throws IOException
      Parse TXT service config records as JSON.
      Throws:
      IOException - if one of the txt records contains improperly formatted JSON.
    • getPercentageFromChoice

      @Nullable private static final Double getPercentageFromChoice(Map<String,?> serviceConfigChoice)
    • getClientLanguagesFromChoice

      @Nullable private static final List<String> getClientLanguagesFromChoice(Map<String,?> serviceConfigChoice)
    • getHostnamesFromChoice

      @Nullable private static final List<String> getHostnamesFromChoice(Map<String,?> serviceConfigChoice)
    • getNetworkAddressCacheTtlNanos

      private static long getNetworkAddressCacheTtlNanos(boolean isAndroid)
      Returns value of network address cache ttl property if not Android environment. For android, DnsNameResolver does not cache the dns lookup result.
    • maybeChooseServiceConfig

      @Nullable static Map<String,?> maybeChooseServiceConfig(Map<String,?> choice, Random random, String hostname)
      Determines if a given Service Config choice applies, and if so, returns it.
      Parameters:
      choice - The service config choice.
      Returns:
      The service config object or null if this choice does not apply.
      See Also:
    • setAddressResolver

      protected void setAddressResolver(DnsNameResolver.AddressResolver addressResolver)
    • setResourceResolver

      protected void setResourceResolver(DnsNameResolver.ResourceResolver resourceResolver)
    • getResourceResolver

      @Nullable protected DnsNameResolver.ResourceResolver getResourceResolver()
    • getResourceResolverFactory

      @Nullable static DnsNameResolver.ResourceResolverFactory getResourceResolverFactory(ClassLoader loader)
    • getLocalHostname

      private static String getLocalHostname()
    • shouldUseJndi

      protected static boolean shouldUseJndi(boolean jndiEnabled, boolean jndiLocalhostEnabled, String target)