13

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

Matt
  • 1,287
  • 2
  • 11
  • 25

2 Answers2

17

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

PKeidel
  • 2,559
  • 1
  • 21
  • 29
  • AFAIK this is NOT true: "cURL has an indefinite timeout by default and does not obey the default_socket_timeout INI setting. This means that you have to configure the timeout in every cURL call in your code." https://tideways.com/profiler/blog/using-http-client-timeouts-in-php – drAlberT Sep 10 '20 at 14:38
  • https://www.php.net/manual/en/filesystem.configuration.php In the table the default value for "default_socket_timeout" is shown as "60". So no need for a down vote – PKeidel Sep 15 '20 at 16:14
  • 1
    Curl does NOT honor that value – drAlberT Sep 15 '20 at 16:28
  • Where is that documented? – PKeidel Sep 16 '20 at 08:28
  • 1
    I updated this in my php.ini file and the cUrl timeout changed. Ignore @drAlberT 's comments – JDandChips Feb 09 '22 at 12:02
  • Maybe you could be right, but on what php version? Are you sure your test is valid on 8.x and 7.x? – drAlberT Feb 11 '22 at 13:05
  • @drAlberT I tried this answer on php 8 and it worked. curl changed its timeout. – Akmal Feb 16 '23 at 16:06
  • Guys probably I got that behavior on an old PHP version .. I don't remember now. So, yep .. ignore the comment and go ahead ;) – drAlberT Jun 14 '23 at 09:03
2

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.

mist
  • 21
  • 2