diff --git a/src/event/ngx_event.c b/src/event/ngx_event.c index b50b8739c..7bfda0448 100644 --- a/src/event/ngx_event.c +++ b/src/event/ngx_event.c @@ -139,6 +139,7 @@ ngx_module_t ngx_event_core_module = { static ngx_int_t ngx_event_module_init(ngx_cycle_t *cycle) { +#if !(WIN32) ngx_core_conf_t *ccf; ngx_event_conf_t *ecf; @@ -163,6 +164,7 @@ static ngx_int_t ngx_event_module_init(ngx_cycle_t *cycle) "mmap(MAP_ANON|MAP_SHARED) failed"); return NGX_ERROR; } +#endif return NGX_OK; } diff --git a/src/event/ngx_event_pipe.c b/src/event/ngx_event_pipe.c index f2545171a..d7136bff5 100644 --- a/src/event/ngx_event_pipe.c +++ b/src/event/ngx_event_pipe.c @@ -100,7 +100,6 @@ int ngx_event_pipe_read_upstream(ngx_event_pipe_t *p) /* use the pre-read hunks if they exist */ - p->read = 1; chain = p->preread_hunks; p->preread_hunks = NULL; n = p->preread_size; @@ -108,6 +107,10 @@ int ngx_event_pipe_read_upstream(ngx_event_pipe_t *p) ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0, "pipe preread: %d", n); + if (n) { + p->read = 1; + } + } else { /* @@ -286,6 +289,35 @@ int ngx_event_pipe_read_upstream(ngx_event_pipe_t *p) p->free_raw_hunks = cl; } +#if (NGX_DEBUG0) + + if (p->in || p->busy || p->free_raw_hunks) { + ngx_log_debug0(NGX_LOG_DEBUG_EVENT, p->log, 0, "pipe buf"); + } + + for (cl = p->in; cl; cl = cl->next) { + ngx_log_debug3(NGX_LOG_DEBUG_EVENT, p->log, 0, + "pipe buf in " PTR_FMT ", pos " PTR_FMT ", size: %d", + cl->hunk->start, cl->hunk->pos, + cl->hunk->last - cl->hunk->pos); + } + + for (cl = p->busy; cl; cl = cl->next) { + ngx_log_debug3(NGX_LOG_DEBUG_EVENT, p->log, 0, + "pipe buf busy " PTR_FMT ", pos " PTR_FMT ", size: %d", + cl->hunk->start, cl->hunk->pos, + cl->hunk->last - cl->hunk->pos); + } + + for (cl = p->free_raw_hunks; cl; cl = cl->next) { + ngx_log_debug3(NGX_LOG_DEBUG_EVENT, p->log, 0, + "pipe buf free " PTR_FMT ", last " PTR_FMT ", size: %d", + cl->hunk->start, cl->hunk->last, + cl->hunk->end - cl->hunk->last); + } + +#endif + if ((p->upstream_eof || p->upstream_error) && p->free_raw_hunks) { /* STUB */ p->free_raw_hunks->hunk->num = p->num++; @@ -315,7 +347,8 @@ int ngx_event_pipe_read_upstream(ngx_event_pipe_t *p) int ngx_event_pipe_write_to_downstream(ngx_event_pipe_t *p) { - size_t bsize, to_write; + size_t bsize; + ngx_uint_t flush; ngx_hunk_t *h; ngx_chain_t *out, **ll, *cl, *tl; @@ -368,44 +401,29 @@ int ngx_event_pipe_write_to_downstream(ngx_event_pipe_t *p) break; } - /* - * bsize is the size of the busy hunks, - * to_write is the size of data in these hunks that - * would be written to a socket - */ + /* bsize is the size of the busy hunks */ bsize = 0; - to_write = 0; -#if 0 - if (!(p->upstream_eof || p->upstream_error || p->upstream_done)) { -#endif - for (cl = p->busy; cl; cl = cl->next) { - bsize += cl->hunk->end - cl->hunk->start; - to_write += ngx_hunk_size(cl->hunk); - } -#if 0 + for (cl = p->busy; cl; cl = cl->next) { + bsize += cl->hunk->end - cl->hunk->start; } -#endif + + ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0, + "pipe write busy: " SIZE_T_FMT, bsize); out = NULL; ll = NULL; + flush = 0; for ( ;; ) { if (p->out) { cl = p->out; -#if 0 - if (!(p->upstream_eof || p->upstream_error || p->upstream_done) - && (bsize + ngx_hunk_size(cl->hunk) > p->busy_size)) - { - break; - } -#else if (bsize + ngx_hunk_size(cl->hunk) > p->busy_size) { + flush = 1; break; } -#endif p->out = p->out->next; ngx_event_pipe_free_shadow_raw_hunk(&p->free_raw_hunks, @@ -414,17 +432,10 @@ int ngx_event_pipe_write_to_downstream(ngx_event_pipe_t *p) } else if (!p->cachable && p->in) { cl = p->in; -#if 0 - if (!(p->upstream_eof || p->upstream_error || p->upstream_done) - && (bsize + ngx_hunk_size(cl->hunk) > p->busy_size)) - { - break; - } -#else if (bsize + ngx_hunk_size(cl->hunk) > p->busy_size) { + flush = 1; break; } -#endif p->in = p->in->next; @@ -437,22 +448,13 @@ int ngx_event_pipe_write_to_downstream(ngx_event_pipe_t *p) ngx_chain_add_link(out, ll, cl); } - if (out == NULL) { - - ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0, - "pipe busy hunk data to write: %d", to_write); - - if (!(p->upstream_blocked && to_write)) { - break; - } + ngx_log_debug2(NGX_LOG_DEBUG_EVENT, p->log, 0, + "pipe write: out:" PTR_FMT ", f:%d", out, flush); + if (out == NULL && !flush) { + break; } - /* - * if the upstream is blocked and there are the busy hunks - * to write then write these hunks - */ - if (p->output_filter(p->output_ctx, out) == NGX_ERROR) { p->downstream_error = 1; return ngx_event_pipe_drain_chains(p); @@ -533,8 +535,9 @@ static int ngx_event_pipe_write_chain_to_temp_file(ngx_event_pipe_t *p) do { hsize = cl->hunk->last - cl->hunk->pos; - ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0, - "pipe hunk size: %d", hsize); + ngx_log_debug3(NGX_LOG_DEBUG_EVENT, p->log, 0, + "pipe buf " PTR_FMT ", pos " PTR_FMT ", size: %d", + cl->hunk->start, cl->hunk->pos, hsize); if ((size + hsize > p->temp_file_write_size) || (p->temp_file->offset + size + hsize > p->max_temp_file_size)) diff --git a/src/event/ngx_event_timer.h b/src/event/ngx_event_timer.h index 0eae57043..bf5d15515 100644 --- a/src/event/ngx_event_timer.h +++ b/src/event/ngx_event_timer.h @@ -77,7 +77,14 @@ ngx_inline static void ngx_event_add_timer(ngx_event_t *ev, ngx_msec_t timer) #endif if (ev->timer_set) { - if (key - ev->rbtree_key < 50) { + + /* + * Use the previous timer value if a difference between them is less + * then 100 milliseconds. It allows to minimize the rbtree operations + * for the fast connections. + */ + + if (key - ev->rbtree_key < 100 / NGX_TIMER_RESOLUTION) { ngx_log_debug3(NGX_LOG_DEBUG_EVENT, ev->log, 0, "event timer: %d, old: %d, new: %d", ngx_event_ident(ev->data), ev->rbtree_key, key); diff --git a/src/http/modules/proxy/ngx_http_proxy_handler.c b/src/http/modules/proxy/ngx_http_proxy_handler.c index 0dba23f9b..95d565442 100644 --- a/src/http/modules/proxy/ngx_http_proxy_handler.c +++ b/src/http/modules/proxy/ngx_http_proxy_handler.c @@ -814,6 +814,8 @@ static char *ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *prev = parent; ngx_http_proxy_loc_conf_t *conf = child; + size_t size; + ngx_conf_merge_msec_value(conf->connect_timeout, prev->connect_timeout, 60000); ngx_conf_merge_msec_value(conf->send_timeout, prev->send_timeout, 30000); @@ -823,22 +825,65 @@ static char *ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, ngx_conf_merge_value(conf->add_x_forwarded_for, prev->add_x_forwarded_for, 0); + ngx_conf_merge_msec_value(conf->read_timeout, prev->read_timeout, 30000); + ngx_conf_merge_size_value(conf->header_buffer_size, prev->header_buffer_size, 4096); - ngx_conf_merge_msec_value(conf->read_timeout, prev->read_timeout, 30000); ngx_conf_merge_bufs_value(conf->bufs, prev->bufs, 8, 4096); - ngx_conf_merge_size_value(conf->busy_buffers_size, - prev->busy_buffers_size, 8192); -#if 0 - if (conf->max_temp_file_size > conf->bufs.size) { - return "\"proxy_max_temp_file\" must be greater " - "than one of the \"proxy_buffers\""; + size = conf->header_buffer_size; + if (size < conf->bufs.size) { + size = conf->bufs.size; } -#endif + + + ngx_conf_merge_size_value(conf->busy_buffers_size, + prev->busy_buffers_size, NGX_CONF_UNSET_SIZE); + + if (conf->busy_buffers_size == NGX_CONF_UNSET_SIZE) { + conf->busy_buffers_size = 2 * size; + + } else if (conf->busy_buffers_size < size) { + ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, + "\"proxy_busy_buffers_size\" must be equal or bigger than " + "maximum of the value of \"proxy_header_buffer_size\" and " + "one of the \"proxy_buffers\""); + + return NGX_CONF_ERROR; + } + ngx_conf_merge_size_value(conf->temp_file_write_size, - prev->temp_file_write_size, 16384); + prev->temp_file_write_size, NGX_CONF_UNSET_SIZE); + + if (conf->temp_file_write_size == NGX_CONF_UNSET_SIZE) { + conf->temp_file_write_size = 2 * size; + + } else if (conf->temp_file_write_size < size) { + ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, + "\"proxy_temp_file_write_size\" must be equal or bigger than " + "maximum of the value of \"proxy_header_buffer_size\" and " + "one of the \"proxy_buffers\""); + + return NGX_CONF_ERROR; + } + + + ngx_conf_merge_size_value(conf->max_temp_file_size, + prev->max_temp_file_size, NGX_CONF_UNSET_SIZE); + + if (conf->max_temp_file_size == NGX_CONF_UNSET_SIZE) { + conf->max_temp_file_size = 2 * size; + + } else if (conf->max_temp_file_size < size) { + ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, + "\"proxy_max_temp_file_size\" must be equal or bigger than " + "maximum of the value of \"proxy_header_buffer_size\" and " + "one of the \"proxy_buffers\""); + + return NGX_CONF_ERROR; + } + ngx_conf_merge_bitmask_value(conf->next_upstream, prev->next_upstream, (NGX_CONF_BITMASK_SET diff --git a/src/http/modules/proxy/ngx_http_proxy_upstream.c b/src/http/modules/proxy/ngx_http_proxy_upstream.c index bd4befd76..d27975919 100644 --- a/src/http/modules/proxy/ngx_http_proxy_upstream.c +++ b/src/http/modules/proxy/ngx_http_proxy_upstream.c @@ -1199,14 +1199,16 @@ static void ngx_http_proxy_send_response(ngx_http_proxy_ctx_t *p) ep->preread_size = p->header_in->last - p->header_in->pos; - ep->hunk_to_file = ngx_calloc_hunk(r->pool); - if (ep->preread_hunks == NULL) { - ngx_http_proxy_finalize_request(p, 0); - return; + if (p->cachable) { + ep->hunk_to_file = ngx_calloc_hunk(r->pool); + if (ep->hunk_to_file == NULL) { + ngx_http_proxy_finalize_request(p, 0); + return; + } + ep->hunk_to_file->pos = p->header_in->start; + ep->hunk_to_file->last = p->header_in->pos; + ep->hunk_to_file->type = NGX_HUNK_IN_MEMORY|NGX_HUNK_TEMP; } - ep->hunk_to_file->pos = p->header_in->start; - ep->hunk_to_file->last = p->header_in->pos; - ep->hunk_to_file->type = NGX_HUNK_IN_MEMORY|NGX_HUNK_TEMP; if (ngx_event_flags & NGX_USE_AIO_EVENT) { /* the posted aio operation can currupt a shadow buffer */