php - Zend HttpClient more granular timeouts? -
it seems though zendframework2's various http client adapters allow connect , read timeouts in seconds range; enforced casting various timeout parameters (int) before setting them. in our organization, typically specify smaller connection , read timeouts production environments (we have soa in place).
however, socket-level functions in php back upwards (float) values timeouts back upwards sub-second timeouts, , there means back upwards sub-second connect timeouts using libcurl since php 5.2.3. (curlopt_connecttimeout_ms curl complied c-ares per how enable curl's asynchdns?).
i'm happy come in ticket zendframework back upwards smaller, more granular timeouts wanted first see if solved problem out here 'in wild'. has gotten zend\http\client\adapter\socket or \curl adapters back upwards sub-second connect , read timeouts?
i know 2 ways tackle problem. can utilize temporary prepare until zend framework 2 supports sub-second timeout on it's own.
override socket/curl classes
create module , inherit socket / curl classes , override connect() method back upwards sub-second timeout.
for curl class add together new alternative timeout_ms in configuration array, can still utilize old timeout option. my code shows relevant changes timeout section.
<?php utilize zend\http\client\adapter\curl; class curlms extends curl { public function connect($host, $port = 80, $secure = false) { ... // replace lines timeout next lines. if (isset($this->config['timeout_ms'])) { // set timeout milliseconds curl_setopt($this->curl, curlopt_connecttimeout_ms, $this->config['timeout_ms']); } elseif (isset($this->config['timeout'])) { // set timeout curl_setopt($this->curl, curlopt_connecttimeout, $this->config['timeout']); } ... } } for socket class situation little bit harder solve, because of different interfaces of stream_socket_client() , stream_socket_timeout(). alternative timeout float value. as above included relevant code parts.
<?php utilize zend\http\client\adapter\socket; class socketms extends socket { public function connectconnect($host, $port = 80, $secure = false) { ... // replace lines socket connect $this->socket = stream_socket_client( $host . ':' . $port, $errno, $errstr, (float) $this->config['timeout'], $flags, $context ); ... // replace lines stream timeout // fraction of float value , convert microseconds $fraction = ($this->config['timeout'] - floor($this->config['timeout'])) * 1000; // set stream timeout if (!stream_set_timeout($this->socket, (int) $this->config['timeout'], (int) $fraction) { throw new adapterexception\runtimeexception('unable set connection timeout'); } ... } } patch socket.php / curl.php files
write patch files want add together sub-second timeout , apply them zendframework 2 version. can automate process composer if utilize it. created patches against zendframework 2 version 2.3.3.
i wouldn't recommend approach, because has pitfalls.
the patch curl.php file following:
--- a/library/zend/http/client/adapter/curl.php +++ b/library/zend/http/client/adapter/curl.php @@ -200,7 +200,10 @@ class curl implements httpadapter, streaminterface curl_setopt($this->curl, curlopt_port, intval($port)); } - if (isset($this->config['timeout'])) { + if (isset($this->config['timeout_ms'])) { + // set timeout milliseconds + curl_setopt($this->curl, curlopt_connecttimeout_ms, $this->config['timeout_ms']); + } elseif (isset($this->config['timeout'])) { // set timeout curl_setopt($this->curl, curlopt_connecttimeout, $this->config['timeout']); } the patch socket.php file this:
--- a/library/zend/http/client/adapter/socket.php +++ b/library/zend/http/client/adapter/socket.php @@ -247,7 +247,7 @@ class socket implements httpadapter, streaminterface $host . ':' . $port, $errno, $errstr, - (int) $this->config['timeout'], + (float) $this->config['timeout'], $flags, $context ); @@ -268,7 +268,9 @@ class socket implements httpadapter, streaminterface } // set stream timeout - if (!stream_set_timeout($this->socket, (int) $this->config['timeout'])) { + // fraction of timeout , convert microseconds + $fraction = ($this->config['timeout'] - floor($this->config['timeout'])) * 1000; + if (!stream_set_timeout($this->socket, (int) $this->config['timeout'], (int) $fraction)) { throw new adapterexception\runtimeexception('unable set connection timeout'); } php curl zend-framework2
No comments:
Post a Comment