看场电影

4年前PHP Curl毫秒超时问题现在怎么样了?

2018-06-03  本文已影响2698人  小武说

江湖惯例,先上结论:

鸟哥在14年抛出的问题

问题起源:Laruence:21 Jan 14 Curl的毫秒超时的一个”Bug”

问题描述:

libcurl新版本支持毫秒级超时,升级PHP后,设置毫秒超时直接返回超时错误(Timeout reached 28

解决方案:

PHP文档中关于TIMEOUT_MS说明

看下PHP文档里的说明:http://php.net/manual/zh/function.curl-setopt.php

2018年的今天这个问题如何

结论:

int Curl_resolv_timeout(struct connectdata *conn,
                        const char *hostname,
                        int port,
                        struct Curl_dns_entry **entry,
                        time_t timeoutms)
{
...
...
#ifdef USE_ALARM_TIMEOUT
  if(data->set.no_signal)  // 设置no_signal,则timeout为0,忽略超时
    /* Ignore the timeout when signals are disabled */
    timeout = 0;
  else
    timeout = (timeoutms > LONG_MAX) ? LONG_MAX : (long)timeoutms;

  if(!timeout)  // timeout=0,则无超时解析
    /* USE_ALARM_TIMEOUT defined, but no timeout actually requested */
    return Curl_resolv(conn, hostname, port, entry);

  if(timeout < 1000) { // 如果timeout < 1000,则直接返回CURLRESOLV_TIMEDOUT超时,增加了日志输出
    /* The alarm() function only provides integer second resolution, so if
       we want to wait less than one second we must bail out already now. */
    failf(data,
        "remaining timeout of %ld too small to resolve via SIGALRM method",
        timeout);
    return CURLRESOLV_TIMEDOUT;
  }

Guzzle Http中是否有规避策略

$timeoutRequiresNoSignal = false;
if (isset($options['timeout'])) {
    $timeoutRequiresNoSignal |= $options['timeout'] < 1;  // 超时时间小于1,则设置NoSignal=1
    $conf[CURLOPT_TIMEOUT_MS] = $options['timeout'] * 1000;
}
// CURL default value is CURL_IPRESOLVE_WHATEVER
if (isset($options['force_ip_resolve'])) {
    if ('v4' === $options['force_ip_resolve']) {
        $conf[CURLOPT_IPRESOLVE] = CURL_IPRESOLVE_V4;
    } else if ('v6' === $options['force_ip_resolve']) {
        $conf[CURLOPT_IPRESOLVE] = CURL_IPRESOLVE_V6;
    }
}
if (isset($options['connect_timeout'])) {
    $timeoutRequiresNoSignal |= $options['connect_timeout'] < 1; // 超时时间小于1,则设置NoSignal=1
    $conf[CURLOPT_CONNECTTIMEOUT_MS] = $options['connect_timeout'] * 1000;
}
if ($timeoutRequiresNoSignal && strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
    $conf[CURLOPT_NOSIGNAL] = true;
}
if (isset($options['timeout'])) {
    $conf[CURLOPT_TIMEOUT_MS] = $options['timeout'] * 1000;
}
if (isset($options['connect_timeout'])) {
    $conf[CURLOPT_CONNECTTIMEOUT_MS] = $options['connect_timeout'] * 1000;
}

CentOS 7中curl

CentOS 7中缺省携带的curl编译已配置threaded-resolver,也就是说CentOS 7 + PHP 7组合使用毫秒超时很安全。

补充一点curl支持异步DNS的方式有两种:c-ares和threaded-resolver(centos 7中缺省启用threaded-resolver)

附带两个查看本机curl编译配置的命令:

附录不同CentOS下curl缺省编译配置:

>curl --version

curl 7.37.0 (x86_64-unknown-linux-gnu) libcurl/7.37.0 OpenSSL/1.0.1e zlib/1.2.3
Protocols: dict file ftp ftps gopher http https imap imaps pop3 pop3s rtsp smtp smtps telnet tftp 
Features: Largefile NTLM NTLM_WB SSL libz

> curl-config --configure

'--with-ssl' '--with-ipv6' '--enable-ldap' '--enable-ldaps'
> cat /etc/redhat-release
CentOS Linux release 7.0.1406 (Core) 

> curl --version
curl 7.29.0 (x86_64-redhat-linux-gnu) libcurl/7.29.0 NSS/3.19.1 Basic ECC zlib/1.2.7 libidn/1.28 libssh2/1.4.3
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smtp smtps telnet tftp 
Features: AsynchDNS GSS-Negotiate IDN IPv6 Largefile NTLM NTLM_WB SSL libz

> curl-config --configure

'--build=x86_64-redhat-linux-gnu' '--host=x86_64-redhat-linux-gnu' '--program-prefix=' '--disable-dependency-tracking' '--prefix=/usr' '--exec-prefix=/usr' '--bindir=/usr/bin' '--sbindir=/usr/sbin' '--sysconfdir=/etc' '--datadir=/usr/share' '--includedir=/usr/include' '--libdir=/usr/lib64' '--libexecdir=/usr/libexec' '--localstatedir=/var' '--sharedstatedir=/var/lib' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--disable-static' '--enable-hidden-symbols' '--enable-ipv6' '--enable-ldaps' '--enable-manual' '--enable-threaded-resolver' '--with-ca-bundle=/etc/pki/tls/certs/ca-bundle.crt' '--with-gssapi' '--with-libidn' '--with-libssh2' '--without-ssl' '--with-nss' 'build_alias=x86_64-redhat-linux-gnu' 'host_alias=x86_64-redhat-linux-gnu' 'CFLAGS=-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' 'LDFLAGS=-Wl,-z,relro '

参考文章

上一篇 下一篇

猜你喜欢

热点阅读