I am using a proprietary, 3rd party Drupal module that queries a 3rd party service via curl. The service has been a bit flakey lately, which is slowing my page loads a lot and when I've got a lot of traffic I am hitting max_connections.
The information that this extension queries is not vital, but it is important enough that I can't just remove the module. For the time being, I fixed it by patching the module to add a curl timeout to the request:
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
However, I don't want to leave the hack in place because it'll disappear on the next update and since the problem is intermittent it won't show up in testing.
Is there any way to set the timeout globally in a php.ini setting or in PHP via code (that I could drop in a custom module)?
Any help is appreciated,
Thanks
PHP's CURL uses the php.ini setting default_socket_timeout
. The default value is 60, the unit is seconds.
The author and maintainer states here:
When using libcurl, if CURLOPT_CONNECTTIMEOUT is not set, what is the default timeout value in seconds?
None at all.
A more complete answer can be found on another Stackoverflow question:
libcurl lists the following connection timeout specific settings:
- CURLOPT_FTP_RESPONSE_TIMEOUT: No default (indefinite)
- CURLOPT_TIMEOUT: No default (indefinite)
- CURLOPT_TIMEOUT_MS: No default (indefinite)
- CURLOPT_CONNECTTIMEOUT: Defaults to 300 seconds
- CURLOPT_CONNECTTIMEOUT_MS: No default
- CURLOPT_ACCEPTTIMEOUT_MS: Defaults to 60000 ms
The PHP source code does not override any of the above default settings: https://github.com/php/php-src/blob/master/ext/curl/interface.c. The only somewhat related parameter that the PHP bindings override is
CURLOPT_DNS_CACHE_TIMEOUT
, changing the default value from 60 seconds to 120 seconds: https://github.com/php/php-src/blob/a0e3ca1c986681d0136ce4550359ecee2826a80c/ext/curl/interface.c#L1926
So: No, php-curl does not honour the default_socket_timeout
setting. We even found several servers stuck with day-old (or even multiple month-old) curl-requests when investigating some customer problems.
And it seems there is no way to set the timeout globally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With