• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  * SPDX-License-Identifier: Apache-2.0.
4  */
5 package software.amazon.awssdk.crt.io;
6 
7 import software.amazon.awssdk.crt.CrtResource;
8 import software.amazon.awssdk.crt.CrtRuntimeException;
9 
10 /**
11  * Java wrapper around the native CRT host resolver, responsible for performing async dns lookups
12  */
13 public class HostResolver extends CrtResource {
14     private final static int DEFAULT_MAX_ENTRIES = 8;
15 
16     /**
17      *
18      * @param elg event loop group to pass to the host resolver.  Not currently used but still mandatory.
19      */
HostResolver(EventLoopGroup elg)20     public HostResolver(EventLoopGroup elg) throws CrtRuntimeException {
21         this(elg, DEFAULT_MAX_ENTRIES);
22     }
23 
24     /**
25      *
26      * @param elg event loop group to pass to the host resolver.  Not currently used but still mandatory.
27      * @param maxEntries maximum size of the name to address mapping cache
28      */
HostResolver(EventLoopGroup elg, int maxEntries)29     public HostResolver(EventLoopGroup elg, int maxEntries) throws CrtRuntimeException {
30         acquireNativeHandle(hostResolverNew(elg.getNativeHandle(), maxEntries));
31         addReferenceTo(elg);
32     }
33 
34     /**
35      * Determines whether a resource releases its dependencies at the same time the native handle is released or if it waits.
36      * Resources that wait are responsible for calling releaseReferences() manually.
37      */
38     @Override
canReleaseReferencesImmediately()39     protected boolean canReleaseReferencesImmediately() { return true; }
40 
41     /**
42      * Cleans up the resolver's associated native handle
43      */
44     @Override
releaseNativeHandle()45     protected void releaseNativeHandle() {
46         if (!isNull()) {
47             hostResolverRelease(getNativeHandle());
48         }
49     }
50 
51     /*
52      * Static interface for access to a default, lazily-created host resolver for users who don't
53      * want to deal with the associated resource management.  Client bootstraps will use this host resolver
54      * if they are passed a null value.
55      */
56 
57     /**
58      * Sets the max number of cached host entries for the static default resolver, if it's ever created/used. Has no
59      * effect if the static default host resolver has already been created.
60      *
61      * @param maxEntries maximum number of host entries cached
62      */
setStaticDefaultMaxEntries(int maxEntries)63     public static void setStaticDefaultMaxEntries(int maxEntries) {
64         synchronized (HostResolver.class) {
65             staticDefaultMaxEntries = maxEntries;
66         }
67     }
68 
69     /**
70      * Closes the static default host resolver, if it exists.  Primarily intended for tests that use the static
71      * default resolver, before they call waitForNoResources().
72      */
closeStaticDefault()73     public static void closeStaticDefault() {
74         synchronized (HostResolver.class) {
75             if (staticDefaultResolver != null) {
76                 staticDefaultResolver.close();
77             }
78             staticDefaultResolver = null;
79         }
80     }
81 
82     /**
83      * Gets the static default HostResolver, creating it if necessary.
84      *
85      * This default will be used when a HostResolver is not explicitly passed but is needed
86      * to allow the process to function. An example of this would be in the MQTT connection creation workflow.
87      *
88      * The HostResolver will be set to have a maximum of 8 entries by default.
89      *
90      * The default HostResolver will be automatically managed and released when it's
91      * resources are being freed, not requiring any manual memory management.
92      * @return the static default host resolver
93      */
getOrCreateStaticDefault()94     static HostResolver getOrCreateStaticDefault() {
95         HostResolver resolver = null;
96         synchronized (HostResolver.class) {
97             if (staticDefaultResolver == null) {
98                 staticDefaultResolver = new HostResolver(EventLoopGroup.getOrCreateStaticDefault(), staticDefaultMaxEntries);
99             }
100 
101             resolver = staticDefaultResolver;
102         }
103 
104         return resolver;
105     }
106 
107     private static int staticDefaultMaxEntries = DEFAULT_MAX_ENTRIES;
108     private static HostResolver staticDefaultResolver;
109 
110     /*******************************************************************************
111      * native methods
112      ******************************************************************************/
hostResolverNew(long el_group, int max_entries)113     private static native long hostResolverNew(long el_group, int max_entries) throws CrtRuntimeException;
hostResolverRelease(long host_resolver)114     private static native void hostResolverRelease(long host_resolver);
115 }
116