douyu8187 2013-10-24 16:47
浏览 109
已采纳

对同一服务器的卷曲请求间歇性地超时

I have developed a proxy service in php which uses php-curl to request another url on the same server. Some times it works great but other times it takes a really long time and a lot of the time it times out.

This application does not make use of sessions so it can't be a locked session issue. I tried session_write_close() as a test before calling curl_exec and it made no difference.

What is the cause of this inconsistent behavior? I expect it to respond immediately since the only work it is doing is serving up 302 redirects. I have pasted my proxy function below.

protected function proxy( $pURL,  $opts = array() ){

    $defaults = array(
        'headers' => array(),
        'follow' => true,
        'return' => false,
        'return_headers' => false,
        'referer' => fp_get_config( 'referer_override' ),
        'user_agent' => $_SERVER['HTTP_USER_AGENT'],
        'timeout' => 30,
        'connect_timeout' => 10,
        'fresh_connect' => false
    );

    $options = array_merge( $defaults, $opts );

    extract( $options );

    $c = curl_init( $pURL );

    curl_setopt_array( $c, array(
        CURLOPT_HEADER => $return_headers,
        CURLOPT_USERAGENT => $user_agent,
        CURLOPT_REFERER => $referer,
        CURLOPT_CONNECTTIMEOUT => $connect_timeout,
        CURLOPT_TIMEOUT => $timeout,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_FOLLOWLOCATION => $follow,
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_FRESH_CONNECT => $fresh_connect,
        CURLOPT_AUTOREFERER => true
    ));

    //session_write_close();

    $response = curl_exec( $c );

    if ( $response === false )
        die("Proxy Error: " . curl_error( $c ) );


    curl_close( $c );

    if ( $return )
        return $response;
    else {

        if ( $return_headers ){

            list( $headerblock, $body ) = explode("

", $response, 2);
            $headers = explode("
", $headerblock );

            foreach( $headers as $header )
                header( $header );

            echo $body;

        } else
            echo $response;
    }

    exit;

}
  • 写回答

1条回答 默认 最新

  • dongyuans61046 2013-12-02 17:48
    关注

    Turns out what was happening was this: We have two load balancers configured for high availability. Whenever one lb made a curl request to the other lb it was getting blocked by the firewall which caused the connection to timeout. Fixing the firewall rules resolved this issue.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题