changeset 303:00c5660d2707

nginx-0.0.3-2004-04-01-20:20:53 import
author Igor Sysoev <igor@sysoev.ru>
date Thu, 01 Apr 2004 16:20:53 +0000
parents 1526e7686b20
children bcbe876f4262
files auto/configure src/core/ngx_connection.c src/event/modules/ngx_kqueue_module.c src/event/ngx_event.c src/event/ngx_event.h src/event/ngx_event_accept.c src/event/ngx_event_timer.h src/http/modules/proxy/ngx_http_proxy_handler.c src/http/modules/proxy/ngx_http_proxy_handler.h src/http/modules/proxy/ngx_http_proxy_upstream.c src/http/ngx_http_core_module.c src/http/ngx_http_core_module.h src/http/ngx_http_request.h src/http/ngx_http_request_body.c src/http/ngx_http_write_filter.c src/os/unix/ngx_files.c src/os/unix/ngx_files.h src/os/unix/ngx_process_cycle.c src/os/unix/ngx_readv_chain.c
diffstat 19 files changed, 130 insertions(+), 64 deletions(-) [+]
line wrap: on
line diff
--- a/auto/configure
+++ b/auto/configure
@@ -36,6 +36,6 @@ if [ "$PLATFORM" != win32 ]; then
     . auto/unix
 fi
 
-have NGX_SMP . auto/have
+have=NGX_SMP . auto/have
 
 . auto/summary
--- a/src/core/ngx_connection.c
+++ b/src/core/ngx_connection.c
@@ -212,6 +212,9 @@ void ngx_close_listening_sockets(ngx_cyc
         return;
     }
 
+    ngx_accept_mutex_held = 0;
+    ngx_accept_mutex = NULL;
+
     ls = cycle->listening.elts;
     for (i = 0; i < cycle->listening.nelts; i++) {
         fd = ls[i].fd;
--- a/src/event/modules/ngx_kqueue_module.c
+++ b/src/event/modules/ngx_kqueue_module.c
@@ -375,7 +375,7 @@ static ngx_int_t ngx_kqueue_process_even
             return NGX_ERROR;
         }
 
-#if 0
+#if 1
         if (ngx_accept_mutex_held == 0 && timer == 0) {
             /* STUB */ timer = 500;
         }
--- a/src/event/ngx_event.c
+++ b/src/event/ngx_event.c
@@ -28,7 +28,8 @@ extern ngx_event_module_t ngx_epoll_modu
 #include <ngx_aio_module.h>
 #endif
 
-static int ngx_event_init(ngx_cycle_t *cycle);
+static ngx_int_t ngx_event_module_init(ngx_cycle_t *cycle);
+static ngx_int_t ngx_event_process_init(ngx_cycle_t *cycle);
 static char *ngx_events_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
 
 static char *ngx_event_connections(ngx_conf_t *cf, ngx_command_t *cmd,
@@ -39,11 +40,16 @@ static void *ngx_event_create_conf(ngx_c
 static char *ngx_event_init_conf(ngx_cycle_t *cycle, void *conf);
 
 
-int                    ngx_event_flags;
-ngx_event_actions_t    ngx_event_actions;
+static ngx_uint_t                 ngx_event_max_module;
+
+ngx_uint_t                        ngx_event_flags;
+ngx_event_actions_t               ngx_event_actions;
 
 
-static int             ngx_event_max_module;
+ngx_atomic_t                     *ngx_accept_mutex_ptr;
+ngx_atomic_t                     *ngx_accept_mutex;
+ngx_uint_t                        ngx_accept_mutex_held;
+
 
 ngx_thread_volatile ngx_event_t  *ngx_posted_events;
 #if (NGX_THREADS)
@@ -101,6 +107,13 @@ static ngx_command_t  ngx_event_core_com
       offsetof(ngx_event_conf_t, multi_accept),
       NULL },
 
+    { ngx_string("accept_mutex"),
+      NGX_EVENT_CONF|NGX_CONF_TAKE1,
+      ngx_conf_set_flag_slot,
+      0,
+      offsetof(ngx_event_conf_t, accept_mutex),
+      NULL },
+
       ngx_null_command
 };
 
