Timeout for whole request body

saravsars nginx-forum at nginx.us
Thu Jan 1 19:44:43 UTC 2015


Nginx provides client_body_timeout which is only for a period between two
successive read operations but in one of our use case we want to set timeout
for whole request body. To set the timeout for whole request body, we
changed the source to add a new timer. We would like to know whether this
approach is correct or not. Please correct me if there is any issue in the
following code.
Thanks

 diff -bur src/core/ngx_connection.c src/core/ngx_connection.c
--- src/core/ngx_connection.c	2014-12-19 15:33:48.000000000 +0530
+++ src/core/ngx_connection.c	2015-01-02 00:18:19.000000000 +0530
@@ -884,6 +884,10 @@
         ngx_del_timer(c->write);
     }
 
+    if (c->read_full->timer_set) {
+	ngx_del_timer(c->read_full);
+    }
+
     if (ngx_del_conn) {
         ngx_del_conn(c, NGX_CLOSE_EVENT);
 
diff -bur src/core/ngx_connection.h src/core/ngx_connection.h
--- src/core/ngx_connection.h	2014-12-19 15:33:48.000000000 +0530
+++ src/core/ngx_connection.h	2015-01-02 00:42:41.000000000 +0530
@@ -114,6 +114,7 @@
     void               *data;
     ngx_event_t        *read;
     ngx_event_t        *write;
+    ngx_event_t        *read_full;
 
     ngx_socket_t        fd;
 
diff -bur src/http/ngx_http_request_body.c src/http/ngx_http_request_body.c
--- src/http/ngx_http_request_body.c	2014-12-19 15:33:48.000000000 +0530
+++ src/http/ngx_http_request_body.c	2015-01-02 00:53:37.000000000 +0530
@@ -27,6 +27,7 @@
 static ngx_int_t ngx_http_request_body_save_filter(ngx_http_request_t *r,
     ngx_chain_t *in);
 
+static void ngx_http_full_body_timer_handler(ngx_event_t *wev);
 
 ngx_int_t
 ngx_http_read_client_request_body(ngx_http_request_t *r,
@@ -355,6 +356,15 @@
             clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
             ngx_add_timer(c->read, clcf->client_body_timeout);
 
+	    if (c->read_full == NULL) {
+                c->read_full=ngx_pcalloc(c->pool, sizeof(ngx_event_t));
+                c->read_full->handler = ngx_http_full_body_timer_handler;
+                c->read_full->data = r;
+                c->read_full->log =  r->connection->log;
+                ngx_add_timer(c->read_full, 10000);
+            }
+
+
             if (ngx_handle_read_event(c->read, 0) != NGX_OK) {
                 return NGX_HTTP_INTERNAL_SERVER_ERROR;
             }
@@ -1081,3 +1091,13 @@
 
     return NGX_OK;
 }
+
+static void ngx_http_full_body_timer_handler(ngx_event_t *wev)
+{
+        if (wev->timedout) {
+        ngx_http_request_t        *r;
+        r = wev->data;
+        //ngx_close_connection(r->connection);
+        ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT);
+	}
+}
diff -bur src/http/ngx_http_request.c src/http/ngx_http_request.c
--- src/http/ngx_http_request.c	2014-12-19 15:33:48.000000000 +0530
+++ src/http/ngx_http_request.c	2015-01-02 00:24:32.000000000 +0530
@@ -2263,6 +2263,10 @@
             if (c->write->timer_set) {
                 ngx_del_timer(c->write);
             }
+		
+	    if (c->read_full->timer_set) {
+		ngx_del_timer(c->read_full);
+            }
         }
 
         c->read->handler = ngx_http_request_handler;
@@ -2376,6 +2380,10 @@
         ngx_del_timer(c->write);
     }
 
+    if (c->read_full->timer_set) {
+        ngx_del_timer(c->read_full);
+    }
+
     if (c->read->eof) {
         ngx_http_close_request(r, 0);
         return;

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,255925,255925#msg-255925



More information about the nginx mailing list