Skip to content

Commit

Permalink
Support a custom DnsAddressResolverGroup builder function (reactor#2745)
Browse files Browse the repository at this point in the history
Support a user provided function to build a DnsAddressResolverGroup
  • Loading branch information
samueldlightfoot authored Mar 27, 2023
1 parent 68dbe82 commit 4effedc
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/asciidoc/http-client.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ Default: 0.
| `completeOncePreferredResolved` | When this setting is enabled, the resolver notifies as soon as all queries for the preferred address type are complete.
When this setting is disabled, the resolver notifies when all possible address types are complete.
This configuration is applicable for `DnsNameResolver#resolveAll(String)`. By default, this setting is enabled.
| `dnsAddressResolverGroupProvider` | Sets a custom function to create a `DnsAddressResolverGroup` given a `DnsNameResolverBuilder`
| `disableOptionalRecord` | Disables the automatic inclusion of an optional record that tries to give a hint to the remote DNS server about
how much data the resolver can read per response. By default, this setting is enabled.
| `disableRecursionDesired` | Specifies whether this resolver has to send a DNS query with the recursion desired (RD) flag set.
Expand Down
1 change: 1 addition & 0 deletions docs/asciidoc/tcp-client.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,7 @@ The following listing shows the available configurations:
| `completeOncePreferredResolved` | When this setting is enabled, the resolver notifies as soon as all queries for the preferred address type are complete.
When this setting is disabled, the resolver notifies when all possible address types are complete.
This configuration is applicable for `DnsNameResolver#resolveAll(String)`. By default, this setting is enabled.
| `dnsAddressResolverGroupProvider` | Sets a custom function to create a `DnsAddressResolverGroup` given a `DnsNameResolverBuilder`
| `disableOptionalRecord` | Disables the automatic inclusion of an optional record that tries to give a hint to the remote DNS server about
how much data the resolver can read per response. By default, this setting is enabled.
| `disableRecursionDesired` | Specifies whether this resolver has to send a DNS query with the recursion desired (RD) flag set.
Expand Down
2 changes: 2 additions & 0 deletions reactor-netty-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ task japicmp(type: JapicmpTask) {
compatibilityChangeExcludes = [ "METHOD_NEW_DEFAULT" ]

methodExcludes = [
'reactor.netty.transport.NameResolverProvider#dnsAddressResolverGroupProvider()',
'reactor.netty.transport.NameResolverProvider$NameResolverSpec#dnsAddressResolverGroupProvider(java.util.function.Function)'
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.time.Duration;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.Supplier;

/**
Expand Down Expand Up @@ -122,6 +123,16 @@ public interface NameResolverSpec {
*/
NameResolverSpec disableRecursionDesired(boolean disable);

/**
* Sets the custom function to build the {@link DnsAddressResolverGroup} given a {@link DnsNameResolverBuilder}
*
* @param dnsAddressResolverGroupProvider the {@link DnsAddressResolverGroup} provider function
* @return {@code this}
* @since 1.1.6
*/
NameResolverSpec dnsAddressResolverGroupProvider(
Function<DnsNameResolverBuilder, DnsAddressResolverGroup> dnsAddressResolverGroupProvider);

/**
* Specifies a custom {@link HostsFileEntriesResolver} to be used for hosts file entries.
* Default to {@link DefaultHostsFileEntriesResolver}.
Expand Down Expand Up @@ -295,6 +306,17 @@ public Duration cacheNegativeTimeToLive() {
return cacheNegativeTimeToLive;
}

/**
* Returns the configured custom provider of {@link DnsAddressResolverGroup} or null
*
* @return the configured custom provider of {@link DnsAddressResolverGroup} or null
* @since 1.1.6
*/
@Nullable
public Function<DnsNameResolverBuilder, DnsAddressResolverGroup> dnsAddressResolverGroupProvider() {
return dnsAddressResolverGroupProvider;
}

/**
* Returns the configured custom {@link HostsFileEntriesResolver} to be used for hosts file entries or null.
*
Expand Down Expand Up @@ -452,6 +474,7 @@ public boolean equals(Object o) {
}
NameResolverProvider that = (NameResolverProvider) o;
return completeOncePreferredResolved == that.completeOncePreferredResolved &&
Objects.equals(dnsAddressResolverGroupProvider, that.dnsAddressResolverGroupProvider) &&
disableRecursionDesired == that.disableRecursionDesired &&
disableOptionalRecord == that.disableOptionalRecord &&
maxPayloadSize == that.maxPayloadSize &&
Expand Down Expand Up @@ -479,6 +502,7 @@ public int hashCode() {
result = 31 * result + Objects.hashCode(cacheMinTimeToLive);
result = 31 * result + Objects.hashCode(cacheNegativeTimeToLive);
result = 31 * result + Boolean.hashCode(completeOncePreferredResolved);
result = 31 * result + Objects.hashCode(dnsAddressResolverGroupProvider);
result = 31 * result + Boolean.hashCode(disableRecursionDesired);
result = 31 * result + Boolean.hashCode(disableOptionalRecord);
result = 31 * result + Objects.hashCode(loggingFactory);
Expand Down Expand Up @@ -547,6 +571,9 @@ public DnsAddressResolverGroup newNameResolverGroup(LoopResources defaultLoopRes
if (searchDomains != null) {
builder.searchDomains(searchDomains);
}
if (dnsAddressResolverGroupProvider != null) {
return dnsAddressResolverGroupProvider.apply(builder);
}
return roundRobinSelection ? new RoundRobinDnsAddressResolverGroup(builder) : new DnsAddressResolverGroup(builder);
}

Expand All @@ -570,11 +597,14 @@ public DnsAddressResolverGroup newNameResolverGroup(LoopResources defaultLoopRes
final boolean roundRobinSelection;
final Iterable<String> searchDomains;

final Function<DnsNameResolverBuilder, DnsAddressResolverGroup> dnsAddressResolverGroupProvider;

NameResolverProvider(Build build) {
this.cacheMaxTimeToLive = build.cacheMaxTimeToLive;
this.cacheMinTimeToLive = build.cacheMinTimeToLive;
this.cacheNegativeTimeToLive = build.cacheNegativeTimeToLive;
this.completeOncePreferredResolved = build.completeOncePreferredResolved;
this.dnsAddressResolverGroupProvider = build.dnsAddressResolverGroupProvider;
this.disableOptionalRecord = build.disableOptionalRecord;
this.disableRecursionDesired = build.disableRecursionDesired;
this.hostsFileEntriesResolver = build.hostsFileEntriesResolver;
Expand Down Expand Up @@ -622,6 +652,8 @@ static final class Build implements NameResolverSpec {
boolean roundRobinSelection;
Iterable<String> searchDomains;

Function<DnsNameResolverBuilder, DnsAddressResolverGroup> dnsAddressResolverGroupProvider;

@Override
public NameResolverSpec cacheMaxTimeToLive(Duration cacheMaxTimeToLive) {
this.cacheMaxTimeToLive = Objects.requireNonNull(cacheMaxTimeToLive);
Expand Down Expand Up @@ -658,6 +690,13 @@ public NameResolverSpec disableRecursionDesired(boolean disable) {
return this;
}

@Override
public NameResolverSpec dnsAddressResolverGroupProvider(
Function<DnsNameResolverBuilder, DnsAddressResolverGroup> dnsAddressResolverGroupProvider) {
this.dnsAddressResolverGroupProvider = Objects.requireNonNull(dnsAddressResolverGroupProvider);
return this;
}

@Override
public NameResolverSpec hostsFileEntriesResolver(HostsFileEntriesResolver hostsFileEntriesResolver) {
this.hostsFileEntriesResolver = Objects.requireNonNull(hostsFileEntriesResolver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@
import io.netty.handler.codec.dns.DnsRecord;
import io.netty.handler.logging.LogLevel;
import io.netty.resolver.ResolvedAddressTypes;
import io.netty.resolver.dns.DnsAddressResolverGroup;
import io.netty.resolver.dns.DnsCache;
import io.netty.resolver.dns.DnsCacheEntry;
import io.netty.resolver.dns.DnsNameResolverBuilder;
import io.netty.resolver.dns.DnsServerAddressStreamProviders;
import io.netty.resolver.dns.RoundRobinDnsAddressResolverGroup;
import io.netty.resolver.dns.macos.MacOSDnsServerAddressStreamProvider;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -36,6 +39,7 @@
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.function.Supplier;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -125,6 +129,15 @@ void completeOncePreferredResolved() {
assertThat(builder.build().isCompleteOncePreferredResolved()).isFalse();
}

@Test
void dnsAddressResolverGroupProvider() {
assertThat(builder.build().dnsAddressResolverGroupProvider()).isNull();

Function<DnsNameResolverBuilder, DnsAddressResolverGroup> provider = RoundRobinDnsAddressResolverGroup::new;
builder.dnsAddressResolverGroupProvider(provider);
assertThat(builder.build().dnsAddressResolverGroupProvider()).isEqualTo(provider);
}

@Test
void disableOptionalRecord() {
assertThat(builder.build().isDisableOptionalRecord()).isFalse();
Expand All @@ -143,10 +156,10 @@ void disableRecursionDesired() {

@Test
void hostsFileEntriesResolver() {
assertThat(builder.build().hostsFileEntriesResolver).isNull();
assertThat(builder.build().hostsFileEntriesResolver()).isNull();

builder.hostsFileEntriesResolver((inetHost, resolvedAddressTypes) -> null);
assertThat(builder.build().hostsFileEntriesResolver).isNotNull();
assertThat(builder.build().hostsFileEntriesResolver()).isNotNull();
}

@Test
Expand Down

0 comments on commit 4effedc

Please sign in to comment.