comparison src/stream/ngx_stream_proxy_module.c @ 6435:d1c791479bbb

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.
author Roman Arutyunyan <arut@nginx.com>
date Tue, 15 Mar 2016 15:55:23 +0300
parents 70e6e1f12dee
children 8f038068f4bc
comparison
equal deleted inserted replaced
6434:602dc42035fe 6435:d1c791479bbb
52 static void ngx_stream_proxy_downstream_handler(ngx_event_t *ev); 52 static void ngx_stream_proxy_downstream_handler(ngx_event_t *ev);
53 static void ngx_stream_proxy_process_connection(ngx_event_t *ev, 53 static void ngx_stream_proxy_process_connection(ngx_event_t *ev,
54 ngx_uint_t from_upstream); 54 ngx_uint_t from_upstream);
55 static void ngx_stream_proxy_connect_handler(ngx_event_t *ev); 55 static void ngx_stream_proxy_connect_handler(ngx_event_t *ev);
56 static ngx_int_t ngx_stream_proxy_test_connect(ngx_connection_t *c); 56 static ngx_int_t ngx_stream_proxy_test_connect(ngx_connection_t *c);
57 static ngx_int_t ngx_stream_proxy_process(ngx_stream_session_t *s, 57 static void ngx_stream_proxy_process(ngx_stream_session_t *s,
58 ngx_uint_t from_upstream, ngx_uint_t do_write); 58 ngx_uint_t from_upstream, ngx_uint_t do_write);
59 static void ngx_stream_proxy_next_upstream(ngx_stream_session_t *s); 59 static void ngx_stream_proxy_next_upstream(ngx_stream_session_t *s);
60 static void ngx_stream_proxy_finalize(ngx_stream_session_t *s, ngx_int_t rc); 60 static void ngx_stream_proxy_finalize(ngx_stream_session_t *s, ngx_int_t rc);
61 static u_char *ngx_stream_proxy_log_error(ngx_log_t *log, u_char *buf, 61 static u_char *ngx_stream_proxy_log_error(ngx_log_t *log, u_char *buf,
62 size_t len); 62 size_t len);
404 404
405 u->downstream_buf.last = p; 405 u->downstream_buf.last = p;
406 u->proxy_protocol = 0; 406 u->proxy_protocol = 0;
407 } 407 }
408 408
409 if (ngx_stream_proxy_process(s, 0, 0) != NGX_OK) { 409 if (c->read->ready) {
410 return; 410 ngx_post_event(c->read, &ngx_posted_events);
411 } 411 }
412 412
413 ngx_stream_proxy_connect(s); 413 ngx_stream_proxy_connect(s);
414 } 414 }
415 415
558 u->connected = 1; 558 u->connected = 1;
559 559
560 pc->read->handler = ngx_stream_proxy_upstream_handler; 560 pc->read->handler = ngx_stream_proxy_upstream_handler;
561 pc->write->handler = ngx_stream_proxy_upstream_handler; 561 pc->write->handler = ngx_stream_proxy_upstream_handler;
562 562
563 if (ngx_stream_proxy_process(s, 1, 0) != NGX_OK) { 563 if (pc->read->ready) {
564 return; 564 ngx_post_event(pc->read, &ngx_posted_events);
565 } 565 }
566 566
567 ngx_stream_proxy_process(s, 0, 1); 567 ngx_stream_proxy_process(s, 0, 1);
568 } 568 }
569 569
1017 1017
1018 return NGX_OK; 1018 return NGX_OK;
1019 } 1019 }
1020 1020
1021 1021
1022 static ngx_int_t 1022 static void
1023 ngx_stream_proxy_process(ngx_stream_session_t *s, ngx_uint_t from_upstream, 1023 ngx_stream_proxy_process(ngx_stream_session_t *s, ngx_uint_t from_upstream,
1024 ngx_uint_t do_write) 1024 ngx_uint_t do_write)
1025 { 1025 {
1026 off_t *received, limit; 1026 off_t *received, limit;
1027 size_t size, limit_rate; 1027 size_t size, limit_rate;
1066 1066
1067 n = dst->send(dst, b->pos, size); 1067 n = dst->send(dst, b->pos, size);
1068 1068
1069 if (n == NGX_ERROR) { 1069 if (n == NGX_ERROR) {
1070 ngx_stream_proxy_finalize(s, NGX_DECLINED); 1070 ngx_stream_proxy_finalize(s, NGX_DECLINED);
1071 return NGX_ERROR; 1071 return;
1072 } 1072 }
1073 1073
1074 if (n > 0) { 1074 if (n > 0) {
1075 b->pos += n; 1075 b->pos += n;
1076 1076
1145 s->received, c->sent, u->received, pc ? pc->sent : 0); 1145 s->received, c->sent, u->received, pc ? pc->sent : 0);
1146 1146
1147 c->log->handler = handler; 1147 c->log->handler = handler;
1148 1148
1149 ngx_stream_proxy_finalize(s, NGX_OK); 1149 ngx_stream_proxy_finalize(s, NGX_OK);
1150 return NGX_DONE; 1150 return;
1151 } 1151 }
1152 1152
1153 flags = src->read->eof ? NGX_CLOSE_EVENT : 0; 1153 flags = src->read->eof ? NGX_CLOSE_EVENT : 0;
1154 1154
1155 if (ngx_handle_read_event(src->read, flags) != NGX_OK) { 1155 if (ngx_handle_read_event(src->read, flags) != NGX_OK) {
1156 ngx_stream_proxy_finalize(s, NGX_ERROR); 1156 ngx_stream_proxy_finalize(s, NGX_ERROR);
1157 return NGX_ERROR; 1157 return;
1158 } 1158 }
1159 1159
1160 if (dst) { 1160 if (dst) {
1161 if (ngx_handle_write_event(dst->write, 0) != NGX_OK) { 1161 if (ngx_handle_write_event(dst->write, 0) != NGX_OK) {
1162 ngx_stream_proxy_finalize(s, NGX_ERROR); 1162 ngx_stream_proxy_finalize(s, NGX_ERROR);
1163 return NGX_ERROR; 1163 return;
1164 } 1164 }
1165 1165
1166 if (!c->read->delayed && !pc->read->delayed) { 1166 if (!c->read->delayed && !pc->read->delayed) {
1167 ngx_add_timer(c->write, pscf->timeout); 1167 ngx_add_timer(c->write, pscf->timeout);
1168 1168
1169 } else if (c->write->timer_set) { 1169 } else if (c->write->timer_set) {
1170 ngx_del_timer(c->write); 1170 ngx_del_timer(c->write);
1171 } 1171 }
1172 } 1172 }
1173
1174 return NGX_OK;
1175 } 1173 }
1176 1174
1177 1175
1178 static void 1176 static void
1179 ngx_stream_proxy_next_upstream(ngx_stream_session_t *s) 1177 ngx_stream_proxy_next_upstream(ngx_stream_session_t *s)