[nginx] Stream: post first read events from client and upstream.

Roman Arutyunyan arut at nginx.com
Tue Mar 15 13:08:39 UTC 2016


details:   http://hg.nginx.org/nginx/rev/d1c791479bbb
branches:  
changeset: 6435:d1c791479bbb
user:      Roman Arutyunyan <arut at nginx.com>
date:      Tue Mar 15 15:55:23 2016 +0300
description:
Stream: post first read events from client and upstream.

The main proxy function ngx_stream_proxy_process() can terminate the stream
session.  The code, following it, should check its return code to make sure the
session still exists.  This happens in client and upstream initialization
functions.  Swapping ngx_stream_proxy_process() call with the code, that
follows it, leaves the same problem vice versa.

In future ngx_stream_proxy_process() will call ngx_stream_proxy_next_upstream()
making it too complicated to know if stream session still exists after this
call.

Now ngx_stream_proxy_process() is called from posted event handlers in both
places with no code following it.  The posted event is automatically removed
once session is terminated.

diffstat:

 src/stream/ngx_stream_proxy_module.c |  22 ++++++++++------------
 1 files changed, 10 insertions(+), 12 deletions(-)

diffs (85 lines):

diff -r 602dc42035fe -r d1c791479bbb src/stream/ngx_stream_proxy_module.c
--- a/src/stream/ngx_stream_proxy_module.c	Tue Mar 15 15:15:30 2016 +0300
+++ b/src/stream/ngx_stream_proxy_module.c	Tue Mar 15 15:55:23 2016 +0300
@@ -54,7 +54,7 @@ static void ngx_stream_proxy_process_con
     ngx_uint_t from_upstream);
 static void ngx_stream_proxy_connect_handler(ngx_event_t *ev);
 static ngx_int_t ngx_stream_proxy_test_connect(ngx_connection_t *c);
-static ngx_int_t ngx_stream_proxy_process(ngx_stream_session_t *s,
+static void ngx_stream_proxy_process(ngx_stream_session_t *s,
     ngx_uint_t from_upstream, ngx_uint_t do_write);
 static void ngx_stream_proxy_next_upstream(ngx_stream_session_t *s);
 static void ngx_stream_proxy_finalize(ngx_stream_session_t *s, ngx_int_t rc);
@@ -406,8 +406,8 @@ ngx_stream_proxy_handler(ngx_stream_sess
         u->proxy_protocol = 0;
     }
 
-    if (ngx_stream_proxy_process(s, 0, 0) != NGX_OK) {
-        return;
+    if (c->read->ready) {
+        ngx_post_event(c->read, &ngx_posted_events);
     }
 
     ngx_stream_proxy_connect(s);
@@ -560,8 +560,8 @@ ngx_stream_proxy_init_upstream(ngx_strea
     pc->read->handler = ngx_stream_proxy_upstream_handler;
     pc->write->handler = ngx_stream_proxy_upstream_handler;
 
-    if (ngx_stream_proxy_process(s, 1, 0) != NGX_OK) {
-        return;
+    if (pc->read->ready) {
+        ngx_post_event(pc->read, &ngx_posted_events);
     }
 
     ngx_stream_proxy_process(s, 0, 1);
@@ -1019,7 +1019,7 @@ ngx_stream_proxy_test_connect(ngx_connec
 }
 
 
-static ngx_int_t
+static void
 ngx_stream_proxy_process(ngx_stream_session_t *s, ngx_uint_t from_upstream,
     ngx_uint_t do_write)
 {
@@ -1068,7 +1068,7 @@ ngx_stream_proxy_process(ngx_stream_sess
 
                 if (n == NGX_ERROR) {
                     ngx_stream_proxy_finalize(s, NGX_DECLINED);
-                    return NGX_ERROR;
+                    return;
                 }
 
                 if (n > 0) {
@@ -1147,20 +1147,20 @@ ngx_stream_proxy_process(ngx_stream_sess
         c->log->handler = handler;
 
         ngx_stream_proxy_finalize(s, NGX_OK);
-        return NGX_DONE;
+        return;
     }
 
     flags = src->read->eof ? NGX_CLOSE_EVENT : 0;
 
     if (ngx_handle_read_event(src->read, flags) != NGX_OK) {
         ngx_stream_proxy_finalize(s, NGX_ERROR);
-        return NGX_ERROR;
+        return;
     }
 
     if (dst) {
         if (ngx_handle_write_event(dst->write, 0) != NGX_OK) {
             ngx_stream_proxy_finalize(s, NGX_ERROR);
-            return NGX_ERROR;
+            return;
         }
 
         if (!c->read->delayed && !pc->read->delayed) {
@@ -1170,8 +1170,6 @@ ngx_stream_proxy_process(ngx_stream_sess
             ngx_del_timer(c->write);
         }
     }
-
-    return NGX_OK;
 }
 
 



More information about the nginx-devel mailing list