nginx子请求与addtion filter模块源码分析

配置:

server {

        root /var/www/html;

        index  main.html main.htm;

        listen 127.0.0.1:991;

        location / {

            add_before_body /before.html;

            add_after_body  /after.html;

        }   

    }

返回:

curl 127.0.0.1:991

before

main

after

分步:

1,主请求:在ngx_http_addition_body_filter中,调用ngx_http_subrequest,创建第一子请求。

2,主请求:ngx_http_postpone_filter中,将主请求的返回in,加入r→postponed

3,主请求:在ngx_http_addition_body_filter中,调用ngx_http_subrequest,创建第二子请求。

4,主请求:ngx_http_finalize_request,因为主请求下有postponed(第一子请求,in:456,第二子请求),set_writer_handler设置write_event_handler。

5,主请求:ngx_http_run_posted_requests,开始处理子请求,此时里面有第一子请求,第二子请求。

6,第一子请求:ngx_http_postpone_filter中,因为它没有子请求,in直接加到r→main的chain中,等待发送。

7,第一子请求:ngx_http_finalize_request,c→data指针移交给主请求,同时将主请求加入到posted_requests,等到被调用,此时posted_requests中为第二子请求和主请求。

8,第二子请求:ngx_http_postpone_filter中,因为c→data不指向第二子请求,不具备发送能力,将生成的返回加入第二子请求的postponed中。

9,第二子请求:ngx_http_finalize_request,第二子请求下有postponed,调用set_writer_handler设置write_event_handler回调,ngx_http_run_posted_requests开始遍历到主请求,此时为posted_requests仅为主请求。

10,主请求:ngx_http_postpone_filter中,遍历自己的postponed,将自己生成的返回加入chain,将c→data设置为第二子请求,将第二子请求加入posted_requests。

11,主请求:ngx_http_finalize_request,set_writer_handler设置write_event_handler,继续遍历,去第二子请求。

12,第二子请求:ngx_http_postpone_filter中,发送它自己postponed下的返回。

13,第二子请求:ngx_http_finalize_request,c→data此时等于它本身,顺利结束,最后不忘将主请求加回posted_requests。

14,主请求:ngx_http_postpone_filter,ngx_http_finalize_request,发送数据ngx_http_post_action。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值