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 ExecutorService executor = scheduledExecutorService;//ForkJoinPool.commonPool();
final CacheResolver resolver = new CacheResolver( final CacheResolver resolver = new CacheResolver(
MinTtlResolver.of(minTtl, MapResolver.minTtl(minTtl,
new BlockingQueueResolver(packetQueueLength) new BlockingQueueResolver(packetQueueLength)
.startQueueProcessingResolvers(executor, queueProcessingResolvers) .startQueueProcessingResolvers(executor, queueProcessingResolvers)
), ),

View File

@ -4,34 +4,33 @@ import com.moparisthebest.dns.dto.Packet;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.function.Function;
public class MinTtlResolver implements Resolver { public class MapResolver implements Resolver {
private final int minTtl;
private final Resolver delegate; 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 // 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) { private MapResolver(final Resolver delegate, final Function<? super Packet, ? extends Packet> mapper) {
this.minTtl = minTtl;
this.delegate = delegate; this.delegate = delegate;
this.mapper = mapper;
} }
@Override @Override
public <E extends RequestResponse> CompletableFuture<E> resolveAsync(final E requestResponse, final Executor executor) { public <E extends RequestResponse> CompletableFuture<E> resolveAsync(final E requestResponse, final Executor executor) {
return delegate.resolveAsync(requestResponse, executor).thenApply(s -> { return delegate.resolveAsync(requestResponse, executor).thenApply(s -> {
s.getResponse().modTtls((ttl) -> Math.max(ttl, minTtl)); s.setResponse(mapper.apply(s.getResponse()));
//s.getResponse().modTtls((ttl) -> 30);
return s; return s;
}); });
} }
@Override @Override
public Packet resolve(final Packet request) throws Exception { public Packet resolve(final Packet request) throws Exception {
return delegate.resolve(request).modTtls((ttl) -> Math.max(ttl, minTtl)); return mapper.apply(delegate.resolve(request));
} }
} }