作为处理相应数据过滤器,ngx_http_write_filter_module起到了收集响应数据(包含buffer缓冲数据及本地文件 本地文件包含实际的静态文件及缓存文件)、使用定时器设置响应限速延时的功能。
ngx_http_write_filter_module也是使用top_body_filter来挂载在body_filter链来处理的,不过write_filter通常位于处理链的最后一个,如下图的堆栈结构也能看到,write_filter是作为最后一个来处理并发送响应数据的过滤器。如下图是处理处理包体数据在经过write_filter处理时的堆栈结构。
通过write_filter处理之后的buffer链中的数据(包括内存及文件)会被预先设置的send_chain函数通过网络处理来发送到请求端。write_filter处理的代码流程如下:
ngx_int_t
ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in)
{
...
/*取得请求的连接*/
c = r->connection;
if (c->error) { /*发现连接存在错误 直接返回错误*/
return NGX_ERROR;
}
size = 0;
flush = 0;
sync = 0;
last = 0;
ll = &r->out;
/* find the size, the flush point and the last link of the saved chain */
for (cl = r->out; cl; cl = cl->next) { /*响应的out链*/
ll = &cl->next;
ngx_log_debug7(NGX_LOG_DEBUG_EVENT, c->log, 0,
"write old buf t:%d f:%d %p, pos %p, size: %z "
"file: %O, size: %O",
cl->buf->temporary, cl->buf->in_file,
cl->buf->start, cl->buf->pos,
cl->buf->last - cl->buf->pos,
cl->buf->file_pos,
cl->buf->file_last - cl->buf->file_pos);
#if 1 /*检查out链中的buffer chain中保存的buffer数据 */
if (ngx_buf_size(cl->buf) == 0 && !ngx_buf_special(cl->buf)) {
ngx_log_error(NGX_LOG_ALERT, c->log, 0,
"zero size buf in writer "
"t:%d r:%d f:%d %p %p-%p %p %O-%O",
cl-&