PHP is actively disconnected under Nginx Connection close and ignore

Source: Internet
Author: User
Tags continue header sleep

This two days to get a PHP call SVN synchronous Update multiple server update program, in order to avoid a commit will not be blocked card half a day to think of a way to only request the trigger, and do not need to wait for the program update to complete the return result that takes too long, So I've studied how to get PHP to actively disconnect the connection. Engaged in the afternoon, found that a lot of problems, but fortunately eventually came out, mainly Nginx Tai Keng.

Nonsense not much to say, the following code:

/**
 * 主动断开与客户端浏览器的连接
 * 如果是 Nginx 服务器需要输出大于等于 fastcgi_buffer_size 缓存的数据才能即时输出 header 断开连接, 若还是不行可尝试关闭 gzip
 * 如: fastcgi_buffer_size 64k; 即: 需要 64*1024 字符(可多不可少),
 * 可使用 str_repeat(' ', 65536); 另外 str_repeat('          ', 6554); 这种方式其实生成速度更慢
 * @param nullstring $str 当前输出的内容, 若无需输出则设置为空
 */
public function connectionClose($str = null) {
    $str = ob_get_contents() . $str;
    // 若实际输出内容长度小于该值将可能导致主动断开失败
    header('Content-Length: '. strlen($str));
    Header::connectionClose();
    ob_start();
    echo $str;
    ob_flush();
    flush();
}

For Apache generally no problem, I started on Windows with the Xampp debugging did not find any problems, the results to the server is Nginx, dead or alive, collapsed a afternoon, and then reflected over the Nginx Fastcgi_buffer of the problem.

Various cases have been tested n times, there should be no bugs ...

When the browser is closed, determines whether the program will continue to execute in the background (in the example below, You do not have to set the test to never timeout limit 0, set a two-minute line, otherwise it may take a long time to restart the HTTP service.

In simple terms, if you want the user's browser to shut down and you need the program to continue, you must add The following code:

 ignore_user_abort (true);  

But slightly different depending on how you followed the program, which is mainly a while dead loop:

Generally in a program you can monitor the connection state for control:

  $isAborted = connection_aborted (); $status = Connection_status (
); if (0!== $status $isAborted) {break;}  

But these two functions have a prerequisite for working properly, that is, your program must have output that is larger than the current webserver output cache, which will work.

If you simply output a space of echo ', you may have to loop thousands of times to determine, so in order to detect the state more instantly you have to output enough content per loop to trigger state detection.

So there's always a problem here: When the browser is disconnected, even if ignore_user_abort (true) is not used, but because there is no output, the program will continue to execute, the dead loop will run, if you set a timeout that's fine, otherwise it will really die.

below the test code (paste a figure is mainly to anti-theft hey ~)

  

You can try commenting out this sentence
//Echo str_repeat (', 65536);
also
set_time_limit (0); better not use 0



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.