Issue #11443 has been updated by Patrick Robinson.


I retested this again today and could only replicate it on Ubuntu Lucid, not Ubuntu Trusty or Debian Jessie. 

----------------------------------------
Bug #11443: DNS name resolution takes twice as long as it should when primary name server is unavailable
https://bugs.ruby-lang.org/issues/11443#change-56893

* Author: Patrick Robinson
* Status: Open
* Priority: Normal
* Assignee: 
* ruby -v: ruby 2.3.0dev (2015-08-13) [x86_64-linux]
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN
----------------------------------------
On linux /etc/resolv.conf can be configured to set the timeout of a nameserver before trying the next one (defaults to 5 seconds). So in the event of a failed name server we expect the time taken to resolve a hostname will be timeout + query time. However testing shows it takes (2 * timeout) + query time. Example resolv.conf to replicate the problem of a failed name server:

~~~
nameserver 1.1.1.1
nameserver 8.8.8.8
nameserver 8.8.4.4
~~~

Example ruby code to test:

```
require 'net/protocol'
require 'uri'
require 'benchmark'
uri = URI.parse("http://google.com")
Benchmark.measure { TCPSocket.new(uri.host, uri.port) }.real
```

Example benchmark with a above resolv.conf:

```
irb(main):005:0> Benchmark.measure { TCPSocket.new(uri.host, uri.port) }.real
=> 10.593100978
```

Example benchmark with timeout set to 3 in resolv.conf (options timeout:3):

```
irb(main):005:0> Benchmark.measure { TCPSocket.new(uri.host, uri.port) }.real
=> 6.262138267
```

and without the invalid nameserver:

```
irb(main):005:0> Benchmark.measure { TCPSocket.new(uri.host, uri.port) }.real
=> 0.243385728
```

Example C code that proves the resolver is behaving as expected:

```
#include <stdio.h>
#include <netdb.h>

int main(int argc, char *argv[])
{
    struct hostent *hstnm;
    if (argc != 2) {
        fprintf(stderr, "usage: %s hostname\n", argv[0]);
        return 1;
    }
    hstnm = gethostbyname (argv[1]);
    if (!hstnm)
        return 1;
    printf ("Name: %s\n", hstnm->h_name);
    return 0;
}
```

Benchmark of C code with failed nameserver:

~~~
$ time ./gethostbyname google.com
Name: google.com

real	0m5.189s
user	0m0.000s
sys	0m0.000s
~~~

Benchmark of C code without failed nameserver:

~~~
$ time ./gethostbyname google.com
Name: google.com

real	0m0.040s
user	0m0.000s
sys	0m0.000s
~~~



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>