PHP stream_socket_client ignoring timeout

2019-08-03 09:04发布

问题:

I'm using zend_mail from zend-framework2 in my project to send some amount of emails in a loop, but sometimes it takes more than usual to send an email. After doing some research, i found that the delay occurs on the stream_socket_client function. I tried to set this function's timeout to acceptable value, but it seems to ignore that setting. Also tried to use STREAM_CLIENT_PERSISTENT to limit the number of opened sockets but with no luck.

Average stream_socket_client times are about 0.03 seconds and occasionally it takes from 5 seconds up to even 40 seconds. Every value above 0.5 seconds is unacceptable for me. I'm out of ideas what can cause that issue.

Current setup:

$start = microtime(true);  

$this->socket = @stream_socket_client($remote, $errorNum, $errorStr, self::TIMEOUT_CONNECTION, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT);

echo 'Stream socket: '.(microtime(true) -  $start);