Improve logging in DnsServerAddressStreamProviders
(#10848)
Motivation: `DnsServerAddressStreamProviders` tries to load `MacOSDnsServerAddressStreamProvider` on macOS. However, it doesn't warn users when `MacOSDnsServerAddressStreamProvider` is not awailable, which may cause incorrect results for DNS resolutions. Modifications: - Log at warn level if `MacOSDnsServerAddressStreamProvider` is not found on macOS; - Log at debug level when `MacOSDnsServerAddressStreamProvider` is loaded and available; Result: macOS users are notified when `MacOSDnsServerAddressStreamProvider` is not available.
This commit is contained in:
parent
6df3adfb9b
commit
643b8471e2
@ -23,7 +23,6 @@ import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.MethodType;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@ -37,6 +36,8 @@ public final class DnsServerAddressStreamProviders {
|
||||
private static final InternalLogger LOGGER =
|
||||
InternalLoggerFactory.getInstance(DnsServerAddressStreamProviders.class);
|
||||
private static final MethodHandle STREAM_PROVIDER_CONSTRUCTOR_HANDLE;
|
||||
private static final String MACOS_PROVIDER_CLASS_NAME =
|
||||
"io.netty.resolver.dns.macos.MacOSDnsServerAddressStreamProvider";
|
||||
|
||||
static {
|
||||
MethodHandle constructorHandle = null;
|
||||
@ -47,7 +48,7 @@ public final class DnsServerAddressStreamProviders {
|
||||
Object maybeProvider = AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
|
||||
try {
|
||||
return Class.forName(
|
||||
"io.netty.resolver.dns.macos.MacOSDnsServerAddressStreamProvider",
|
||||
MACOS_PROVIDER_CLASS_NAME,
|
||||
true,
|
||||
DnsServerAddressStreamProviders.class.getClassLoader());
|
||||
} catch (Throwable cause) {
|
||||
@ -58,18 +59,17 @@ public final class DnsServerAddressStreamProviders {
|
||||
@SuppressWarnings("unchecked")
|
||||
Class<? extends DnsServerAddressStreamProvider> providerClass =
|
||||
(Class<? extends DnsServerAddressStreamProvider>) maybeProvider;
|
||||
|
||||
MethodHandles.Lookup lookup = MethodHandles.lookup();
|
||||
Method method = providerClass.getMethod("ensureAvailability");
|
||||
method.invoke(null);
|
||||
constructorHandle = lookup.findConstructor(providerClass, MethodType.methodType(void.class));
|
||||
constructorHandle.invoke();
|
||||
} else if (!(maybeProvider instanceof ClassNotFoundException)) {
|
||||
constructorHandle.invoke(); // ctor ensures availability
|
||||
LOGGER.debug("{}: available", MACOS_PROVIDER_CLASS_NAME);
|
||||
} else {
|
||||
throw (Throwable) maybeProvider;
|
||||
}
|
||||
} catch (Throwable cause) {
|
||||
LOGGER.debug(
|
||||
"Unable to use MacOSDnsServerAddressStreamProvider, fallback to system defaults", cause);
|
||||
LOGGER.warn(
|
||||
"Unable to load {}, fallback to system defaults. {}", MACOS_PROVIDER_CLASS_NAME,
|
||||
"This may result in incorrect DNS resolutions on MacOS.", cause);
|
||||
constructorHandle = null;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user