From c7d8712ab5dfec3bd0b549e266327504feb8595a Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Sat, 13 Apr 2019 17:08:56 -0400 Subject: [PATCH] Add RandomUpstreamResolver --- .../dns/resolve/RandomUpstreamResolver.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 jDnsProxy/src/main/java/com/moparisthebest/dns/resolve/RandomUpstreamResolver.java diff --git a/jDnsProxy/src/main/java/com/moparisthebest/dns/resolve/RandomUpstreamResolver.java b/jDnsProxy/src/main/java/com/moparisthebest/dns/resolve/RandomUpstreamResolver.java new file mode 100644 index 0000000..5d3c432 --- /dev/null +++ b/jDnsProxy/src/main/java/com/moparisthebest/dns/resolve/RandomUpstreamResolver.java @@ -0,0 +1,31 @@ +package com.moparisthebest.dns.resolve; + +import com.moparisthebest.dns.dto.Packet; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; +import java.util.concurrent.ThreadLocalRandom; + +public class RandomUpstreamResolver implements Resolver { + + private final Resolver[] delegates; + + public RandomUpstreamResolver(final Resolver... delegates) { + this.delegates = delegates; + } + + public Resolver random() { + return delegates[ThreadLocalRandom.current().nextInt(delegates.length)]; + } + + @Override + public CompletableFuture resolveAsync(final E requestResponse, final Executor executor) { + return random().resolveAsync(requestResponse, executor); + } + + @Override + public Packet resolve(final Packet request) throws Exception { + return random().resolve(request); + } + +}