首页 > Web开发 > 详细

ngx_http_finalize_request的使用

时间:2020-02-25 19:37:15      阅读:166      评论:0      收藏:0      [点我收藏+]

情景:

开发了一个http模块,挂在conten-phase阶段,

static char *
ngx_http_ivms(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {
    ngx_http_core_loc_conf_t *clcf;
 
    clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
    /* 设置回调函数。当请求http://127.0.0.1/ivms的时候,会调用此回调函数 */
    clcf->handler = ngx_http_ivms_loc_handler;
    
    return NGX_CONF_OK;
}

在ngx_http_ivms_loc_handler中读取包体,回调为ngx_http_ivms_handler;

static ngx_int_t 
ngx_http_ivms_loc_handler(ngx_http_request_t *r)
{
    ngx_int_t rc;
    
    rc = ngx_http_read_client_request_body(r, ngx_http_ivms_handler);

    if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
        return rc;
    }
    
//    return NGX_OK;
    return NGX_DONE;
}

在ngx_http_ivms_handler中处理包体并返回。

问题:

我想在处理完包体后销毁这个request,调用ngx_http_finalize_request()之后发现连接没法再次接收请求,原因是r->main->count =2;在销毁请求过程中减一,发现还有引用,所以没有销毁请求而是直接返回了。

cgdb调试nginx,watch r->main->count(没有子请求,所以这就是r->count),发现在创建r的时候r->count++,在ngx_http_read_client_request_body的时候r->count++;

解决:

我查看了nginx中ngx_http_read_client_request_body的使用,在其回调函数中并没有找到r->count--,听说在别的地方有减,但是我直接在其回调中r->count--,问题解决。

总结:

debug的时候发现,https://blog.csdn.net/ApeLife/article/details/74780059 这篇博文说的对:“到此,需要正常的关闭http请求了,但会不会马上就关闭请求要看引用计数,引用计数为0则会关闭请求,但并不一定会马上关闭tcp连接,因为有可能开启了keepalive机制或者延迟关闭机制。也就是说引用计数决定是否需要关闭http请求,而keepalive机制或者延迟关闭机制则决定是否需要关闭tcp连接。”

请求中Connection: close,则r->keepalive =1时,不会直接销毁会话,需要设置r->cln,当销毁同一个connection的request的时候,会调用之前r->cln->handler,做清理工作;

请求中Connection: close,则r->keepalive=0时,request处理完,直接销毁会话。

 

ngx_http_finalize_request的使用

原文:https://www.cnblogs.com/micoblog/p/12363192.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!