Refactor MinTtlResolver to MapResolver

This commit is contained in:
Travis Burtrum 2019-04-13 17:03:34 -04:00
parent 52f3773408
commit 74e9b546bc
2 changed files with 10 additions and 11 deletions

View File

@ -63,7 +63,7 @@ public class DnsProxy {
final ExecutorService executor = scheduledExecutorService;//ForkJoinPool.commonPool();
final CacheResolver resolver = new CacheResolver(
MinTtlResolver.of(minTtl,
MapResolver.minTtl(minTtl,
new BlockingQueueResolver(packetQueueLength)
.startQueueProcessingResolvers(executor, queueProcessingResolvers)
),

View File

@ -4,34 +4,33 @@ import com.moparisthebest.dns.dto.Packet;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Function;
public class MinTtlResolver implements Resolver {
private final int minTtl;
public class MapResolver implements Resolver {
private final Resolver delegate;
private final Function<? super Packet, ? extends Packet> mapper;
public static Resolver of(final int minTtl, final Resolver delegate) {
public static Resolver minTtl(final int minTtl, final Resolver delegate) {
// anything less than 1 just don't wrap
return minTtl < 1 ? delegate : new MinTtlResolver(minTtl, delegate);
return minTtl < 1 ? delegate : new MapResolver(delegate, (s) -> s.modTtls((ttl) -> Math.max(ttl, minTtl)));
}
private MinTtlResolver(final int minTtl, final Resolver delegate) {
this.minTtl = minTtl;
private MapResolver(final Resolver delegate, final Function<? super Packet, ? extends Packet> mapper) {
this.delegate = delegate;
this.mapper = mapper;
}
@Override
public <E extends RequestResponse> CompletableFuture<E> resolveAsync(final E requestResponse, final Executor executor) {
return delegate.resolveAsync(requestResponse, executor).thenApply(s -> {
s.getResponse().modTtls((ttl) -> Math.max(ttl, minTtl));
//s.getResponse().modTtls((ttl) -> 30);
s.setResponse(mapper.apply(s.getResponse()));
return s;
});
}
@Override
public Packet resolve(final Packet request) throws Exception {
return delegate.resolve(request).modTtls((ttl) -> Math.max(ttl, minTtl));
return mapper.apply(delegate.resolve(request));
}
}