Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set global CURL timeout

Tags:

php

curl

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

like image 700
Matt Avatar asked Apr 13 '15 09:04

Matt


2 Answers

PHP's CURL uses the php.ini setting default_socket_timeout. The default value is 60, the unit is seconds.

like image 163
PKeidel Avatar answered Oct 23 '22 07:10

PKeidel


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.

like image 43
mist Avatar answered Oct 23 '22 06:10

mist