Refactor MinTtlResolver out of CacheResolver
This commit is contained in:
parent
c06d3311e8
commit
52f3773408
@ -2,10 +2,7 @@ package com.moparisthebest.dns;
|
||||
|
||||
import com.moparisthebest.dns.listen.Listener;
|
||||
import com.moparisthebest.dns.net.ParsedUrl;
|
||||
import com.moparisthebest.dns.resolve.BlockingQueueResolver;
|
||||
import com.moparisthebest.dns.resolve.CacheResolver;
|
||||
import com.moparisthebest.dns.resolve.QueueProcessingResolver;
|
||||
import com.moparisthebest.dns.resolve.Resolver;
|
||||
import com.moparisthebest.dns.resolve.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
@ -66,8 +63,11 @@ public class DnsProxy {
|
||||
final ExecutorService executor = scheduledExecutorService;//ForkJoinPool.commonPool();
|
||||
|
||||
final CacheResolver resolver = new CacheResolver(
|
||||
new BlockingQueueResolver(packetQueueLength).startQueueProcessingResolvers(executor, queueProcessingResolvers),
|
||||
minTtl, staleResponseTtl, staleResponseTimeout,
|
||||
MinTtlResolver.of(minTtl,
|
||||
new BlockingQueueResolver(packetQueueLength)
|
||||
.startQueueProcessingResolvers(executor, queueProcessingResolvers)
|
||||
),
|
||||
staleResponseTtl, staleResponseTimeout,
|
||||
scheduledExecutorService, cacheFile, cacheDelayMinutes)
|
||||
;
|
||||
|
||||
|
@ -14,7 +14,7 @@ import static com.moparisthebest.dns.Util.supplyAsyncOnTimeOut;
|
||||
|
||||
public class CacheResolver implements Resolver, AutoCloseable {
|
||||
|
||||
private final int minTtl, staleResponseTtl;
|
||||
private final int staleResponseTtl;
|
||||
private final long staleResponseTimeout;
|
||||
|
||||
private final Resolver delegate;
|
||||
@ -23,10 +23,9 @@ public class CacheResolver implements Resolver, AutoCloseable {
|
||||
|
||||
private final ConcurrentMap<String, CachedPacket> cache = new ConcurrentHashMap<>();
|
||||
|
||||
public CacheResolver(final Resolver delegate, final int minTtl, final int staleResponseTtl, final long staleResponseTimeout, final ScheduledExecutorService scheduledExecutorService,
|
||||
public CacheResolver(final Resolver delegate, final int staleResponseTtl, final long staleResponseTimeout, final ScheduledExecutorService scheduledExecutorService,
|
||||
final String cacheFile, final long cacheDelayMinutes) throws IOException {
|
||||
this.delegate = delegate;
|
||||
this.minTtl = minTtl;
|
||||
this.staleResponseTtl = staleResponseTtl;
|
||||
this.staleResponseTimeout = staleResponseTimeout;
|
||||
this.scheduledExecutorService = scheduledExecutorService;
|
||||
@ -129,14 +128,7 @@ public class CacheResolver implements Resolver, AutoCloseable {
|
||||
}
|
||||
|
||||
private <E extends RequestResponse> CompletableFuture<E> requestAndCache(final E requestResponse, final Executor executor) {
|
||||
CompletableFuture<E> request = delegate.resolveAsync(requestResponse, executor);
|
||||
if(minTtl > 0) {
|
||||
request = request.thenApply(s -> {
|
||||
s.getResponse().modTtls((ttl) -> Math.max(ttl, minTtl));
|
||||
//s.getResponse().modTtls((ttl) -> 30);
|
||||
return s;
|
||||
});
|
||||
}
|
||||
final CompletableFuture<E> request = delegate.resolveAsync(requestResponse, executor);
|
||||
request.thenAcceptAsync(s -> {
|
||||
final Packet response = s.getResponse().copy(); // todo: do we need to copy?
|
||||
final long currentTime = currentTimeSeconds();
|
||||
|
@ -0,0 +1,37 @@
|
||||
package com.moparisthebest.dns.resolve;
|
||||
|
||||
import com.moparisthebest.dns.dto.Packet;
|
||||
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
public class MinTtlResolver implements Resolver {
|
||||
|
||||
private final int minTtl;
|
||||
|
||||
private final Resolver delegate;
|
||||
|
||||
public static Resolver of(final int minTtl, final Resolver delegate) {
|
||||
// anything less than 1 just don't wrap
|
||||
return minTtl < 1 ? delegate : new MinTtlResolver(minTtl, delegate);
|
||||
}
|
||||
|
||||
private MinTtlResolver(final int minTtl, final Resolver delegate) {
|
||||
this.minTtl = minTtl;
|
||||
this.delegate = delegate;
|
||||
}
|
||||
|
||||
@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);
|
||||
return s;
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Packet resolve(final Packet request) throws Exception {
|
||||
return delegate.resolve(request).modTtls((ttl) -> Math.max(ttl, minTtl));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user