php gethostbyaddr awfully slow

Today I had the problem, that a php script that needs to resolve IP Adresses was awfully slow (5 seconds for every call).

<?
$beg = microtime(true);
print gethostbyaddr('74.125.232.159')."\n";
print "diff: ".(microtime(true) - $beg)."\n";

print gethostbyaddr('172.28.128.199')."\
print "diff: ".(microtime(true) - $beg)."\n";

I didn’t find the cause for this, until I strace’d the php process on the commandline:

strace php x.php
[...]
open("/lib/libnss_mdns4.so.2", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0644, st_size=8824, ...}) = 0
[...]
 connect(3, {sa_family=AF_FILE, path="/var/run/avahi-daemon/socket"}, 110) = 0
fcntl64(3, F_GETFL) = 0x2 (flags O_RDWR)
fstat64(3, {st_mode=S_IFSOCK|0777, st_size=0, ...}) = 0
[...]
write(3, "RESOLVE-ADDRESS 172.28.128.199\n", 31) = 31
read(3,
[...]

 

PHP tries to resolve the IP address using the nss_mdns library and avahi. In my setup this makes no sense and the avahid replies to the request with a timeout; after 5 seconds!

After removing avahi-daemon and libnss-mdns the scripts work now without delay!

Leave a Reply

Your email address will not be published. Required fields are marked *