Skip to content

Commit

Permalink
Stream: post first read events from client and upstream.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
arut committed Mar 15, 2016
1 parent 8f53f6f commit c790f96
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/stream/ngx_stream_proxy_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static void ngx_stream_proxy_process_connection(ngx_event_t *ev,
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);
Expand Down Expand Up @@ -406,8 +406,8 @@ ngx_stream_proxy_handler(ngx_stream_session_t *s)
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);
Expand Down Expand Up @@ -560,8 +560,8 @@ ngx_stream_proxy_init_upstream(ngx_stream_session_t *s)
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);
Expand Down Expand Up @@ -1019,7 +1019,7 @@ ngx_stream_proxy_test_connect(ngx_connection_t *c)
}


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)
{
Expand Down Expand Up @@ -1068,7 +1068,7 @@ ngx_stream_proxy_process(ngx_stream_session_t *s, ngx_uint_t from_upstream,

if (n == NGX_ERROR) {
ngx_stream_proxy_finalize(s, NGX_DECLINED);
return NGX_ERROR;
return;
}

if (n > 0) {
Expand Down Expand Up @@ -1147,20 +1147,20 @@ ngx_stream_proxy_process(ngx_stream_session_t *s, ngx_uint_t from_upstream,
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) {
Expand All @@ -1170,8 +1170,6 @@ ngx_stream_proxy_process(ngx_stream_session_t *s, ngx_uint_t from_upstream,
ngx_del_timer(c->write);
}
}

return NGX_OK;
}


Expand Down

0 comments on commit c790f96

Please sign in to comment.