diff --git a/.classpath b/.classpath index c6a047462..31db0ac5a 100644 --- a/.classpath +++ b/.classpath @@ -48,6 +48,9 @@ + + + diff --git a/source/net/yacy/cora/protocol/Domains.java b/source/net/yacy/cora/protocol/Domains.java index f32278361..23f36aba1 100644 --- a/source/net/yacy/cora/protocol/Domains.java +++ b/source/net/yacy/cora/protocol/Domains.java @@ -43,7 +43,10 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; +import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; @@ -54,6 +57,10 @@ import net.yacy.cora.storage.KeyList; import net.yacy.kelondro.logging.Log; import net.yacy.kelondro.util.MemoryControl; +import com.google.common.net.InetAddresses; +import com.google.common.util.concurrent.SimpleTimeLimiter; +import com.google.common.util.concurrent.TimeLimiter; + public class Domains { @@ -553,14 +560,16 @@ public class Domains { cacheHit_Insert++; } + final private static TimeLimiter timeLimiter = new SimpleTimeLimiter(Executors.newFixedThreadPool(20)); + /** * resolve a host address using a local DNS cache and a DNS lookup if necessary * @param host * @return the hosts InetAddress or null if the address cannot be resolved */ - public static InetAddress dnsResolve(String host) { - if ((host == null) || (host.length() == 0)) return null; - host = host.toLowerCase().trim(); + public static InetAddress dnsResolve(final String host0) { + if ((host0 == null) || (host0.length() == 0)) return null; + final String host = host0.toLowerCase().trim(); // try to simply parse the address InetAddress ip = parseInetAddress(host); if (ip != null) return ip; @@ -615,8 +624,23 @@ public class Domains { try { //final long t = System.currentTimeMillis(); Thread.currentThread().setName("Domains: DNS resolve of '" + host + "'"); // thread dump show which host is resolved - ip = TimeoutRequest.getByName(host, 1000); // this makes the DNS request to backbone - //ip = InetAddress.getByName(host); // this makes the DNS request to backbone + if (InetAddresses.isInetAddress(host)) { + try { + ip = InetAddresses.forString(host); + Log.logInfo("Domains", "using guava for host resolution:" + host); + } catch (IllegalArgumentException e) { + ip = null; + } + } + if (ip == null) { + ip = timeLimiter.callWithTimeout(new Callable() { + @Override + public InetAddress call() throws Exception { + return InetAddress.getByName(host); + } + }, 1000L, TimeUnit.MILLISECONDS, false); + //ip = TimeoutRequest.getByName(host, 1000); // this makes the DNS request to backbone + } //.out.println("DNSLOOKUP-*LOOKUP* " + host + ", time = " + (System.currentTimeMillis() - t) + "ms"); } catch (final Throwable e) { // add new entries diff --git a/source/net/yacy/cora/protocol/TimeoutRequest.java b/source/net/yacy/cora/protocol/TimeoutRequest.java index fed518a13..071759c19 100644 --- a/source/net/yacy/cora/protocol/TimeoutRequest.java +++ b/source/net/yacy/cora/protocol/TimeoutRequest.java @@ -70,6 +70,7 @@ public class TimeoutRequest { try { final Future taskFuture = service.submit(this.call); final Runnable t = new Runnable() { + @Override public void run() { taskFuture.cancel(true); } }; service.execute(t); @@ -109,6 +110,7 @@ public class TimeoutRequest { */ public static boolean ping(final String host, final int port, final int timeout) throws ExecutionException { return new TimeoutRequest(new Callable() { + @Override public Boolean call() { //long time = System.currentTimeMillis(); try { @@ -133,25 +135,6 @@ public class TimeoutRequest { }).call(timeout).booleanValue(); } - /** - * do a DNS lookup within a given time - * @param host - * @param timeout - * @return the InetAddress for a given domain name - * @throws ExecutionException - */ - public static InetAddress getByName(final String host, final long timeout) throws ExecutionException { - return new TimeoutRequest(new Callable() { - public InetAddress call() { - try { - return InetAddress.getByName(host); - } catch (final UnknownHostException e) { - return null; - } - } - }).call(timeout); - } - /** * perform a reverse domain name lookup for a given InetAddress within a given timeout * @param i @@ -161,6 +144,7 @@ public class TimeoutRequest { */ public static String getHostName(final InetAddress i, final long timeout) throws ExecutionException { return new TimeoutRequest(new Callable() { + @Override public String call() { return i.getHostName(); } }).call(timeout); } @@ -175,6 +159,7 @@ public class TimeoutRequest { public static boolean exists(final SmbFile file, final long timeout) throws IOException { try { return new TimeoutRequest(new Callable() { + @Override public Boolean call() { try { return file.exists(); } catch (final SmbException e) { @@ -196,6 +181,7 @@ public class TimeoutRequest { public static boolean canRead(final SmbFile file, final long timeout) throws IOException { try { return new TimeoutRequest(new Callable() { + @Override public Boolean call() { try { return file.canRead(); } catch (final SmbException e) { @@ -217,6 +203,7 @@ public class TimeoutRequest { public static boolean canWrite(final SmbFile file, final long timeout) throws IOException { try { return new TimeoutRequest(new Callable() { + @Override public Boolean call() { try { return file.canWrite(); } catch (final SmbException e) { @@ -238,6 +225,7 @@ public class TimeoutRequest { public static boolean isHidden(final SmbFile file, final long timeout) throws IOException { try { return new TimeoutRequest(new Callable() { + @Override public Boolean call() { try { return file.isHidden(); } catch (final SmbException e) { @@ -259,6 +247,7 @@ public class TimeoutRequest { public static boolean isDirectory(final SmbFile file, final long timeout) throws IOException { try { return new TimeoutRequest(new Callable() { + @Override public Boolean call() { try { return file.isDirectory(); } catch (final SmbException e) { @@ -280,6 +269,7 @@ public class TimeoutRequest { public static long length(final SmbFile file, final long timeout) throws IOException { try { return new TimeoutRequest(new Callable() { + @Override public Long call() { try { return file.length(); } catch (final SmbException e) { @@ -301,6 +291,7 @@ public class TimeoutRequest { public static long lastModified(final SmbFile file, final long timeout) throws IOException { try { return new TimeoutRequest(new Callable() { + @Override public Long call() { try { return file.lastModified(); } catch (final SmbException e) { @@ -322,6 +313,7 @@ public class TimeoutRequest { public static String[] list(final SmbFile file, final long timeout) throws IOException { try { return new TimeoutRequest(new Callable() { + @Override public String[] call() { try { return file.list(); } catch (final SmbException e) { @@ -334,11 +326,4 @@ public class TimeoutRequest { } } - public static void main(final String[] args) { - try { - System.out.println(getByName("yacy.net", 100)); - } catch (final ExecutionException e) { - e.printStackTrace(); - } - } }