lighttpd的超时参数详解

[| 不指定 2012/02/14 21:00]
| |
Lighttpd配置中,关于超时的参数有如下几个(篇幅考虑,只写读超时,写超时参数同理):


server.max-keep-alive-idle = 5
server.max-read-idle = 60
server.read-timeout = 0
server.max-connection-idle = 360


这几个参数意思相近,配置的时候很容易搞混。




对于一个keep-alive连接上的连续请求,发送第一个请求内容的最大间隔由参数max-read-idle决定,从第二个请求起,发送请求内容的最大间隔由参数max-keep-alive-idle决定。请求间的间隔超时也由max-keep-alive-idle决定。发送请求内容的总时间超时由参数read-timeout决定。Lighttpd与后端交互数据的超时由max-connection-idle决定。



例子:


下面是模拟客户端代码:


$fp = fsockopen("127.0.0.1", 8902, $errno, $errstr, 30);

fwrite($fp, "GET / HTTP/1.1\r\n");
sleep(3);                                             //$1这个时间必须小于max-read-idle,否则会超时
fwrite($fp, "Host: a.com\r\n");
sleep(3);                                             //$2这个时间必须小于max-read-idle,否则会超时。且$1+$2时间之和必须小于read-timeout,否则超时
fwrite($fp, "Connection: Keep-Alive\r\n\r\n");
echo fread($fp, 1024);



sleep(7);                                             //$3 这个时间必须小于max-keep-alive-idle,否则超时



fwrite($fp, "GET / HTTP/1.1\r\n");
fwrite($fp, "Host: a.com\r\n");
sleep(15);                                           //$4  这个时间必须小于max-keep-alive-idle,否则超时,可以大于max-read-idle,但仍然不能超过read-timeout
fwrite($fp, "Connection: Keep-Alive\r\n\r\n");
echo fread($fp, 1024);
fclose($fp);

                                                  //以上时间均不受max-connection-idle限制


下面是模拟后端server代码:



$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if($sock == NULL)
{
    echo "can't create socket";
    exit;
}
if(!socket_bind($sock, "0.0.0.0", 8904))
{
    echo "can't bind socket";
    exit;
}
socket_listen($sock, 100);

while(1)
{
    if($new_conn = socket_accept($sock))
    {  
        $recv = socket_read($new_conn, 100000);
        //echo $recv;
        echo "begin sleep\n";
        sleep(10);                                                                                  //这个时间必须小于max-connection-idle,否则会超时
        echo "end sleep\n";
        socket_write($new_conn, "HTTP/1.1 200 OK\r\nDate: Tue, 01 Nov 2011 05:58:25 GMT\r\nServer: TestServer/1.0\r\nContent-Length: 1\r\nContent-Type: text/html;charset=gb2312\r\nConnection: Keep-Alive\r\n\r\na");
    }
    else
    {
        echo "accept failed!";
    }
}




下面是lighttpd中关于这几个参数实现的代码:

if (con->recv->is_closed) {                                                                        
                                                                 if (srv->cur_ts - con->read_idle_ts > con->conf.max_connection_idle) {                                              //对于客户端已经发送完请求数据的情况下,超时时间max-connection-idle
                                                                           /* time - out */
#if 1
                                                                           WARNING("(connection process timeout) [%s]", SAFE_BUF_STR(con->dst_addr_buf));
#endif
                                                                           connection_set_state(srv, con, CON_STATE_ERROR);
                                                                           changed = 1;
                                                                 }
                                                        }
                                                        else {

                                                                 if (con->request_count == 1) {                  
                                                                           if (srv->cur_ts - con->read_idle_ts > con->conf.max_read_idle) {                                              //对于第一个请求,发送的数据最大时间间隔:max_read_idle
                                                                                    /* time - out */
#if 1
                                                                                    if (con->conf.log_timeouts) {
                                                                                             WARNING("(initial read timeout) [%s]", SAFE_BUF_STR(con->dst_addr_buf));
                                                                                    }
#endif
                                                                                    connection_set_state(srv, con, CON_STATE_ERROR);
                                                                                    changed = 1;
                                                                           }
                                                                 } else {                                                                                               //从第二个请求开始,发送的数据最大时间间隔:keep_alive_idle
                                                                           if (srv->cur_ts - con->read_idle_ts > con->keep_alive_idle) {
                                                                                    /* time - out */
#if 1
                                                                                    if (con->conf.log_timeouts) {
                                                                                             DEBUG("(keep-alive read timeout) [%s]", SAFE_BUF_STR(con->dst_addr_buf));
                                                                                    }
#endif
                                                                                    connection_set_state(srv, con, CON_STATE_ERROR);
                                                                                    changed = 1;
                                                                           }
                                                                 }

                                                                 if (con->conf.read_timeout > 0 && con->read_start_ts > 0)                                                    //在read_timeout设置不为0的情况下,发送数据的最大总时间:read_timeout
                                                                 {
                                                                           used_time = srv->cur_ts - con->read_start_ts;
                                                                           if (used_time > con->conf.read_timeout)
                                                                           {
                                                                                    WARNING ("read timeout, client[%s], time=%lu",
                                                                                             SAFE_BUF_STR(con->dst_addr_buf), used_time);
                                                                                    connection_set_state(srv, con, CON_STATE_ERROR);
                                                                                    changed = 1;
                                                                           }
                                                                 }
                                                        }
by snooda | 分类: lighttpd | 评论(0) | 引用(0) | 阅读(9024)
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]