static void ngx_worker_process_exit(ngx_cycle_t *cycle) { ngx_uint_t i; ngx_connection_t *c; #if (NGX_THREADS) ngx_terminate = 1; ngx_wakeup_worker_threads(cycle); #endif for (i = 0; ngx_modules[i]; i++) { if (ngx_modules[i]->exit_process) { ngx_modules[i]->exit_process(cycle); } } if (ngx_exiting) { c = cycle->connections; for (i = 0; i < cycle->connection_n; i++) { if (c[i].fd != -1 && c[i].read && !c[i].read->accept && !c[i].read->channel && !c[i].read->resolver) { ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "open socket #%d left in connection %ui%s", c[i].fd, i, ngx_debug_quit ? ", aborting" : ""); ngx_debug_point(); } } if (ngx_debug_quit) { ngx_debug_point(); } } /* * Copy ngx_cycle->log related data to the special static exit cycle, * log, and log file structures enough to allow a signal handler to log. * The handler may be called when standard ngx_cycle->log allocated from * ngx_cycle->pool is already destroyed. */ ngx_exit_log_file.fd = ngx_cycle->log->file->fd; ngx_exit_log = *ngx_cycle->log; ngx_exit_log.file = &ngx_exit_log_file; ngx_exit_cycle.log = &ngx_exit_log; ngx_cycle = &ngx_exit_cycle; ngx_destroy_pool(cycle->pool); ngx_log_error(NGX_LOG_NOTICE, ngx_cycle->log, 0, "exit"); exit(0); }
/* 工作进程退出 */ static void ngx_worker_process_exit(ngx_cycle_t *cycle) { ngx_uint_t i; ngx_connection_t *c; for (i = 0; ngx_modules[i]; i++) { if (ngx_modules[i]->exit_process) { ngx_modules[i]->exit_process(cycle); } } if (ngx_exiting) { c = cycle->connections; for (i = 0; i < cycle->connection_n; i++) { if (c[i].fd != -1 && c[i].read && !c[i].read->accept && !c[i].read->channel && !c[i].read->resolver) { ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "*%uA open socket #%d left in connection %ui", c[i].number, c[i].fd, i); ngx_debug_quit = 1; } } if (ngx_debug_quit) { ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "aborting"); ngx_debug_point(); } } /* * Copy ngx_cycle->log related data to the special static exit cycle, * log, and log file structures enough to allow a signal handler to log. * The handler may be called when standard ngx_cycle->log allocated from * ngx_cycle->pool is already destroyed. */ ngx_exit_log = *ngx_log_get_file_log(ngx_cycle->log); ngx_exit_log_file.fd = ngx_exit_log.file->fd; ngx_exit_log.file = &ngx_exit_log_file; ngx_exit_log.next = NULL; ngx_exit_log.writer = NULL; ngx_exit_cycle.log = &ngx_exit_log; ngx_exit_cycle.files = ngx_cycle->files; ngx_exit_cycle.files_n = ngx_cycle->files_n; ngx_cycle = &ngx_exit_cycle; ngx_destroy_pool(cycle->pool); ngx_log_error(NGX_LOG_NOTICE, ngx_cycle->log, 0, "exit"); /* 此时进程就退出了,进程之后的代码就都不会执行了 */ exit(0); }
ngx_int_t ngx_tcp_chain_writer(void *data, ngx_chain_t *in) { ngx_chain_writer_ctx_t *ctx = data; off_t size; ngx_chain_t *cl; ngx_connection_t *c; c = ctx->connection; if (ctx->out == NULL) { *(ctx->last) = in; } else { ctx->out->next = in; } ngx_log_debug1(NGX_LOG_DEBUG_CORE, c->log, 0, "tcp chain writer in: %p", ctx->out); size = 0; for (cl = ctx->out; cl; cl = cl->next) { #if 1 if (ngx_buf_size(cl->buf) == 0 && !ngx_buf_special(cl->buf)) { ngx_debug_point(); } #endif size += ngx_buf_size(cl->buf); } if (size == 0 && !c->buffered) { return NGX_OK; } ctx->out = c->send_chain(c, ctx->out, ctx->limit); ngx_log_debug1(NGX_LOG_DEBUG_CORE, c->log, 0, "chain writer out: %p", ctx->out); if (ctx->out == NGX_CHAIN_ERROR) { return NGX_ERROR; } if (ctx->out == NULL) { ctx->last = &ctx->out; if (!c->buffered) { return NGX_OK; } } return NGX_AGAIN; }
static ngx_int_t ngx_http_replace_output(ngx_http_request_t *r, ngx_http_replace_ctx_t *ctx) { ngx_int_t rc; ngx_buf_t *b; ngx_chain_t *cl; #if (DDEBUG) b = NULL; for (cl = ctx->out; cl; cl = cl->next) { ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "replace out: %p %p", cl->buf, cl->buf->pos); if (cl->buf == b) { ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "the same buf was used in sub"); ngx_debug_point(); return NGX_ERROR; } b = cl->buf; } /* ngx_http_replace_dump_chain("ctx->out", &ctx->out, ctx->last_out); */ #endif rc = ngx_http_next_body_filter(r, ctx->out); /* we are essentially duplicating the logic of * ngx_chain_update_chains below, * with our own optimizations */ if (ctx->busy == NULL) { ctx->busy = ctx->out; } else { for (cl = ctx->busy; cl->next; cl = cl->next) { /* void */ } cl->next = ctx->out; } ctx->out = NULL; ctx->last_out = &ctx->out; while (ctx->busy) { cl = ctx->busy; b = cl->buf; if (ngx_buf_size(b) != 0) { break; } if (cl->buf->tag != (ngx_buf_tag_t) &ngx_http_replace_filter_module) { ctx->busy = cl->next; ngx_free_chain(r->pool, cl); continue; } if (b->shadow) { b->shadow->pos = b->shadow->last; b->shadow->file_pos = b->shadow->file_last; } ctx->busy = cl->next; if (ngx_buf_special(b)) { /* collect special bufs to ctx->special, which may still be busy */ cl->next = NULL; *ctx->last_special = cl; ctx->last_special = &cl->next; } else { /* add ctx->special to ctx->free because they cannot be busy at * this point */ *ctx->last_special = ctx->free; ctx->free = ctx->special; ctx->special = NULL; ctx->last_special = &ctx->special; #if 0 /* free the temporary buf's data block if it is big enough */ if (b->temporary && b->start != NULL && b->end - b->start > (ssize_t) r->pool->max) { ngx_pfree(r->pool, b->start); } #endif /* add the data buf itself to the free buf chain */ cl->next = ctx->free; ctx->free = cl; } } if (ctx->in || ctx->buf) { r->buffered |= NGX_HTTP_SUB_BUFFERED; } else { r->buffered &= ~NGX_HTTP_SUB_BUFFERED; } return rc; }
ngx_int_t ngx_chain_writer(void *data, ngx_chain_t *in) { #if 0 ngx_chain_writer_ctx_t *ctx = data; off_t size; ngx_chain_t *cl; ngx_connection_t *c; c = ctx->connection; for (size = 0; in; in = in->next) { #if 1 if (ngx_buf_size(in->buf) == 0 && !ngx_buf_special(in->buf)) { ngx_debug_point(); } #endif size += ngx_buf_size(in->buf); ngx_log_debug2(NGX_LOG_DEBUG_CORE, c->log, 0, "chain writer buf fl:%d s:%uO", in->buf->flush, ngx_buf_size(in->buf)); cl = ngx_alloc_chain_link(ctx->pool); if (cl == NULL) { return NGX_ERROR; } cl->buf = in->buf; cl->next = NULL; *ctx->last = cl; ctx->last = &cl->next; } ngx_log_debug1(NGX_LOG_DEBUG_CORE, c->log, 0, "chain writer in: %p", ctx->out); for (cl = ctx->out; cl; cl = cl->next) { #if 1 if (ngx_buf_size(cl->buf) == 0 && !ngx_buf_special(cl->buf)) { ngx_debug_point(); } #endif size += ngx_buf_size(cl->buf); } if (size == 0 && !c->buffered) { return NGX_OK; } ctx->out = c->send_chain(c, ctx->out, ctx->limit); ngx_log_debug1(NGX_LOG_DEBUG_CORE, c->log, 0, "chain writer out: %p", ctx->out); if (ctx->out == NGX_CHAIN_ERROR) { return NGX_ERROR; } if (ctx->out == NULL) { ctx->last = &ctx->out; if (!c->buffered) { return NGX_OK; } } #endif return NGX_AGAIN; }
static ngx_int_t ngx_http_spdy_serverpush_body_filter(ngx_http_request_t *r, ngx_chain_t *in) { off_t size; ngx_buf_t *b; ngx_chain_t *cl, *ll, *out, **ln; ngx_http_spdy_stream_t *stream; ngx_http_spdy_out_frame_t *frame; ngx_output_chain_ctx_t *ctx; ctx = ngx_pcalloc(r->pool, sizeof(ngx_output_chain_ctx_t)); ngx_http_set_ctx(r, ctx, ngx_http_spdy_serverpush_filter_module); //ngx_chain_t *myChain=NULL; ngx_http_static_handler(r); stream =myStream; if (stream == NULL) { return ngx_http_next_body_filter(r, in); } ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "spdy body filter \"%V?%V\"", &r->uri, &r->args); ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "Data in server Push "); if (myChain == NULL || r->header_only) { ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "Ending"); if (stream->waiting) { return NGX_AGAIN; } r->connection->buffered &= ~NGX_SPDY_WRITE_BUFFERED; return NGX_OK; } size = 0; ln = &out; ll = myChain; ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "into for "); for ( ;; ) { b = ll->buf; //#if 1 if(b->file) { ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "file detected push : %d",(b->file_last-b->file_pos)); ngx_int_t my_size = b->file_last-b->file_pos; u_char* my_buf = ngx_pcalloc(r->pool,sizeof(u_char)*my_size); ngx_read_file(b->file, my_buf, my_size, 0); b->start = my_buf; b->pos = my_buf; b->last = my_buf+my_size;//+NGX_SPDY_FRAME_HEADER_SIZE; } if (ngx_buf_size(b) == 0 && !ngx_buf_special(b)) { ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "zero size buf in spdy body filter " "t:%d r:%d f:%d %p %p-%p %p %O-%O", b->temporary, b->recycled, b->in_file, b->start, b->pos, b->last, b->file, b->file_pos, b->file_last); ngx_debug_point(); return NGX_ERROR; } //#endif ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "out of for"); cl = ngx_alloc_chain_link(r->pool); if (cl == NULL) { return NGX_ERROR; } size += ngx_buf_size(b); ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "Size %d ",size); cl->buf = b; *ln = cl; ln = &cl->next; if (ll->next == NULL) { break; } ll = ll->next; } if (size > NGX_SPDY_MAX_FRAME_SIZE) { ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "FIXME: chain too big in spdy filter: %O", size); return NGX_ERROR; } ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "ONE "); frame = ngx_http_spdy_filter_get_data_frame(stream, (size_t) size, b->last_buf, out, cl); ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "FOUR "); if (frame == NULL) { return NGX_ERROR; } ngx_http_spdy_queue_frame(stream->connection, frame); stream->waiting++; r->main->blocked++; ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "Data in server Push end sai ram "); ngx_http_spdy_serverpush_filter_send(r->connection, stream); return ngx_http_next_body_filter(r,in); }
static ngx_int_t ngx_http_spdy_body_filter(ngx_http_request_t *r, ngx_chain_t *in) { off_t size; ngx_buf_t *b; ngx_chain_t *cl, *ll, *out, **ln; ngx_http_spdy_stream_t *stream; ngx_http_spdy_out_frame_t *frame; stream = r->spdy_stream; if (stream == NULL) { return ngx_http_next_body_filter(r, in); } ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "spdy body filter \"%V?%V\"", &r->uri, &r->args); if (in == NULL || r->header_only) { if (stream->waiting) { return NGX_AGAIN; } r->connection->buffered &= ~NGX_SPDY_WRITE_BUFFERED; return NGX_OK; } size = 0; ln = &out; ll = in; for ( ;; ) { b = ll->buf; #if 1 if (ngx_buf_size(b) == 0 && !ngx_buf_special(b)) { ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "zero size buf in spdy body filter " "t:%d r:%d f:%d %p %p-%p %p %O-%O", b->temporary, b->recycled, b->in_file, b->start, b->pos, b->last, b->file, b->file_pos, b->file_last); ngx_debug_point(); return NGX_ERROR; } #endif cl = ngx_alloc_chain_link(r->pool); if (cl == NULL) { return NGX_ERROR; } size += ngx_buf_size(b); cl->buf = b; *ln = cl; ln = &cl->next; if (ll->next == NULL) { break; } ll = ll->next; } if (size > NGX_SPDY_MAX_FRAME_SIZE) { ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "FIXME: chain too big in spdy filter: %O", size); return NGX_ERROR; } frame = ngx_http_spdy_filter_get_data_frame(stream, (size_t) size, b->last_buf, out, cl); if (frame == NULL) { return NGX_ERROR; } ngx_http_spdy_queue_frame(stream->connection, frame); stream->waiting++; r->main->blocked++; return ngx_http_spdy_filter_send(r->connection, stream); }
ngx_chain_t * ngx_linux_sendfile_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit) { int rc, tcp_nodelay; off_t size, send, prev_send, aligned, sent, fprev; u_char *prev; size_t file_size; ngx_err_t err; ngx_buf_t *file; ngx_uint_t eintr, complete; ngx_array_t header; ngx_event_t *wev; ngx_chain_t *cl; struct iovec *iov, headers[NGX_HEADERS]; #if (NGX_HAVE_SENDFILE64) off_t offset; #else int32_t offset; #endif wev = c->write; if (!wev->ready) { return in; } /* the maximum limit size is 2G-1 - the page size */ if (limit == 0 || limit > (off_t) (NGX_SENDFILE_LIMIT - ngx_pagesize)) { limit = NGX_SENDFILE_LIMIT - ngx_pagesize; } send = 0; header.elts = headers; header.size = sizeof(struct iovec); header.nalloc = NGX_HEADERS; header.pool = c->pool; for ( ;; ) { file = NULL; file_size = 0; eintr = 0; complete = 0; prev_send = send; header.nelts = 0; prev = NULL; iov = NULL; /* create the iovec and coalesce the neighbouring bufs */ for (cl = in; cl && header.nelts < IOV_MAX && send < limit; cl = cl->next) { if (ngx_buf_special(cl->buf)) { continue; } #if 1 if (!ngx_buf_in_memory(cl->buf) && !cl->buf->in_file) { ngx_log_error(NGX_LOG_ALERT, c->log, 0, "zero size buf in sendfile " "t:%d r:%d f:%d %p %p-%p %p %O-%O", cl->buf->temporary, cl->buf->recycled, cl->buf->in_file, cl->buf->start, cl->buf->pos, cl->buf->last, cl->buf->file, cl->buf->file_pos, cl->buf->file_last); ngx_debug_point(); return NGX_CHAIN_ERROR; } #endif if (!ngx_buf_in_memory_only(cl->buf)) { break; } size = cl->buf->last - cl->buf->pos; if (send + size > limit) { size = limit - send; } if (prev == cl->buf->pos) { iov->iov_len += (size_t) size; } else { iov = ngx_array_push(&header); if (iov == NULL) { return NGX_CHAIN_ERROR; } iov->iov_base = (void *) cl->buf->pos; iov->iov_len = (size_t) size; } prev = cl->buf->pos + (size_t) size; send += size; } /* set TCP_CORK if there is a header before a file */ if (c->tcp_nopush == NGX_TCP_NOPUSH_UNSET && header.nelts != 0 && cl && cl->buf->in_file) { /* the TCP_CORK and TCP_NODELAY are mutually exclusive */ if (c->tcp_nodelay == NGX_TCP_NODELAY_SET) { tcp_nodelay = 0; if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY, (const void *) &tcp_nodelay, sizeof(int)) == -1) { err = ngx_errno; /* * there is a tiny chance to be interrupted, however, * we continue a processing with the TCP_NODELAY * and without the TCP_CORK */ if (err != NGX_EINTR) { wev->error = 1; ngx_connection_error(c, err, "setsockopt(TCP_NODELAY) failed"); return NGX_CHAIN_ERROR; } } else { c->tcp_nodelay = NGX_TCP_NODELAY_UNSET; ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "no tcp_nodelay"); } } if (c->tcp_nodelay == NGX_TCP_NODELAY_UNSET) { if (ngx_tcp_nopush(c->fd) == NGX_ERROR) { err = ngx_errno; /* * there is a tiny chance to be interrupted, however, * we continue a processing without the TCP_CORK */ if (err != NGX_EINTR) { wev->error = 1; ngx_connection_error(c, err, ngx_tcp_nopush_n " failed"); return NGX_CHAIN_ERROR; } } else { c->tcp_nopush = NGX_TCP_NOPUSH_SET; ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0, "tcp_nopush"); } } } /* get the file buf */ if (header.nelts == 0 && cl && cl->buf->in_file && send < limit) { file = cl->buf; /* coalesce the neighbouring file bufs */ do { size = cl->buf->file_last - cl->buf->file_pos; if (send + size > limit) { size = limit - send; aligned = (cl->buf->file_pos + size + ngx_pagesize - 1) & ~((off_t) ngx_pagesize - 1); if (aligned <= cl->buf->file_last) { size = aligned - cl->buf->file_pos; } } file_size += (size_t) size; send += size; fprev = cl->buf->file_pos + size; cl = cl->next; } while (cl && cl->buf->in_file && send < limit && file->file->fd == cl->buf->file->fd && fprev == cl->buf->file_pos); } if (file) { #if 1 if (file_size == 0) { ngx_debug_point(); return NGX_CHAIN_ERROR; } #endif #if (NGX_HAVE_SENDFILE64) offset = file->file_pos; #else offset = (int32_t) file->file_pos; #endif ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0, "sendfile: @%O %uz", file->file_pos, file_size); rc = sendfile(c->fd, file->file->fd, &offset, file_size); if (rc == -1) { err = ngx_errno; switch (err) { case NGX_EAGAIN: break; case NGX_EINTR: eintr = 1; break; default: wev->error = 1; ngx_connection_error(c, err, "sendfile() failed"); return NGX_CHAIN_ERROR; } ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err, "sendfile() is not ready"); } sent = rc > 0 ? rc : 0; ngx_log_debug4(NGX_LOG_DEBUG_EVENT, c->log, 0, "sendfile: %d, @%O %O:%uz", rc, file->file_pos, sent, file_size); } else { rc = writev(c->fd, header.elts, header.nelts); if (rc == -1) { err = ngx_errno; switch (err) { case NGX_EAGAIN: break; case NGX_EINTR: eintr = 1; break; default: wev->error = 1; ngx_connection_error(c, err, "writev() failed"); return NGX_CHAIN_ERROR; } ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err, "writev() not ready"); } sent = rc > 0 ? rc : 0; ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "writev: %O", sent); } if (send - prev_send == sent) { complete = 1; } c->sent += sent; for (cl = in; cl; cl = cl->next) { if (ngx_buf_special(cl->buf)) { continue; } if (sent == 0) { break; } size = ngx_buf_size(cl->buf); if (sent >= size) { sent -= size; if (ngx_buf_in_memory(cl->buf)) { cl->buf->pos = cl->buf->last; } if (cl->buf->in_file) { cl->buf->file_pos = cl->buf->file_last; } continue; } if (ngx_buf_in_memory(cl->buf)) { cl->buf->pos += (size_t) sent; } if (cl->buf->in_file) { cl->buf->file_pos += sent; } break; } if (eintr) { continue; } if (!complete) { wev->ready = 0; return cl; } if (send >= limit || cl == NULL) { return cl; } in = cl; } }
ngx_int_t ngx_chain_writer(void *data, ngx_chain_t *in) { ngx_chain_writer_ctx_t *ctx = data; off_t size; ngx_chain_t *cl, *ln, *chain; ngx_connection_t *c; c = ctx->connection; for (size = 0; in; in = in->next) { #if 1 if (ngx_buf_size(in->buf) == 0 && !ngx_buf_special(in->buf)) { ngx_log_error(NGX_LOG_ALERT, ctx->pool->log, 0, "zero size buf in chain writer " "t:%d r:%d f:%d %p %p-%p %p %O-%O", in->buf->temporary, in->buf->recycled, in->buf->in_file, in->buf->start, in->buf->pos, in->buf->last, in->buf->file, in->buf->file_pos, in->buf->file_last); ngx_debug_point(); continue; } #endif size += ngx_buf_size(in->buf); ngx_log_debug2(NGX_LOG_DEBUG_CORE, c->log, 0, "chain writer buf fl:%d s:%uO", in->buf->flush, ngx_buf_size(in->buf)); cl = ngx_alloc_chain_link(ctx->pool); if (cl == NULL) { return NGX_ERROR; } cl->buf = in->buf; cl->next = NULL; *ctx->last = cl; ctx->last = &cl->next; } ngx_log_debug1(NGX_LOG_DEBUG_CORE, c->log, 0, "chain writer in: %p", ctx->out); for (cl = ctx->out; cl; cl = cl->next) { #if 1 if (ngx_buf_size(cl->buf) == 0 && !ngx_buf_special(cl->buf)) { ngx_log_error(NGX_LOG_ALERT, ctx->pool->log, 0, "zero size buf in chain writer " "t:%d r:%d f:%d %p %p-%p %p %O-%O", cl->buf->temporary, cl->buf->recycled, cl->buf->in_file, cl->buf->start, cl->buf->pos, cl->buf->last, cl->buf->file, cl->buf->file_pos, cl->buf->file_last); ngx_debug_point(); continue; } #endif size += ngx_buf_size(cl->buf); } if (size == 0 && !c->buffered) { return NGX_OK; } chain = c->send_chain(c, ctx->out, ctx->limit); ngx_log_debug1(NGX_LOG_DEBUG_CORE, c->log, 0, "chain writer out: %p", chain); if (chain == NGX_CHAIN_ERROR) { return NGX_ERROR; } for (cl = ctx->out; cl && cl != chain; /* void */) { ln = cl; cl = cl->next; ngx_free_chain(ctx->pool, ln); } ctx->out = chain; if (ctx->out == NULL) { ctx->last = &ctx->out; if (!c->buffered) { return NGX_OK; } } return NGX_AGAIN; }
ngx_chain_t * ngx_writev_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit) { u_char *prev; ssize_t n, size, sent; off_t send, prev_send; ngx_uint_t eintr, complete; ngx_err_t err; ngx_array_t vec; ngx_chain_t *cl; ngx_event_t *wev; struct iovec *iov, iovs[NGX_IOVS]; wev = c->write; if (!wev->ready) { return in; } #if (NGX_HAVE_KQUEUE) if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT) && wev->pending_eof) { (void) ngx_connection_error(c, wev->kq_errno, "kevent() reported about an closed connection"); wev->error = 1; return NGX_CHAIN_ERROR; } #endif /* the maximum limit size is the maximum size_t value - the page size */ if (limit == 0 || limit > (off_t) (NGX_MAX_SIZE_T_VALUE - ngx_pagesize)) { limit = NGX_MAX_SIZE_T_VALUE - ngx_pagesize; } send = 0; vec.elts = iovs; vec.size = sizeof(struct iovec); vec.nalloc = NGX_IOVS; vec.pool = c->pool; for ( ;; ) { prev = NULL; iov = NULL; eintr = 0; complete = 0; prev_send = send; vec.nelts = 0; /* create the iovec and coalesce the neighbouring bufs */ for (cl = in; cl && send < limit; cl = cl->next) { if (ngx_buf_special(cl->buf)) { continue; } #if 1 if (!ngx_buf_in_memory(cl->buf)) { ngx_debug_point(); } #endif size = cl->buf->last - cl->buf->pos; if (send + size > limit) { size = (ssize_t) (limit - send); } if (prev == cl->buf->pos) { iov->iov_len += size; } else { if (vec.nelts >= IOV_MAX) { break; } iov = ngx_array_push(&vec); if (iov == NULL) { return NGX_CHAIN_ERROR; } iov->iov_base = (void *) cl->buf->pos; iov->iov_len = size; } prev = cl->buf->pos + size; send += size; } n = writev(c->fd, vec.elts, vec.nelts); if (n == -1) { err = ngx_errno; switch (err) { case NGX_EAGAIN: break; case NGX_EINTR: eintr = 1; break; default: wev->error = 1; (void) ngx_connection_error(c, err, "writev() failed (in writev_chain.c)"); return NGX_CHAIN_ERROR; } ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err, "writev() not ready"); } sent = n > 0 ? n : 0; ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "writev: %z", sent); if (send - prev_send == sent) { complete = 1; } c->sent += sent; for (cl = in; cl; cl = cl->next) { if (ngx_buf_special(cl->buf)) { continue; } if (sent == 0) { break; } size = cl->buf->last - cl->buf->pos; if (sent >= size) { sent -= size; cl->buf->pos = cl->buf->last; continue; } cl->buf->pos += sent; break; } if (eintr) { continue; } if (!complete) { wev->ready = 0; return cl; } if (send >= limit || cl == NULL) { return cl; } in = cl; } }
/* 参数r是对应的请求,in是保存本次待发送数据的链表缓冲区 */ ngx_int_t ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in) { off_t size, sent, nsent, limit; ngx_uint_t last, flush; ngx_msec_t delay; ngx_chain_t *cl, *ln, **ll, *chain; ngx_connection_t *c; ngx_http_core_loc_conf_t *clcf; /* 获取当前请求所对应的连接 */ c = r->connection; /* * 检查当前连接的错误标志位error,若该标志位为1, * 表示当前请求出错,返回NGX_ERROR; */ if (c->error) { return NGX_ERROR; } size = 0; flush = 0; last = 0; ll = &r->out; /* find the size, the flush point and the last link of the saved chain */ /* * 遍历当前请求out链表缓冲区,计算剩余响应报文的长度; * 因为当响应报文一次性不能发送完成时,会把剩余的响应报文保存在out中, * 相对于本次发送的响应报文数据in来说(即该方法所传入的参数in), * out链表缓冲区保存的是前一次剩余的响应报文; */ for (cl = r->out; cl; cl = cl->next) { 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: %z", 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 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->buf->temporary, cl->buf->recycled, cl->buf->in_file, cl->buf->start, cl->buf->pos, cl->buf->last, cl->buf->file, cl->buf->file_pos, cl->buf->file_last); ngx_debug_point(); return NGX_ERROR; } #endif size += ngx_buf_size(cl->buf); if (cl->buf->flush || cl->buf->recycled) { flush = 1; } if (cl->buf->last_buf) { last = 1; } } /* add the new chain to the existent one */ /* * 将本次待发送的响应报文的缓冲区in添加到out链表缓冲区的尾部, * 并计算待发送响应报文总的长度size; */ for (ln = in; ln; ln = ln->next) { cl = ngx_alloc_chain_link(r->pool); if (cl == NULL) { return NGX_ERROR; } cl->buf = ln->buf; *ll = cl;/* 由上面可知 ll=&r->out */ ll = &cl->next; ngx_log_debug7(NGX_LOG_DEBUG_EVENT, c->log, 0, "write new buf t:%d f:%d %p, pos %p, size: %z " "file: %O, size: %z", 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 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->buf->temporary, cl->buf->recycled, cl->buf->in_file, cl->buf->start, cl->buf->pos, cl->buf->last, cl->buf->file, cl->buf->file_pos, cl->buf->file_last); ngx_debug_point(); return NGX_ERROR; } #endif size += ngx_buf_size(cl->buf); if (cl->buf->flush || cl->buf->recycled) { flush = 1; } if (cl->buf->last_buf) { last = 1; } } *ll = NULL; ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0, "http write filter: l:%d f:%d s:%O", last, flush, size); /* 获取ngx_http_core_module模块的loc级别配置项结构体 */ clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); /* * avoid the output if there are no last buf, no flush point, * there are the incoming bufs and the size of all bufs * is smaller than "postpone_output" directive */ /* * 若out链表最后一块缓冲区last为空,且没有强制性刷新flush链表缓冲区out, * 且当前有待发响应报文in,但是待发送响应报文总的长度size小于预设可发送条件值postpone_output, * 则本次不能发送响应报文,继续保存在out链表缓冲区中,以待下次才发送; * 其中postpone_output预设值我们可以在配置文件nginx.conf中设置; */ if (!last && !flush && in && size < (off_t) clcf->postpone_output) { return NGX_OK; } /* * 检查当前连接上写事件的delayed标志位, * 若该标志位为1,表示需要延迟发送响应报文, * 因此,返回NGX_AGAIN,表示延迟发送; */ if (c->write->delayed) { c->buffered |= NGX_HTTP_WRITE_BUFFERED; return NGX_AGAIN; } if (size == 0 && !(c->buffered & NGX_LOWLEVEL_BUFFERED) && !(last && c->need_last_buf)) { if (last || flush) { for (cl = r->out; cl; /* void */) { ln = cl; cl = cl->next; ngx_free_chain(r->pool, ln); } r->out = NULL; c->buffered &= ~NGX_HTTP_WRITE_BUFFERED; return NGX_OK; } ngx_log_error(NGX_LOG_ALERT, c->log, 0, "the http output chain is empty"); ngx_debug_point(); return NGX_ERROR; } /* * 检查当前请求的限速标志位limit_rate, * 若该标志位为大于0,表示发送响应报文的速度不能超过limit_rate指定的速度; */ if (r->limit_rate) { if (r->limit_rate_after == 0) { r->limit_rate_after = clcf->limit_rate_after; } /* 计算发送速度是否超过限速值 */ limit = (off_t) r->limit_rate * (ngx_time() - r->start_sec + 1) - (c->sent - r->limit_rate_after); /* * 若当前发送响应报文的速度超过限速值,则写事件标志位delayed设为1, * 并把该写事件添加到定时器机制中,并且将buffered设置为可写状态, * 返回NGX_AGAIN,表示链表缓冲区out还保存剩余待发送的响应报文; */ if (limit <= 0) { c->write->delayed = 1; ngx_add_timer(c->write, (ngx_msec_t) (- limit * 1000 / r->limit_rate + 1)); c->buffered |= NGX_HTTP_WRITE_BUFFERED; return NGX_AGAIN; } if (clcf->sendfile_max_chunk && (off_t) clcf->sendfile_max_chunk < limit) { limit = clcf->sendfile_max_chunk; } } else { limit = clcf->sendfile_max_chunk; } /* 若不需要减速,或没有设置速度限制,则向客户端发送响应字符流 */ sent = c->sent; ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http write filter limit %O", limit); chain = c->send_chain(c, r->out, limit); ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http write filter %p", chain); if (chain == NGX_CHAIN_ERROR) { c->error = 1; return NGX_ERROR; } /* 再次检查limit_rate标志位 */ if (r->limit_rate) { nsent = c->sent; if (r->limit_rate_after) { sent -= r->limit_rate_after; if (sent < 0) { sent = 0; } nsent -= r->limit_rate_after; if (nsent < 0) { nsent = 0; } } /* 再次计算当前发送响应报文速度是否超过限制值 */ delay = (ngx_msec_t) ((nsent - sent) * 1000 / r->limit_rate); /* 若超过,需要限速,并把写事件添加到定时器机制中 */ if (delay > 0) { limit = 0; c->write->delayed = 1; ngx_add_timer(c->write, delay); } } if (limit && c->write->ready && c->sent - sent >= limit - (off_t) (2 * ngx_pagesize)) { c->write->delayed = 1; ngx_add_timer(c->write, 1); } /* 重新调整链表缓冲区out的情况,把已发送数据的缓冲区内存回收 */ for (cl = r->out; cl && cl != chain; /* void */) { ln = cl; cl = cl->next; ngx_free_chain(r->pool, ln); } /* 检查out链表缓冲区是否还有数据 */ r->out = chain; /* 若还有数据,返回NGX_AGAIN,表示还存在待发送的响应报文数据 */ if (chain) { c->buffered |= NGX_HTTP_WRITE_BUFFERED; return NGX_AGAIN; } c->buffered &= ~NGX_HTTP_WRITE_BUFFERED; if ((c->buffered & NGX_LOWLEVEL_BUFFERED) && r->postponed == NULL) { return NGX_AGAIN; } /* 若已发送全部数据则返回NGX_OK */ return NGX_OK; }
//遍历r->out上的每一个缓存块,根据缓存块里的数据类型调用不同的系统接口函数将数据写出到客户端。 ngx_chain_t * ngx_linux_sendfile_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit) { int tcp_nodelay; off_t send, prev_send; size_t file_size, sent; ssize_t n; ngx_err_t err; ngx_buf_t *file; ngx_event_t *wev; ngx_chain_t *cl; ngx_iovec_t header; struct iovec headers[NGX_IOVS_PREALLOCATE]; #if (NGX_THREADS) ngx_int_t rc; ngx_uint_t thread_handled, thread_complete; #endif wev = c->write; if (!wev->ready) { return in; } /* the maximum limit size is 2G-1 - the page size */ if (limit == 0 || limit > (off_t) (NGX_SENDFILE_MAXSIZE - ngx_pagesize)) { limit = NGX_SENDFILE_MAXSIZE - ngx_pagesize; } send = 0; header.iovs = headers; header.nalloc = NGX_IOVS_PREALLOCATE; for ( ;; ) { prev_send = send; #if (NGX_THREADS) thread_handled = 0; thread_complete = 0; #endif /* create the iovec and coalesce the neighbouring bufs */ cl = ngx_output_chain_to_iovec(&header, in, limit - send, c->log); if (cl == NGX_CHAIN_ERROR) { return NGX_CHAIN_ERROR; } send += header.size; /* set TCP_CORK if there is a header before a file */ if (c->tcp_nopush == NGX_TCP_NOPUSH_UNSET && header.count != 0 && cl && cl->buf->in_file) { /* the TCP_CORK and TCP_NODELAY are mutually exclusive */ if (c->tcp_nodelay == NGX_TCP_NODELAY_SET) { tcp_nodelay = 0; if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY, (const void *) &tcp_nodelay, sizeof(int)) == -1) { err = ngx_socket_errno; /* * there is a tiny chance to be interrupted, however, * we continue a processing with the TCP_NODELAY * and without the TCP_CORK */ if (err != NGX_EINTR) { wev->error = 1; ngx_connection_error(c, err, "setsockopt(TCP_NODELAY) failed"); return NGX_CHAIN_ERROR; } } else { c->tcp_nodelay = NGX_TCP_NODELAY_UNSET; ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0, "no tcp_nodelay"); } } if (c->tcp_nodelay == NGX_TCP_NODELAY_UNSET) { if (ngx_tcp_nopush(c->fd) == NGX_ERROR) { err = ngx_socket_errno; /* * there is a tiny chance to be interrupted, however, * we continue a processing without the TCP_CORK */ if (err != NGX_EINTR) { wev->error = 1; ngx_connection_error(c, err, ngx_tcp_nopush_n " failed"); return NGX_CHAIN_ERROR; } } else { c->tcp_nopush = NGX_TCP_NOPUSH_SET; ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0, "tcp_nopush"); } } } /* get the file buf */ if (header.count == 0 && cl && cl->buf->in_file && send < limit) { file = cl->buf; /* coalesce the neighbouring file bufs */ file_size = (size_t) ngx_chain_coalesce_file(&cl, limit - send); send += file_size; #if 1 if (file_size == 0) { ngx_debug_point(); return NGX_CHAIN_ERROR; } #endif #if (NGX_THREADS) if (file->file->thread_handler) { rc = ngx_linux_sendfile_thread(c, file, file_size, &sent); switch (rc) { case NGX_OK: thread_handled = 1; break; case NGX_DONE: thread_complete = 1; break; case NGX_AGAIN: break; default: /* NGX_ERROR */ return NGX_CHAIN_ERROR; } } else #endif { n = ngx_linux_sendfile(c, file, file_size); if (n == NGX_ERROR) { return NGX_CHAIN_ERROR; } sent = (n == NGX_AGAIN) ? 0 : n; } } else { n = ngx_writev(c, &header); if (n == NGX_ERROR) { return NGX_CHAIN_ERROR; } sent = (n == NGX_AGAIN) ? 0 : n; } c->sent += sent; in = ngx_chain_update_sent(in, sent); if ((size_t) (send - prev_send) != sent) { #if (NGX_THREADS) if (thread_handled) { return in; } if (thread_complete) { send = prev_send + sent; continue; } #endif wev->ready = 0; return in; } if (send >= limit || in == NULL) { return in; } } }
ngx_chain_t * ngx_udp_unix_sendmsg_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit) { ssize_t n; off_t send; ngx_chain_t *cl; ngx_event_t *wev; ngx_iovec_t vec; struct iovec iovs[NGX_IOVS_PREALLOCATE]; wev = c->write; if (!wev->ready) { return in; } #if (NGX_HAVE_KQUEUE) if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT) && wev->pending_eof) { (void) ngx_connection_error(c, wev->kq_errno, "kevent() reported about an closed connection"); wev->error = 1; return NGX_CHAIN_ERROR; } #endif /* the maximum limit size is the maximum size_t value - the page size */ if (limit == 0 || limit > (off_t) (NGX_MAX_SIZE_T_VALUE - ngx_pagesize)) { limit = NGX_MAX_SIZE_T_VALUE - ngx_pagesize; } send = 0; vec.iovs = iovs; vec.nalloc = NGX_IOVS_PREALLOCATE; for ( ;; ) { /* create the iovec and coalesce the neighbouring bufs */ cl = ngx_udp_output_chain_to_iovec(&vec, in, c->log); if (cl == NGX_CHAIN_ERROR) { return NGX_CHAIN_ERROR; } if (cl && cl->buf->in_file) { ngx_log_error(NGX_LOG_ALERT, c->log, 0, "file buf in sendmsg " "t:%d r:%d f:%d %p %p-%p %p %O-%O", cl->buf->temporary, cl->buf->recycled, cl->buf->in_file, cl->buf->start, cl->buf->pos, cl->buf->last, cl->buf->file, cl->buf->file_pos, cl->buf->file_last); ngx_debug_point(); return NGX_CHAIN_ERROR; } if (cl == in) { return in; } send += vec.size; n = ngx_sendmsg(c, &vec); if (n == NGX_ERROR) { return NGX_CHAIN_ERROR; } if (n == NGX_AGAIN) { wev->ready = 0; return in; } c->sent += n; in = ngx_chain_update_sent(in, n); if (send >= limit || in == NULL) { return in; } } }
static ngx_chain_t * ngx_udp_output_chain_to_iovec(ngx_iovec_t *vec, ngx_chain_t *in, ngx_log_t *log) { size_t total, size; u_char *prev; ngx_uint_t n, flush; ngx_chain_t *cl; struct iovec *iov; cl = in; iov = NULL; prev = NULL; total = 0; n = 0; flush = 0; for ( /* void */ ; in && !flush; in = in->next) { if (in->buf->flush || in->buf->last_buf) { flush = 1; } if (ngx_buf_special(in->buf)) { continue; } if (in->buf->in_file) { break; } if (!ngx_buf_in_memory(in->buf)) { ngx_log_error(NGX_LOG_ALERT, log, 0, "bad buf in output chain " "t:%d r:%d f:%d %p %p-%p %p %O-%O", in->buf->temporary, in->buf->recycled, in->buf->in_file, in->buf->start, in->buf->pos, in->buf->last, in->buf->file, in->buf->file_pos, in->buf->file_last); ngx_debug_point(); return NGX_CHAIN_ERROR; } size = in->buf->last - in->buf->pos; if (prev == in->buf->pos) { iov->iov_len += size; } else { if (n == vec->nalloc) { ngx_log_error(NGX_LOG_ALERT, log, 0, "too many parts in a datagram"); return NGX_CHAIN_ERROR; } iov = &vec->iovs[n++]; iov->iov_base = (void *) in->buf->pos; iov->iov_len = size; } prev = in->buf->pos + size; total += size; } if (!flush) { #if (NGX_SUPPRESS_WARN) vec->size = 0; vec->count = 0; #endif return cl; } vec->count = n; vec->size = total; return in; }
// 真正的向客户端发送数据,调用send_chain // 也由ngx_http_set_write_handler设置epoll的写事件触发 // 如果数据发送不完,就保存在r->out里,返回again // 需要再次发生可写事件才能发送 // 不是last、flush,且数据量较小(默认1460) // 那么这次就不真正调用write发送,减少系统调用的次数,提高性能 // 在此函数里处理限速 ngx_int_t ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in) { off_t size, sent, nsent, limit; ngx_uint_t last, flush, sync; ngx_msec_t delay; ngx_chain_t *cl, *ln, **ll, *chain; ngx_connection_t *c; ngx_http_core_loc_conf_t *clcf; // 获取连接对象 c = r->connection; // 如果连接有错误就不能发送数据 if (c->error) { return NGX_ERROR; } // 数据的长度, size = 0; // 是否flush的标志 flush = 0; // 是否sync的标志 sync = 0; // 是否是最后一块数据,即数据全部发送完毕 last = 0; // 请求里存储的待发送的数据链表 // 可能是之前因为again而未能发送出去 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) { 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 // 缓冲区0长度,但不是flush、sync等控制用缓冲区,报错 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->buf->temporary, cl->buf->recycled, cl->buf->in_file, cl->buf->start, cl->buf->pos, cl->buf->last, cl->buf->file, cl->buf->file_pos, cl->buf->file_last); ngx_debug_point(); return NGX_ERROR; } #endif // 累计数据长度 size += ngx_buf_size(cl->buf); // flush标志 if (cl->buf->flush || cl->buf->recycled) { flush = 1; } // sync标志 if (cl->buf->sync) { sync = 1; } // 发送结束的标志 if (cl->buf->last_buf) { last = 1; } } /* add the new chain to the existent one */ // 遍历新的数据链表,计算长度 // 把in链表里的数据挂到r->out链表后面 for (ln = in; ln; ln = ln->next) { // 分配一个新的链表节点 cl = ngx_alloc_chain_link(r->pool); if (cl == NULL) { return NGX_ERROR; } // 拷贝缓冲区,然后挂到out后面 cl->buf = ln->buf; *ll = cl; ll = &cl->next; ngx_log_debug7(NGX_LOG_DEBUG_EVENT, c->log, 0, "write new 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 // 缓冲区0长度,但不是flush、sync等控制用缓冲区,报错 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->buf->temporary, cl->buf->recycled, cl->buf->in_file, cl->buf->start, cl->buf->pos, cl->buf->last, cl->buf->file, cl->buf->file_pos, cl->buf->file_last); ngx_debug_point(); return NGX_ERROR; } #endif // 累计数据长度 size += ngx_buf_size(cl->buf); // flush标志 if (cl->buf->flush || cl->buf->recycled) { flush = 1; } // sync标志 if (cl->buf->sync) { sync = 1; } // 发送结束的标志 if (cl->buf->last_buf) { last = 1; } } // 链表的最后节点,必须设置为空指针 *ll = NULL; ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0, "http write filter: l:%ui f:%ui s:%O", last, flush, size); clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); /* * avoid the output if there are no last buf, no flush point, * there are the incoming bufs and the size of all bufs * is smaller than "postpone_output" directive */ // 不是last、flush,且数据量较小(默认1460) // 那么这次就不真正调用write发送,减少系统调用的次数,提高性能 if (!last && !flush && in && size < (off_t) clcf->postpone_output) { return NGX_OK; } // flush,用户要求立即发送 // last,所有的数据都集齐了,之后不会有新数据 // size > postpone_output,数据已经积累的足够多,应该发送了 // delayed表示需要限速,那么就暂不发送 if (c->write->delayed) { // 置标志位,表示连接有数据缓冲待发送 c->buffered |= NGX_HTTP_WRITE_BUFFERED; return NGX_AGAIN; } // 数据长度为0 if (size == 0 && !(c->buffered & NGX_LOWLEVEL_BUFFERED) && !(last && c->need_last_buf)) { // 释放r->out里的节点 if (last || flush || sync) { for (cl = r->out; cl; /* void */) { ln = cl; cl = cl->next; ngx_free_chain(r->pool, ln); } r->out = NULL; c->buffered &= ~NGX_HTTP_WRITE_BUFFERED; return NGX_OK; } ngx_log_error(NGX_LOG_ALERT, c->log, 0, "the http output chain is empty"); ngx_debug_point(); return NGX_ERROR; } // 处理限速,不研究 if (r->limit_rate) { if (r->limit_rate_after == 0) { r->limit_rate_after = clcf->limit_rate_after; } limit = (off_t) r->limit_rate * (ngx_time() - r->start_sec + 1) - (c->sent - r->limit_rate_after); if (limit <= 0) { c->write->delayed = 1; delay = (ngx_msec_t) (- limit * 1000 / r->limit_rate + 1); ngx_add_timer(c->write, delay); c->buffered |= NGX_HTTP_WRITE_BUFFERED; return NGX_AGAIN; } if (clcf->sendfile_max_chunk && (off_t) clcf->sendfile_max_chunk < limit) { limit = clcf->sendfile_max_chunk; } } else { // 不限速,使用配置的参数,默认是0,即不限制,尽量多发 limit = clcf->sendfile_max_chunk; } // 已经发送的字节数 sent = c->sent; ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http write filter limit %O", limit); // 调用send_chain发送out链表 // 实际上调用的是ngx_writev_chain // // 发送limit长度(字节数)的数据 // 如果事件not ready,即暂不可写,那么立即返回,无动作 // 要求缓冲区必须在内存里,否则报错 // 最后返回消费缓冲区之后的链表指针 // 发送出错、遇到again、发送完毕,这三种情况函数结束 // 返回的是最后发送到的链表节点指针 chain = c->send_chain(c, r->out, limit); ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http write filter %p", chain); if (chain == NGX_CHAIN_ERROR) { c->error = 1; return NGX_ERROR; } // 处理限速,不研究 if (r->limit_rate) { nsent = c->sent; if (r->limit_rate_after) { sent -= r->limit_rate_after; if (sent < 0) { sent = 0; } nsent -= r->limit_rate_after; if (nsent < 0) { nsent = 0; } } delay = (ngx_msec_t) ((nsent - sent) * 1000 / r->limit_rate); if (delay > 0) { limit = 0; c->write->delayed = 1; ngx_add_timer(c->write, delay); } } // 处理限速,不研究 if (limit && c->write->ready && c->sent - sent >= limit - (off_t) (2 * ngx_pagesize)) { c->write->delayed = 1; ngx_add_timer(c->write, 1); } // 在out链表里把已经发送过的节点都回收,供以后复用 for (cl = r->out; cl && cl != chain; /* void */) { ln = cl; cl = cl->next; ngx_free_chain(r->pool, ln); } // out指向新的位置 r->out = chain; // 不是空指针,表明还有数据没发完 if (chain) { // 置标志位,表示连接有数据缓冲待发送 c->buffered |= NGX_HTTP_WRITE_BUFFERED; return NGX_AGAIN; } // 已经发送完了 // 清除缓冲标志位 c->buffered &= ~NGX_HTTP_WRITE_BUFFERED; // NGX_LOWLEVEL_BUFFERED目前似乎在nginx里还没有用到 if ((c->buffered & NGX_LOWLEVEL_BUFFERED) && r->postponed == NULL) { return NGX_AGAIN; } return NGX_OK; }
// A slightly simplified version of ngx_http_sub_output static ngx_int_t ngx_http_secure_token_output(ngx_http_request_t *r, ngx_http_secure_token_ctx_t *ctx) { ngx_int_t rc; ngx_buf_t *b; ngx_chain_t *cl; #if 1 b = NULL; for (cl = ctx->out; cl; cl = cl->next) { ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "secure token out: %p %p", cl->buf, cl->buf->pos); if (cl->buf == b) { ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "the same buf was used in secure token"); ngx_debug_point(); return NGX_ERROR; } b = cl->buf; } #endif rc = ngx_http_next_body_filter(r, ctx->out); if (ctx->busy == NULL) { ctx->busy = ctx->out; } else { for (cl = ctx->busy; cl->next; cl = cl->next) { /* void */ } cl->next = ctx->out; } ctx->out = NULL; ctx->last_out = &ctx->out; while (ctx->busy) { cl = ctx->busy; b = cl->buf; if (ngx_buf_size(b) != 0) { break; } if (b->shadow) { b->shadow->pos = b->shadow->last; } ctx->busy = cl->next; if (ngx_buf_in_memory(b) || b->in_file) { /* add data bufs only to the free buf chain */ cl->next = ctx->free; ctx->free = cl; } } return rc; }
// 缓冲区链表转换为iovec结构体 // 输出参数vec,存储iovec,输入参数in是nginx的缓冲区链表 // limit,发送数据的限制长度 // 要求缓冲区必须在内存里,否则报错 // 最后返回消费缓冲区之后的链表指针 ngx_chain_t * ngx_output_chain_to_iovec(ngx_iovec_t *vec, ngx_chain_t *in, size_t limit, ngx_log_t *log) { size_t total, size; u_char *prev; ngx_uint_t n; struct iovec *iov; // 指向vec里的某个元素 iov = NULL; // 缓冲区的字节指针,由于优化 prev = NULL; // 发送的总字节数 total = 0; // vec里的数组序号 n = 0; // 填满vec数组,或者字节数达到limit的限制 // 每处理完一个缓冲区指针就后移 for ( /* void */ ; in && total < limit; in = in->next) { // 忽略flush、sync、eof等控制用特殊缓冲区 if (ngx_buf_special(in->buf)) { continue; } // 不考虑磁盘文件 if (in->buf->in_file) { break; } // 要求缓冲区必须在内存里,否则报错 if (!ngx_buf_in_memory(in->buf)) { ngx_log_error(NGX_LOG_ALERT, log, 0, "bad buf in output chain " "t:%d r:%d f:%d %p %p-%p %p %O-%O", in->buf->temporary, in->buf->recycled, in->buf->in_file, in->buf->start, in->buf->pos, in->buf->last, in->buf->file, in->buf->file_pos, in->buf->file_last); ngx_debug_point(); return NGX_CHAIN_ERROR; } // 获得缓冲区内数据长度 size = in->buf->last - in->buf->pos; // 如果当前缓冲区的大小超过了最后的限制,那么只发一部分 if (size > limit - total) { size = limit - total; } // 这里是一种特殊情况,也可能很常见 // 两个buf,它们实际上指向了一块连续的内存 // 即buf1的last是buf2的pos // 所以nginx进行优化,不需要赋值,直接加上长度 // 节约一个iov数组元素 if (prev == in->buf->pos) { iov->iov_len += size; } else { // 不是连续的内存,就要使用一个iov结构体 // vec里的数组已经填满了 if (n == vec->nalloc) { break; } // iov指针指向vec里的第n个数组,然后n加1 iov = &vec->iovs[n++]; // iov结构里的数据指针和数据长度 iov->iov_base = (void *) in->buf->pos; iov->iov_len = size; } // prev指针移动 prev = in->buf->pos + size; // 总字节数增加,当大于等于limit时就结束循环 total += size; } // 如果不连续的内存很多,那么n就是vec->nalloc // 如果limit比较小,那么n就小于vec->nalloc vec->count = n; // size是总字节数,受limit和n的限制 // 不一定正好是limit vec->size = total; // 最后返回消费缓冲区之后的链表指针 return in; }
/* ·¢ËÍchain buf */ ngx_int_t ngx_output_chain(ngx_output_chain_ctx_t *ctx, ngx_chain_t *in) { off_t bsize; ngx_int_t rc, last; ngx_chain_t *cl, *out, **last_out; if (ctx->in == NULL && ctx->busy == NULL #if (NGX_HAVE_FILE_AIO || NGX_THREADS) && !ctx->aio #endif ) { /* * the short path for the case when the ctx->in and ctx->busy chains * are empty, the incoming chain is empty too or has the single buf * that does not require the copy */ if (in == NULL) { return ctx->output_filter(ctx->filter_ctx, in); } if (in->next == NULL #if (NGX_SENDFILE_LIMIT) && !(in->buf->in_file && in->buf->file_last > NGX_SENDFILE_LIMIT) #endif && ngx_output_chain_as_is(ctx, in->buf)) { return ctx->output_filter(ctx->filter_ctx, in); } } /* add the incoming buf to the chain ctx->in */ if (in) { if (ngx_output_chain_add_copy(ctx->pool, &ctx->in, in) == NGX_ERROR) { return NGX_ERROR; } } out = NULL; last_out = &out; last = NGX_NONE; for ( ;; ) { #if (NGX_HAVE_FILE_AIO || NGX_THREADS) if (ctx->aio) { return NGX_AGAIN; } #endif while (ctx->in) { /* * cycle while there are the ctx->in bufs * and there are the free output bufs to copy in */ bsize = ngx_buf_size(ctx->in->buf); if (bsize == 0 && !ngx_buf_special(ctx->in->buf)) { ngx_log_error(NGX_LOG_ALERT, ctx->pool->log, 0, "zero size buf in output " "t:%d r:%d f:%d %p %p-%p %p %O-%O", ctx->in->buf->temporary, ctx->in->buf->recycled, ctx->in->buf->in_file, ctx->in->buf->start, ctx->in->buf->pos, ctx->in->buf->last, ctx->in->buf->file, ctx->in->buf->file_pos, ctx->in->buf->file_last); ngx_debug_point(); ctx->in = ctx->in->next; continue; } if (ngx_output_chain_as_is(ctx, ctx->in->buf)) { /* move the chain link to the output chain */ cl = ctx->in; ctx->in = cl->next; *last_out = cl; last_out = &cl->next; cl->next = NULL; continue; } if (ctx->buf == NULL) { rc = ngx_output_chain_align_file_buf(ctx, bsize); if (rc == NGX_ERROR) { return NGX_ERROR; } if (rc != NGX_OK) { if (ctx->free) { /* get the free buf */ cl = ctx->free; ctx->buf = cl->buf; ctx->free = cl->next; ngx_free_chain(ctx->pool, cl); } else if (out || ctx->allocated == ctx->bufs.num) { break; } else if (ngx_output_chain_get_buf(ctx, bsize) != NGX_OK) { return NGX_ERROR; } } } rc = ngx_output_chain_copy_buf(ctx); if (rc == NGX_ERROR) { return rc; } if (rc == NGX_AGAIN) { if (out) { break; } return rc; } /* delete the completed buf from the ctx->in chain */ if (ngx_buf_size(ctx->in->buf) == 0) { ctx->in = ctx->in->next; } cl = ngx_alloc_chain_link(ctx->pool); if (cl == NULL) { return NGX_ERROR; } cl->buf = ctx->buf; cl->next = NULL; *last_out = cl; last_out = &cl->next; ctx->buf = NULL; } if (out == NULL && last != NGX_NONE) { if (ctx->in) { return NGX_AGAIN; } return last; } last = ctx->output_filter(ctx->filter_ctx, out); if (last == NGX_ERROR || last == NGX_DONE) { return last; } ngx_chain_update_chains(ctx->pool, &ctx->free, &ctx->busy, &out, ctx->tag); last_out = &out; } }
// 发送limit长度(字节数)的数据 // limit有限制,但基本上可以说是无限大了 // 如果事件not ready,即暂不可写,那么立即返回,无动作 // 要求缓冲区必须在内存里,否则报错 // 最后返回消费缓冲区之后的链表指针 // 发送出错、遇到again、发送完毕,这三种情况函数结束 // 返回的是最后发送到的链表节点指针 ngx_chain_t * ngx_writev_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit) { ssize_t n, sent; off_t send, prev_send; ngx_chain_t *cl; ngx_event_t *wev; ngx_iovec_t vec; struct iovec iovs[NGX_IOVS_PREALLOCATE]; // 从连接获取写事件 wev = c->write; // 如果事件not ready,即暂不可写,那么立即返回,无动作 if (!wev->ready) { return in; } #if (NGX_HAVE_KQUEUE) if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT) && wev->pending_eof) { (void) ngx_connection_error(c, wev->kq_errno, "kevent() reported about an closed connection"); wev->error = 1; return NGX_CHAIN_ERROR; } #endif /* the maximum limit size is the maximum size_t value - the page size */ // 由脚本生成,ngx_auto_config.h:#define NGX_MAX_SIZE_T_VALUE 9223372036854775807LL // limit有限制,但基本上可以说是无限大了 if (limit == 0 || limit > (off_t) (NGX_MAX_SIZE_T_VALUE - ngx_pagesize)) { limit = NGX_MAX_SIZE_T_VALUE - ngx_pagesize; } // 发送的字节数,初始化为0 // 注意不是sent send = 0; // 设置iovs,指向函数内部的数组iovs,长度是NGX_IOVS_PREALLOCATE,通常是64 vec.iovs = iovs; vec.nalloc = NGX_IOVS_PREALLOCATE; // 成功发送了limit字节,或者缓冲区链表已经结束 // 内核发送缓冲区满,不能再发送 // 那么发送结束 for ( ;; ) { // 暂存之前发送的字节数 prev_send = send; /* create the iovec and coalesce the neighbouring bufs */ // 缓冲区链表转换为iovec结构体 // 输出参数vec,存储iovec,输入参数in是nginx的缓冲区链表 // limit,发送数据的限制长度 // 要求缓冲区必须在内存里,否则报错 // 最后返回消费缓冲区之后的链表指针 cl = ngx_output_chain_to_iovec(&vec, in, limit - send, c->log); // 要求缓冲区必须在内存里,否则报错 if (cl == NGX_CHAIN_ERROR) { return NGX_CHAIN_ERROR; } // 要求缓冲区必须在内存里,否则报错 if (cl && cl->buf->in_file) { ngx_log_error(NGX_LOG_ALERT, c->log, 0, "file buf in writev " "t:%d r:%d f:%d %p %p-%p %p %O-%O", cl->buf->temporary, cl->buf->recycled, cl->buf->in_file, cl->buf->start, cl->buf->pos, cl->buf->last, cl->buf->file, cl->buf->file_pos, cl->buf->file_last); ngx_debug_point(); return NGX_CHAIN_ERROR; } // vec.size里存储的是在iovs里的总字节数 // 增加发送的字节数 send += vec.size; // 封装系统调用writev,发送多个内存块 // again,暂时不可写,需要等待事件可写再重试,返回again // 被中断,需要立即重试,可能就成功了 // 其他的就是错误 n = ngx_writev(c, &vec); // 不可恢复的错误 if (n == NGX_ERROR) { return NGX_CHAIN_ERROR; } // 如果是again,发送失败,已发送字节不增加 sent = (n == NGX_AGAIN) ? 0 : n; // 连接里的已发送字节数增加 c->sent += sent; // 根据已经实际发送的字节数更新链表 // 已经发送的缓冲区会清空 // 最后返回处理之后的链表指针 // 如果没有发送(again)就直接返回 in = ngx_chain_update_sent(in, sent); // 两者相减,判断是否完全发送了数据 // 不完全,只发送了部分,也就是说内核写缓冲区满,写不可用 if (send - prev_send != sent) { // 暂时不可写,需要等待下次写事件发生才能写 wev->ready = 0; return in; } // 成功发送了limit字节,或者缓冲区链表已经结束 // 那么发送结束 if (send >= limit || in == NULL) { return in; } // limit字节很多,这次没有发送完 // 需要再从循环开头取数据发送 } }
ngx_int_t ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in) { off_t size, sent, nsent, limit; ngx_uint_t last, flush, sync; ngx_msec_t delay; ngx_chain_t *cl, *ln, **ll, *chain; ngx_connection_t *c; ngx_http_core_loc_conf_t *clcf; 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) { 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 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->buf->temporary, cl->buf->recycled, cl->buf->in_file, cl->buf->start, cl->buf->pos, cl->buf->last, cl->buf->file, cl->buf->file_pos, cl->buf->file_last); ngx_debug_point(); return NGX_ERROR; } #endif size += ngx_buf_size(cl->buf); if (cl->buf->flush || cl->buf->recycled) { flush = 1; } if (cl->buf->sync) { sync = 1; } if (cl->buf->last_buf) { last = 1; } } /* add the new chain to the existent one */ for (ln = in; ln; ln = ln->next) { cl = ngx_alloc_chain_link(r->pool); if (cl == NULL) { return NGX_ERROR; } cl->buf = ln->buf; *ll = cl; ll = &cl->next; ngx_log_debug7(NGX_LOG_DEBUG_EVENT, c->log, 0, "write new 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 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->buf->temporary, cl->buf->recycled, cl->buf->in_file, cl->buf->start, cl->buf->pos, cl->buf->last, cl->buf->file, cl->buf->file_pos, cl->buf->file_last); ngx_debug_point(); return NGX_ERROR; } #endif size += ngx_buf_size(cl->buf); if (cl->buf->flush || cl->buf->recycled) { flush = 1; } if (cl->buf->sync) { sync = 1; } if (cl->buf->last_buf) { last = 1; } } *ll = NULL; ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0, "http write filter: l:%d f:%d s:%O", last, flush, size); clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module); /* * avoid the output if there are no last buf, no flush point, * there are the incoming bufs and the size of all bufs * is smaller than "postpone_output" directive */ if (!last && !flush && in && size < (off_t) clcf->postpone_output) { return NGX_OK; } if (c->write->delayed) { c->buffered |= NGX_HTTP_WRITE_BUFFERED; return NGX_AGAIN; } if (size == 0 && !(c->buffered & NGX_LOWLEVEL_BUFFERED) && !(last && c->need_last_buf)) { if (last || flush || sync) { for (cl = r->out; cl; /* void */) { ln = cl; cl = cl->next; ngx_free_chain(r->pool, ln); } r->out = NULL; c->buffered &= ~NGX_HTTP_WRITE_BUFFERED; return NGX_OK; } ngx_log_error(NGX_LOG_ALERT, c->log, 0, "the http output chain is empty"); ngx_debug_point(); return NGX_ERROR; } if (r->limit_rate) { if (r->limit_rate_after == 0) { r->limit_rate_after = clcf->limit_rate_after; } limit = (off_t) r->limit_rate * (ngx_time() - r->start_sec + 1) - (c->sent - r->limit_rate_after); if (limit <= 0) { c->write->delayed = 1; delay = (ngx_msec_t) (- limit * 1000 / r->limit_rate + 1); ngx_add_timer(c->write, delay); c->buffered |= NGX_HTTP_WRITE_BUFFERED; return NGX_AGAIN; } if (clcf->sendfile_max_chunk && (off_t) clcf->sendfile_max_chunk < limit) { limit = clcf->sendfile_max_chunk; } } else { limit = clcf->sendfile_max_chunk; } sent = c->sent; ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http write filter limit %O", limit); chain = c->send_chain(c, r->out, limit); /* ngx_linux_sendfile_chain */ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http write filter %p", chain); if (chain == NGX_CHAIN_ERROR) { c->error = 1; return NGX_ERROR; } if (r->limit_rate) { nsent = c->sent; if (r->limit_rate_after) { sent -= r->limit_rate_after; if (sent < 0) { sent = 0; } nsent -= r->limit_rate_after; if (nsent < 0) { nsent = 0; } } delay = (ngx_msec_t) ((nsent - sent) * 1000 / r->limit_rate); if (delay > 0) { limit = 0; c->write->delayed = 1; ngx_add_timer(c->write, delay); } } if (limit && c->write->ready && c->sent - sent >= limit - (off_t) (2 * ngx_pagesize)) { c->write->delayed = 1; ngx_add_timer(c->write, 1); } for (cl = r->out; cl && cl != chain; /* void */) { ln = cl; cl = cl->next; ngx_free_chain(r->pool, ln); } r->out = chain; if (chain) { c->buffered |= NGX_HTTP_WRITE_BUFFERED; return NGX_AGAIN; } c->buffered &= ~NGX_HTTP_WRITE_BUFFERED; if ((c->buffered & NGX_LOWLEVEL_BUFFERED) && r->postponed == NULL) { return NGX_AGAIN; } return NGX_OK; }
ngx_int_t ngx_tcp_chain_writer(ngx_tcp_session_t *s) { ngx_chain_writer_ctx_t *ctx; ngx_chain_t *in; ngx_tcp_output_again_t *again_ptr; off_t size; ngx_chain_t *cl; ngx_connection_t *c; ctx = s->output_ctx->filter_ctx; in = s->output_buffer_chain; again_ptr = &s->output_again; c = ctx->connection; again_ptr->out_chain_arr[again_ptr->ix_w++] = in; again_ptr->ix_w &= (OUTPUT_CHAIN_AGAIN_SIZE - 1); //if output_again_arr is full, return error; if (again_ptr->ix_w == again_ptr->ix_r) { ngx_log_error(NGX_LOG_ERR, c->log, 0, "ngx_tcp_chain_writer|output_again_arr is full|ix_w=%d|ix_r=%d", again_ptr->ix_w, again_ptr->ix_r); return NGX_ERROR; } while (1) { if (NULL == again_ptr->out_chain_arr[again_ptr->ix_r]) { // ix_r = 0; // ix_w = 0; break; } ctx->out = again_ptr->out_chain_arr[again_ptr->ix_r]; ngx_log_debug1(NGX_LOG_DEBUG_CORE, c->log, 0, "tcp chain writer in: %p", ctx->out); size = 0; for (cl = ctx->out; cl; cl = cl->next) { #if 1 if (ngx_buf_size(cl->buf) == 0 && !ngx_buf_special(cl->buf)) { ngx_debug_point(); } #endif size += ngx_buf_size(cl->buf); } if (size == 0 && !c->buffered) { again_ptr->out_chain_arr[again_ptr->ix_r] = NULL; again_ptr->ix_r++; again_ptr->ix_r &= (OUTPUT_CHAIN_AGAIN_SIZE - 1); continue; } again_ptr->out_chain_arr[again_ptr->ix_r] = c->send_chain(c, again_ptr->out_chain_arr[again_ptr->ix_r], ctx->limit); ctx->out = again_ptr->out_chain_arr[again_ptr->ix_r]; ngx_log_debug1(NGX_LOG_DEBUG_CORE, c->log, 0, "chain writer out: %p", ctx->out); if (ctx->out == NGX_CHAIN_ERROR) { ngx_log_error(NGX_LOG_ERR, c->log, 0, "ngx_tcp_chain_writer|out_chain_arr[%d]==NGX_CHAIN_ERROR", again_ptr->ix_r); again_ptr->out_chain_arr[again_ptr->ix_r] = NULL; again_ptr->ix_r++; again_ptr->ix_r &= (OUTPUT_CHAIN_AGAIN_SIZE - 1); return NGX_ERROR; } if (ctx->out == NULL) { ctx->last = &ctx->out; if (!c->buffered) { again_ptr->ix_r++; again_ptr->ix_r &= (OUTPUT_CHAIN_AGAIN_SIZE - 1); //return NGX_OK; continue; } } ngx_log_error(NGX_LOG_DEBUG, c->log, 0, "ngx_tcp_chain_writer|again_ptr->out_chain_arr[%d] NGX_AGAIN", again_ptr->ix_r); return NGX_AGAIN; } return NGX_OK; }
static ngx_int_t ngx_stream_write_filter(ngx_stream_session_t *s, ngx_chain_t *in, ngx_uint_t from_upstream) { off_t size; ngx_uint_t last, flush, sync; ngx_chain_t *cl, *ln, **ll, **out, *chain; ngx_connection_t *c; ngx_stream_write_filter_ctx_t *ctx; ctx = ngx_stream_get_module_ctx(s, ngx_stream_write_filter_module); if (ctx == NULL) { ctx = ngx_pcalloc(s->connection->pool, sizeof(ngx_stream_write_filter_ctx_t)); if (ctx == NULL) { return NGX_ERROR; } ngx_stream_set_ctx(s, ctx, ngx_stream_write_filter_module); } if (from_upstream) { c = s->connection; out = &ctx->from_upstream; } else { c = s->upstream->peer.connection; out = &ctx->from_downstream; } if (c->error) { return NGX_ERROR; } size = 0; flush = 0; sync = 0; last = 0; ll = out; /* find the size, the flush point and the last link of the saved chain */ for (cl = *out; cl; cl = cl->next) { 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 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->buf->temporary, cl->buf->recycled, cl->buf->in_file, cl->buf->start, cl->buf->pos, cl->buf->last, cl->buf->file, cl->buf->file_pos, cl->buf->file_last); ngx_debug_point(); return NGX_ERROR; } #endif size += ngx_buf_size(cl->buf); if (cl->buf->flush || cl->buf->recycled) { flush = 1; } if (cl->buf->sync) { sync = 1; } if (cl->buf->last_buf) { last = 1; } } /* add the new chain to the existent one */ for (ln = in; ln; ln = ln->next) { cl = ngx_alloc_chain_link(c->pool); if (cl == NULL) { return NGX_ERROR; } cl->buf = ln->buf; *ll = cl; ll = &cl->next; ngx_log_debug7(NGX_LOG_DEBUG_EVENT, c->log, 0, "write new 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 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->buf->temporary, cl->buf->recycled, cl->buf->in_file, cl->buf->start, cl->buf->pos, cl->buf->last, cl->buf->file, cl->buf->file_pos, cl->buf->file_last); ngx_debug_point(); return NGX_ERROR; } #endif size += ngx_buf_size(cl->buf); if (cl->buf->flush || cl->buf->recycled) { flush = 1; } if (cl->buf->sync) { sync = 1; } if (cl->buf->last_buf) { last = 1; } } *ll = NULL; ngx_log_debug3(NGX_LOG_DEBUG_STREAM, c->log, 0, "stream write filter: l:%ui f:%ui s:%O", last, flush, size); if (size == 0 && !(c->buffered & NGX_LOWLEVEL_BUFFERED) && !(last && c->need_last_buf)) { if (last || flush || sync) { for (cl = *out; cl; /* void */) { ln = cl; cl = cl->next; ngx_free_chain(c->pool, ln); } *out = NULL; c->buffered &= ~NGX_STREAM_WRITE_BUFFERED; return NGX_OK; } ngx_log_error(NGX_LOG_ALERT, c->log, 0, "the stream output chain is empty"); ngx_debug_point(); return NGX_ERROR; } chain = c->send_chain(c, *out, 0); ngx_log_debug1(NGX_LOG_DEBUG_STREAM, c->log, 0, "stream write filter %p", chain); if (chain == NGX_CHAIN_ERROR) { c->error = 1; return NGX_ERROR; } for (cl = *out; cl && cl != chain; /* void */) { ln = cl; cl = cl->next; ngx_free_chain(c->pool, ln); } *out = chain; if (chain) { if (c->shared) { ngx_log_error(NGX_LOG_ALERT, c->log, 0, "shared connection is busy"); return NGX_ERROR; } c->buffered |= NGX_STREAM_WRITE_BUFFERED; return NGX_AGAIN; } c->buffered &= ~NGX_STREAM_WRITE_BUFFERED; if (c->buffered & NGX_LOWLEVEL_BUFFERED) { return NGX_AGAIN; } return NGX_OK; }
static ngx_int_t ngx_http_spdy_v3_body_filter(ngx_http_request_t *r, ngx_chain_t *in) { off_t total, size; ngx_buf_t *b; ngx_chain_t *cl, *tl, *ll, *out, **ln; ngx_http_spdy_stream_t *stream; ngx_http_spdy_srv_conf_t *sscf; ngx_http_spdy_out_frame_t *frame; total = 0; size = 0; out = NULL; stream = r->spdy_stream; if (stream == NULL) { return ngx_http_next_body_filter(r, in); } ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "spdy body filter \"%V?%V\"", &r->uri, &r->args); if (in == NULL || r->header_only) { if (stream->pending && (stream->send_window_size > 0)) { goto output; } if (stream->waiting) { return NGX_AGAIN; } if (!stream->pending) { r->connection->buffered &= ~NGX_SPDY_WRITE_BUFFERED; } return NGX_OK; } ln = &out; ll = in; for ( ;; ) { b = ll->buf; #if 1 if (ngx_buf_size(b) == 0 && !ngx_buf_special(b)) { ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0, "zero size buf in spdy body filter " "t:%d r:%d f:%d %p %p-%p %p %O-%O", b->temporary, b->recycled, b->in_file, b->start, b->pos, b->last, b->file, b->file_pos, b->file_last); ngx_debug_point(); return NGX_ERROR; } #endif cl = ngx_alloc_chain_link(r->pool); if (cl == NULL) { return NGX_ERROR; } cl->next = NULL; size += ngx_buf_size(b); cl->buf = b; *ln = cl; ln = &cl->next; if (ll->next == NULL) { break; } ll = ll->next; } output: total = size; if (stream->pending) { for (tl = stream->pending; ;tl = tl->next) { total += ngx_buf_size(tl->buf); if (tl->next == NULL) { break; } } tl->next = out; } else { stream->pending = out; } ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "spdy id=%ui, send_window_size=%i, total=%O", stream->id, stream->send_window_size, total); if (stream->send_window_size == 0) { ngx_log_error(NGX_LOG_NOTICE, r->connection->log, 0, "zero send windows size"); r->connection->buffered |= NGX_SPDY_WRITE_BUFFERED; return NGX_AGAIN; } else if (total > stream->send_window_size) { out = ngx_http_spdy_split_chain(stream, stream->send_window_size); if (out == NULL) { return NGX_ERROR; } r->connection->buffered |= NGX_SPDY_WRITE_BUFFERED; stream->send_window_size = 0; } else { out = stream->pending; stream->pending = NULL; stream->send_window_size -= total; } size = 0; cl = out; for (;;) { size += ngx_buf_size(cl->buf); ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "spdy, cl=%p, cl->buf=%p, size=%uz, fin:%d", cl, cl->buf, ngx_buf_size(cl->buf), cl->buf->last_buf); if (cl->next == NULL) { b = cl->buf; break; } cl = cl->next; } frame = ngx_http_spdy_filter_get_data_frame(stream, (size_t) size, b->last_buf, out, cl); if (frame == NULL) { return NGX_ERROR; } ngx_http_spdy_queue_frame(stream->connection, frame); stream->waiting++; r->main->blocked++; sscf = ngx_http_get_module_srv_conf(stream->connection->http_connection->conf_ctx, ngx_http_spdy_module); if (!sscf->flow_control) { stream->send_window_size = NGX_SPDY_TMP_WINDOW_SIZE; } return ngx_http_spdy_filter_send(r->connection, stream); }