2

I'm opening multiple (75) streams through stream_socket_client() and then processing it with stream_select(). The first call of this method takes approx. 15 seconds and I have no idea why. Next calls are much faster - less than one or two seconds for whole method. I've tracked the issue to the foreach where connections are getting opened, which takes 14/15 seconds itself.

Code:

foreach ($tlds as $index => $server ) {

    $ip = gethostbyname($server);
    $con = @stream_socket_client($ip.':43',$errno, $errstr, 10, STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT);

    usleep(200);

    if (!$con) {
        $fails[] = $server;
    } else {
        $calls[$index] = $con;
        stream_set_blocking($calls[$index], false);
    }

    //get time here
}

Testing results:

╔════════╦══════════╦══════════╗
║ $index ║ 1st call ║ 2nd call ║
╠════════╬══════════╬══════════╣
║ 0      ║ 5s       ║ 0s       ║
╠════════╬══════════╬══════════╣
║ 10     ║ 6s       ║ 0s       ║
╠════════╬══════════╬══════════╣
║ 20     ║ 7s       ║ 0s       ║
╠════════╬══════════╬══════════╣
║ 30     ║ 9s       ║ 0s       ║
╠════════╬══════════╬══════════╣
║ 40     ║ 11s      ║ 0s       ║
╠════════╬══════════╬══════════╣
║ 50     ║ 12s      ║ 0s       ║
╠════════╬══════════╬══════════╣
║ 60     ║ 13s      ║ 0s       ║
╠════════╬══════════╬══════════╣
║ 70     ║ 14s      ║ 1s       ║
╠════════╬══════════╬══════════╣
║ end    ║ 14s      ║ 1s       ║
╚════════╩══════════╩══════════╝

I'm not experienced in socket programming at all so I'm greatful for any hints.

PHP 7.1, Apache/2.4.6 (CentOS)

Ask for any info you need - hope I'll be able to answer.

Note: Sometimes the second call still takes around 1/3 of the time the first call takes. But next calls are around 1 second or even less.

1 Answers1

1

I'm thinking that you have problem with DNS latency. You can try in linux console this in loop to detect it.

time nslookup $server

If you confirm that dns is slow, you can use NSCD service for local cache of records. In CENTOS : yum -y install nscd;systemctl enable nscd;systemctl start nscd; and after restart httpd service. Statistics from nscd: /usr/sbin/nscd -g

Pavel
  • 250
  • 3
  • 11
  • I've consulted and tried this with my server admin but he made some mistakes probably. This solution did not work. But based on it, I've saved IP addresses into DB and that worked. So I assume that this is the correct answer and my server admin just did not understand correctly what to do. Thank you. – Vojtěch Šalda Sep 18 '18 at 10:55