Re-factor and document Resolver

This commit is contained in:
Travis Burtrum 2019-03-14 02:04:25 -04:00
parent 964521f0cd
commit f52f1de8ad
6 changed files with 44 additions and 26 deletions

View File

@ -43,7 +43,7 @@ public class TcpAsync implements Listener {
bufChan.setRequest(new Packet(bufChan.buf));
//debugPacket(bufChan.getRequest().getBuf());
resolver.resolveAsync(bufChan).whenCompleteAsync((bc, t) -> {
resolver.resolveAsync(bufChan, executor).whenCompleteAsync((bc, t) -> {
//System.out.println("got completed!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
if(t != null) {
t.printStackTrace();

View File

@ -42,7 +42,7 @@ public class UdpSync implements Listener {
//System.out.println(requestResponse);
//debugPacket(requestResponse.getRequest().getBuf());
resolver.resolveAsync(requestResponse).whenCompleteAsync((urr, t) -> {
resolver.resolveAsync(requestResponse, executor).whenCompleteAsync((urr, t) -> {
if(t != null) {
t.printStackTrace();
return;

View File

@ -105,7 +105,7 @@ public class CacheResolver implements Resolver, AutoCloseable {
}
@Override
public <E extends RequestResponse> CompletableFuture<E> resolveAsync(final E requestResponse) {
public <E extends RequestResponse> CompletableFuture<E> resolveAsync(final E requestResponse, final Executor executor) {
final String key = calcRequestPacketKey(requestResponse.getRequest());
//System.out.println("requestPacketKey: " + key);
final CachedPacket response = cache.get(key);
@ -115,7 +115,7 @@ public class CacheResolver implements Resolver, AutoCloseable {
if (response.isExpired(currentTime)) {
//System.out.println("cachedPacket isExpired!");
requestResponse.setRequestPacketKey(key);
final CompletableFuture<E> request = requestAndCache(requestResponse);
final CompletableFuture<E> request = requestAndCache(requestResponse, executor);
final CompletableFuture<E> stale = supplyAsyncOnTimeOut(scheduledExecutorService, staleResponseTimeout, TimeUnit.MILLISECONDS, () -> {
requestResponse.setResponse(response.getStaleResponse().setId(requestResponse.getRequest().getId()));
return requestResponse;
@ -129,7 +129,7 @@ public class CacheResolver implements Resolver, AutoCloseable {
}
//System.out.println("no cachedPacket, querying upstream!");
requestResponse.setRequestPacketKey(key);
return requestAndCache(requestResponse);
return requestAndCache(requestResponse, executor);
/*
// todo: should not have to do this, some upstreams seem to eat stuff though, figure that out, I think readTimeout fixed this
final CompletableFuture<E> request = requestAndCache(requestResponse);
@ -142,7 +142,7 @@ public class CacheResolver implements Resolver, AutoCloseable {
//boolean first = true;
private <E extends RequestResponse> CompletableFuture<E> requestAndCache(final E requestResponse) {
private <E extends RequestResponse> CompletableFuture<E> requestAndCache(final E requestResponse, final Executor executor) {
CompletableFuture<E> request = new CompletableFuture<>();
requestResponse.setCompletableFuture(request);
//if(first) {

View File

@ -3,6 +3,7 @@ package com.moparisthebest.dns.resolve;
import com.moparisthebest.dns.dto.Packet;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
public class DelegatingQueueProcessingResolver extends AbstractQueueProcessingResolver {
@ -14,8 +15,8 @@ public class DelegatingQueueProcessingResolver extends AbstractQueueProcessingRe
}
@Override
public <E extends RequestResponse> CompletableFuture<E> resolveAsync(final E requestResponse) {
return delegate.resolveAsync(requestResponse);
public <E extends RequestResponse> CompletableFuture<E> resolveAsync(final E requestResponse, final Executor executor) {
return delegate.resolveAsync(requestResponse, executor);
}
@Override

View File

@ -5,27 +5,44 @@ import com.moparisthebest.dns.net.ParsedUrl;
import java.util.ServiceLoader;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
/**
* Implementations need to implement at least 1 of the resolve functions, either resolveAsync or resolve, the default
* implementations simply call the other one ending in a stack overflow.
*
* Ideally, implementations provide optimized versions of both the in-line and async call.
*/
public interface Resolver {
default <E extends RequestResponse> CompletableFuture<E> resolveAsync(E requestResponse) {
try {
requestResponse.setResponse(resolve(requestResponse.getRequest()));
return CompletableFuture.completedFuture(requestResponse);
} catch (Exception e) {
final CompletableFuture<E> ret = new CompletableFuture<>();
ret.completeExceptionally(e);
return ret;
}
/*
return CompletableFuture.supplyAsync(() -> {
requestResponse.setResponse(resolve(requestResponse.getRequest()));
return requestResponse;
}, executor);
*/
/**
* This must return immediately and resolve the DNS query in the background, using the given executor
* @param requestResponse
* @param executor
* @param <E>
* @return
*/
default <E extends RequestResponse> CompletableFuture<E> resolveAsync(final E requestResponse, final Executor executor) {
final CompletableFuture<E> ret = new CompletableFuture<>();
executor.execute(() -> {
try {
requestResponse.setResponse(resolve(requestResponse.getRequest()));
ret.complete(requestResponse);
} catch (Throwable e) {
ret.completeExceptionally(e);
}
});
return ret;
}
default Packet resolve(Packet request) throws Exception {
return resolveAsync(new BaseRequestResponse(request)).get().getResponse();
/**
* This must block on resolving the given query
* @param request
* @return
* @throws Exception
*/
default Packet resolve(final Packet request) throws Exception {
return resolveAsync(new BaseRequestResponse(request), Runnable::run).get().getResponse();
}
ServiceLoader<Services> services = ServiceLoader.load(Services.class);

View File

@ -51,7 +51,7 @@ public class XmppListener implements Listener {
//System.out.println("good request: " + req);
final XmppRequestResponse requestResponse = new XmppRequestResponse(req.getFrom(), new Packet(request));
resolver.resolveAsync(requestResponse).whenCompleteAsync((urr, t) -> {
resolver.resolveAsync(requestResponse, executor).whenCompleteAsync((urr, t) -> {
if (t != null) {
t.printStackTrace();
return;