@@ -119,18 +132,50 @@ ngx_module_t  ngx_event_core_module = {
     &ngx_event_core_module_ctx,            /* module context */
     ngx_event_core_commands,               /* module directives */
     NGX_EVENT_MODULE,                      /* module type */
-    NULL,                                  /* init module */
-    ngx_event_init                         /* init child */
+    ngx_event_module_init,                 /* init module */
+    ngx_event_process_init                 /* init process */
 };
 
 
-static int ngx_event_init(ngx_cycle_t *cycle)
+static ngx_int_t ngx_event_module_init(ngx_cycle_t *cycle)
+{
+    ngx_core_conf_t   *ccf;
+    ngx_event_conf_t  *ecf;
+
+    ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
+
+    if (ccf->master == 0) {
+        return NGX_OK;
+    }
+
+    ecf = ngx_event_get_conf(cycle->conf_ctx, ngx_event_core_module);
+
+    if (ecf->accept_mutex == 0) {
+        return NGX_OK;
+    }
+
+    ngx_accept_mutex_ptr = mmap(NULL, sizeof(ngx_atomic_t),
+                                PROT_READ|PROT_WRITE,
+                                MAP_ANON|MAP_SHARED, -1, 0);
+
+    if (ngx_accept_mutex_ptr == NULL) {
+        ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
+                      "mmap(MAP_ANON|MAP_SHARED) failed");
+        return NGX_ERROR;
+    }
+
+    return NGX_OK;
+}
+
+
+static ngx_int_t ngx_event_process_init(ngx_cycle_t *cycle)
 {
     ngx_uint_t           m, i;
     ngx_socket_t         fd;
     ngx_event_t         *rev, *wev;
     ngx_listening_t     *s;
     ngx_connection_t    *c;
+    ngx_core_conf_t     *ccf;
     ngx_event_conf_t    *ecf;
     ngx_event_module_t  *module;
 #if (WIN32)
@@ -138,6 +183,13 @@ static int ngx_event_init(ngx_cycle_t *c
 #endif
 
 
+    ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
+
+    if (ccf->worker_processes > 1 && ngx_accept_mutex_ptr) {
+        ngx_accept_mutex = ngx_accept_mutex_ptr;
+        ngx_accept_mutex_held = 1;
+    }
+
 #if (NGX_THREADS)
     if (!(ngx_posted_events_mutex = ngx_mutex_init(cycle->log, 0))) {
         return NGX_ERROR;
@@ -470,6 +522,7 @@ static void *ngx_event_create_conf(ngx_c
     ecf->connections = NGX_CONF_UNSET;
     ecf->use = NGX_CONF_UNSET;
     ecf->multi_accept = NGX_CONF_UNSET;
+    ecf->accept_mutex = NGX_CONF_UNSET;
     ecf->name = (void *) NGX_CONF_UNSET;
 
     return ecf;
@@ -542,6 +595,7 @@ static char *ngx_event_init_conf(ngx_cyc
     cycle->connection_n = ecf->connections;
 
     ngx_conf_init_value(ecf->multi_accept, 0);
+    ngx_conf_init_value(ecf->accept_mutex, 1);
 
     return NGX_CONF_OK;
 }
--- a/src/event/ngx_event.h
+++ b/src/event/ngx_event.h
@@ -374,9 +374,12 @@ extern ngx_event_actions_t   ngx_event_a
 
 
 typedef struct {
-    int          connections;
-    int          use;
+    ngx_int_t    connections;
+    ngx_int_t    use;
+
     ngx_flag_t   multi_accept;
+    ngx_flag_t   accept_mutex;
+
     u_char      *name;
 } ngx_event_conf_t;
 
@@ -407,7 +410,7 @@ extern ngx_uint_t             ngx_accept
            }
 
 
-extern int                    ngx_event_flags;
+extern ngx_uint_t             ngx_event_flags;
 extern ngx_module_t           ngx_events_module;
 extern ngx_module_t           ngx_event_core_module;
 
--- a/src/event/ngx_event_accept.c
+++ b/src/event/ngx_event_accept.c
@@ -14,11 +14,6 @@ typedef struct {
 static size_t ngx_accept_log_error(void *data, char *buf, size_t len);
 
 
-ngx_atomic_t  *ngx_accept_mutex_ptr;
-ngx_atomic_t  *ngx_accept_mutex;
-ngx_uint_t     ngx_accept_mutex_held;
-
-
 void ngx_event_accept(ngx_event_t *ev)
 {
     ngx_uint_t             instance, rinstance, winstance, accepted;
--- a/src/event/ngx_event_timer.h
+++ b/src/event/ngx_event_timer.h
@@ -67,17 +67,28 @@ ngx_inline static void ngx_event_del_tim
 
 ngx_inline static void ngx_event_add_timer(ngx_event_t *ev, ngx_msec_t timer)
 {
-    if (ev->timer_set) {
-        ngx_del_timer(ev);
-    }
+    ngx_int_t  key;
 
-    ev->rbtree_key = (ngx_int_t)
+    key = (ngx_int_t)
               (ngx_elapsed_msec / NGX_TIMER_RESOLUTION * NGX_TIMER_RESOLUTION
                                               + timer) / NGX_TIMER_RESOLUTION;
 #if 0
                              (ngx_elapsed_msec + timer) / NGX_TIMER_RESOLUTION;
 #endif
 
+    if (ev->timer_set) {
+        if (key - ev->rbtree_key < 50) {
+            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);
+            return;
+        }
+
+        ngx_del_timer(ev);
+    }
+
+    ev->rbtree_key = key;
+
     ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0,
                    "event timer add: %d: %d",
                     ngx_event_ident(ev->data), ev->rbtree_key);
--- a/src/http/modules/proxy/ngx_http_proxy_handler.c
+++ b/src/http/modules/proxy/ngx_http_proxy_handler.c
@@ -60,13 +60,6 @@ static ngx_command_t  ngx_http_proxy_com
       0,
       NULL },
 
-    { ngx_string("proxy_request_buffer_size"),
-      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
-      ngx_conf_set_size_slot,
-      NGX_HTTP_LOC_CONF_OFFSET,
-      offsetof(ngx_http_proxy_loc_conf_t, request_buffer_size),
-      NULL },
-
     { ngx_string("proxy_connect_timeout"),
       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
       ngx_conf_set_msec_slot,
@@ -781,7 +774,6 @@ static void *ngx_http_proxy_create_loc_c
 
     */
 
-    conf->request_buffer_size = NGX_CONF_UNSET_SIZE;
     conf->connect_timeout = NGX_CONF_UNSET_MSEC;
     conf->send_timeout = NGX_CONF_UNSET_MSEC;
 
@@ -822,8 +814,6 @@ static char *ngx_http_proxy_merge_loc_co
     ngx_http_proxy_loc_conf_t *prev = parent;
     ngx_http_proxy_loc_conf_t *conf = child;
 
-    ngx_conf_merge_size_value(conf->request_buffer_size,
-                              prev->request_buffer_size, 8192);
     ngx_conf_merge_msec_value(conf->connect_timeout,
                               prev->connect_timeout, 60000);
     ngx_conf_merge_msec_value(conf->send_timeout, prev->send_timeout, 30000);
--- a/src/http/modules/proxy/ngx_http_proxy_handler.h
+++ b/src/http/modules/proxy/ngx_http_proxy_handler.h
@@ -48,7 +48,6 @@ typedef struct {
 
 
 typedef struct {
-    size_t                           request_buffer_size;
     size_t                           header_buffer_size;
     size_t                           busy_buffers_size;
     size_t                           max_temp_file_size;
--- a/src/http/modules/proxy/ngx_http_proxy_upstream.c
+++ b/src/http/modules/proxy/ngx_http_proxy_upstream.c
@@ -86,7 +86,6 @@ int ngx_http_proxy_request_upstream(ngx_
     tf->warn = "a client request body is buffered to a temporary file";
     /* tf->persistent = 0; */
 
-    rb->buf_size = p->lcf->request_buffer_size;
     rb->handler = ngx_http_proxy_init_upstream;
     rb->data = p;
     /* rb->bufs = NULL; */
@@ -1179,7 +1178,10 @@ static void ngx_http_proxy_send_response
     ep->temp_file->file.log = r->connection->log;
     ep->temp_file->path = p->lcf->temp_path;
     ep->temp_file->pool = r->pool;
-    if (!p->cachable) {
+
+    if (p->cachable) {
+        ep->temp_file->persistent = 1;
+    } else {
         ep->temp_file->warn = "an upstream response is buffered "
                               "to a temporary file";
     }
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -149,6 +149,13 @@ static ngx_command_t  ngx_http_core_comm
       offsetof(ngx_http_core_loc_conf_t, client_max_body_size),
       NULL },
 
+    { ngx_string("client_body_buffer_size"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+      ngx_conf_set_size_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_core_loc_conf_t, client_body_buffer_size),
+      NULL },
+
     { ngx_string("client_body_timeout"),
       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
       ngx_conf_set_msec_slot,
@@ -1181,6 +1188,7 @@ static void *ngx_http_core_create_loc_co
     */
 
     lcf->client_max_body_size = NGX_CONF_UNSET_SIZE;
+    lcf->client_body_buffer_size = NGX_CONF_UNSET_SIZE;
     lcf->client_body_timeout = NGX_CONF_UNSET_MSEC;
     lcf->sendfile = NGX_CONF_UNSET;
     lcf->tcp_nopush = NGX_CONF_UNSET;
@@ -1261,6 +1269,8 @@ static char *ngx_http_core_merge_loc_con
 
     ngx_conf_merge_size_value(conf->client_max_body_size,
                               prev->client_max_body_size, 10 * 1024 * 1024);
+    ngx_conf_merge_size_value(conf->client_body_buffer_size,
+                              prev->client_body_buffer_size, 8192);
     ngx_conf_merge_msec_value(conf->client_body_timeout,
                               prev->client_body_timeout, 60000);
     ngx_conf_merge_value(conf->sendfile, prev->sendfile, 0);
--- a/src/http/ngx_http_core_module.h
+++ b/src/http/ngx_http_core_module.h
@@ -124,6 +124,7 @@ typedef struct {
     size_t        client_max_body_size;    /* client_max_body_size */
     size_t        send_lowat;              /* send_lowat */
     size_t        discarded_buffer_size;   /* discarded_buffer_size */
+    size_t        client_body_buffer_size; /* client_body_buffer_size */
 
     ngx_msec_t    client_body_timeout;     /* client_body_timeout */
     ngx_msec_t    send_timeout;            /* send_timeout */
--- a/src/http/ngx_http_request.h
+++ b/src/http/ngx_http_request.h
@@ -155,7 +155,6 @@ typedef struct {
     ngx_chain_t       *bufs;
     ngx_hunk_t        *buf;
     size_t             rest;
-    size_t             buf_size;
     void             (*handler) (void *data); 
     void              *data;
 } ngx_http_request_body_t;
--- a/src/http/ngx_http_request_body.c
+++ b/src/http/ngx_http_request_body.c
@@ -11,10 +11,10 @@ static ngx_int_t ngx_http_do_read_client
 
 ngx_int_t ngx_http_read_client_request_body(ngx_http_request_t *r)
 {
-    ssize_t       size;
-    ngx_hunk_t   *h;
-    ngx_chain_t  *cl;
-
+    ssize_t                    size;
+    ngx_hunk_t                *h;
+    ngx_chain_t               *cl;
+    ngx_http_core_loc_conf_t  *clcf;
 
     size = r->header_in->last - r->header_in->pos;
 
@@ -47,15 +47,18 @@ ngx_int_t ngx_http_read_client_request_b
     }
 
 
+    clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
+
     r->request_body->rest = r->headers_in.content_length_n - size;
 
     if (r->request_body->rest
-                < r->request_body->buf_size + (r->request_body->buf_size >> 2))
+                             < clcf->client_body_buffer_size
+                                        + (clcf->client_body_buffer_size >> 2))
     {
         size = r->request_body->rest;
 
     } else {
-        size = r->request_body->buf_size;
+        size = clcf->client_body_buffer_size;
     }
 
     ngx_test_null(r->request_body->buf, ngx_create_temp_hunk(r->pool, size),
--- a/src/http/ngx_http_write_filter.c
+++ b/src/http/ngx_http_write_filter.c
@@ -62,7 +62,7 @@ ngx_module_t  ngx_http_write_filter_modu
     ngx_http_write_filter_commands,        /* module directives */
     NGX_HTTP_MODULE,                       /* module type */
     ngx_http_write_filter_init,            /* init module */
-    NULL                                   /* init child */
+    NULL                                   /* init process */
 };
 
 
@@ -82,7 +82,8 @@ int ngx_http_write_filter(ngx_http_reque
                             sizeof(ngx_http_write_filter_ctx_t), NGX_ERROR);
     }
 
-    size = flush = 0;
+    size = 0;
+    flush = 0;
     last = 0;
     ll = &ctx->out;
 
--- a/src/os/unix/ngx_files.c
+++ b/src/os/unix/ngx_files.c
@@ -101,6 +101,20 @@ ssize_t ngx_write_file(ngx_file_t *file,
 }
 
 
+int ngx_open_tempfile(u_char *name, ngx_uint_t persistent)
+{
+    ngx_fd_t  fd;
+
+    fd = open((const char *) name, O_CREAT|O_EXCL|O_RDWR, 0600);
+
+    if (fd != -1 && !persistent) {
+        unlink((const char *) name);
+    }
+
+    return fd;
+}
+
+
 ssize_t ngx_write_chain_to_file(ngx_file_t *file, ngx_chain_t *cl,
                                 off_t offset, ngx_pool_t *pool)
 {
--- a/src/os/unix/ngx_files.h
+++ b/src/os/unix/ngx_files.h
@@ -30,8 +30,7 @@
 #define ngx_delete_file_n        "unlink()"
 
 
-#define ngx_open_tempfile(name, persistent)                                 \
-                         open((const char *) name, O_CREAT|O_EXCL|O_RDWR, 0600)
+int ngx_open_tempfile(u_char *name, ngx_uint_t persistent);
 #define ngx_open_tempfile_n      "open()"
 
 
--- a/src/os/unix/ngx_process_cycle.c
+++ b/src/os/unix/ngx_process_cycle.c
@@ -65,17 +65,6 @@ void ngx_master_process_cycle(ngx_cycle_
     signo = 0;
     live = 0;
 
-    ngx_accept_mutex_ptr = mmap(NULL, sizeof(ngx_atomic_t),
-                                PROT_READ|PROT_WRITE,
-                                MAP_ANON|MAP_SHARED, -1, 0);
-
-    if (ngx_accept_mutex_ptr == NULL) {
-        ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
-                      "mmap(MAP_ANON|MAP_SHARED) failed");
-        /* fatal */
-        exit(2);
-    }
-
     for ( ;; ) {
         ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, "new cycle");
 
@@ -378,11 +367,6 @@ static void ngx_worker_process_cycle(ngx
 
     ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
 
-    if (ccf->worker_processes > 1) {
-        ngx_accept_mutex = ngx_accept_mutex_ptr;
-        ngx_accept_mutex_held = 1;
-    }
-
     if (ccf->group != (gid_t) NGX_CONF_UNSET) {
         if (setuid(ccf->group) == -1) {
             ngx_log_error(NGX_LOG_EMERG, cycle->log, ngx_errno,
@@ -485,8 +469,6 @@ static void ngx_worker_process_cycle(ngx
 
     ngx_close_listening_sockets(cycle);
 
-    ngx_accept_mutex = NULL;
-
     for ( ;; ) {
         if (ngx_event_timer_rbtree == &ngx_event_timer_sentinel) {
             ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "exiting");
--- a/src/os/unix/ngx_readv_chain.c
+++ b/src/os/unix/ngx_readv_chain.c
@@ -68,7 +68,7 @@ ssize_t ngx_readv_chain(ngx_connection_t
     }
 
     ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
-                   "readv: %d:%d", io.nelts, iov->iov_len);
+                   "readv: %d, last:%d", io.nelts, iov->iov_len);
 
     rev = c->read;