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 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到
  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错