Klaus Demo nginx / 7494204
Stream: upstream and downstream limit rates. Roman Arutyunyan 7 years ago
2 changed file(s) with 112 addition(s) and 16 deletion(s). Raw diff Collapse all Expand all
1717 ngx_msec_t timeout;
1818 ngx_msec_t next_upstream_timeout;
1919 size_t downstream_buf_size;
20 size_t downstream_limit_rate;
2021 size_t upstream_buf_size;
22 size_t upstream_limit_rate;
2123 ngx_uint_t next_upstream_tries;
2224 ngx_flag_t next_upstream;
2325 ngx_flag_t proxy_protocol;
131133 offsetof(ngx_stream_proxy_srv_conf_t, downstream_buf_size),
132134 NULL },
133135
136 { ngx_string("proxy_downstream_limit_rate"),
137 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
138 ngx_conf_set_size_slot,
139 NGX_STREAM_SRV_CONF_OFFSET,
140 offsetof(ngx_stream_proxy_srv_conf_t, downstream_limit_rate),
141 NULL },
142
134143 { ngx_string("proxy_upstream_buffer"),
135144 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
136145 ngx_conf_set_size_slot,
137146 NGX_STREAM_SRV_CONF_OFFSET,
138147 offsetof(ngx_stream_proxy_srv_conf_t, upstream_buf_size),
148 NULL },
149
150 { ngx_string("proxy_upstream_limit_rate"),
151 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
152 ngx_conf_set_size_slot,
153 NGX_STREAM_SRV_CONF_OFFSET,
154 offsetof(ngx_stream_proxy_srv_conf_t, upstream_limit_rate),
139155 NULL },
140156
141157 { ngx_string("proxy_next_upstream"),
339355 }
340356
341357 u->proxy_protocol = pscf->proxy_protocol;
358 u->start_sec = ngx_time();
342359
343360 p = ngx_pnalloc(c->pool, pscf->downstream_buf_size);
344361 if (p == NULL) {
830847 static void
831848 ngx_stream_proxy_process_connection(ngx_event_t *ev, ngx_uint_t from_upstream)
832849 {
833 ngx_connection_t *c;
834 ngx_stream_session_t *s;
835 ngx_stream_upstream_t *u;
850 ngx_connection_t *c, *pc;
851 ngx_stream_session_t *s;
852 ngx_stream_upstream_t *u;
853 ngx_stream_proxy_srv_conf_t *pscf;
836854
837855 c = ev->data;
838856 s = c->data;
839857 u = s->upstream;
840858
841859 if (ev->timedout) {
842 ngx_connection_error(c, NGX_ETIMEDOUT, "connection timed out");
843 ngx_stream_proxy_finalize(s, NGX_DECLINED);
860
861 if (ev->delayed) {
862
863 ev->timedout = 0;
864 ev->delayed = 0;
865
866 if (!ev->ready) {
867 if (ngx_handle_read_event(ev, 0) != NGX_OK) {
868 ngx_stream_proxy_finalize(s, NGX_ERROR);
869 return;
870 }
871
872 if (u->upstream_buf.start) {
873 pc = u->peer.connection;
874
875 if (!c->read->delayed && !pc->read->delayed) {
876 pscf = ngx_stream_get_module_srv_conf(s,
877 ngx_stream_proxy_module);
878 ngx_add_timer(c->write, pscf->timeout);
879 }
880 }
881
882 return;
883 }
884
885 } else {
886 ngx_connection_error(c, NGX_ETIMEDOUT, "connection timed out");
887 ngx_stream_proxy_finalize(s, NGX_DECLINED);
888 return;
889 }
890
891 } else if (ev->delayed) {
892
893 ngx_log_debug0(NGX_LOG_DEBUG_STREAM, c->log, 0,
894 "stream connection delayed");
895
896 if (ngx_handle_read_event(ev, 0) != NGX_OK) {
897 ngx_stream_proxy_finalize(s, NGX_ERROR);
898 }
899
844900 return;
845901 }
846902
929985 ngx_stream_proxy_process(ngx_stream_session_t *s, ngx_uint_t from_upstream,
930986 ngx_uint_t do_write)
931987 {
932 size_t size;
988 off_t *received, limit;
989 size_t size, limit_rate;
933990 ssize_t n;
934991 ngx_buf_t *b;
935992 ngx_uint_t flags;
993 ngx_msec_t delay;
936994 ngx_connection_t *c, *pc, *src, *dst;
937995 ngx_log_handler_pt handler;
938996 ngx_stream_upstream_t *u;
9431001 c = s->connection;
9441002 pc = u->upstream_buf.start ? u->peer.connection : NULL;
9451003
1004 pscf = ngx_stream_get_module_srv_conf(s, ngx_stream_proxy_module);
1005
9461006 if (from_upstream) {
9471007 src = pc;
9481008 dst = c;
9491009 b = &u->upstream_buf;
1010 limit_rate = pscf->upstream_limit_rate;
1011 received = &u->received;
9501012
9511013 } else {
9521014 src = c;
9531015 dst = pc;
9541016 b = &u->downstream_buf;
1017 limit_rate = pscf->downstream_limit_rate;
1018 received = &s->received;
9551019 }
9561020
9571021 for ( ;; ) {
9821046
9831047 size = b->end - b->last;
9841048
985 if (size && src->read->ready) {
1049 if (size && src->read->ready && !src->read->delayed) {
1050
1051 if (limit_rate) {
1052 limit = (off_t) limit_rate * (ngx_time() - u->start_sec + 1)
1053 - *received;
1054
1055 if (limit <= 0) {
1056 src->read->delayed = 1;
1057 delay = (ngx_msec_t) (- limit * 1000 / limit_rate + 1);
1058 ngx_add_timer(src->read, delay);
1059 break;
1060 }
1061
1062 if (size > (size_t) limit) {
1063 size = limit;
1064 }
1065 }
9861066
9871067 n = src->recv(src, b->last, size);
9881068
9911071 }
9921072
9931073 if (n > 0) {
994 if (from_upstream) {
995 u->received += n;
996
997 } else {
998 s->received += n;
1074 if (limit_rate) {
1075 delay = (ngx_msec_t) (n * 1000 / limit_rate);
1076
1077 if (delay > 0) {
1078 src->read->delayed = 1;
1079 ngx_add_timer(src->read, delay);
1080 }
9991081 }
10001082
1083 *received += n;
1084 b->last += n;
10011085 do_write = 1;
1002 b->last += n;
1086
10031087 continue;
10041088 }
10051089
10101094
10111095 break;
10121096 }
1013
1014 pscf = ngx_stream_get_module_srv_conf(s, ngx_stream_proxy_module);
10151097
10161098 if (src->read->eof && (b->pos == b->last || (dst && dst->read->eof))) {
10171099 handler = c->log->handler;
10431125 return NGX_ERROR;
10441126 }
10451127
1046 ngx_add_timer(c->read, pscf->timeout);
1128 if (!c->read->delayed && !pc->read->delayed) {
1129 ngx_add_timer(c->write, pscf->timeout);
1130
1131 } else if (c->write->timer_set) {
1132 ngx_del_timer(c->write);
1133 }
10471134 }
10481135
10491136 return NGX_OK;
12061293 conf->timeout = NGX_CONF_UNSET_MSEC;
12071294 conf->next_upstream_timeout = NGX_CONF_UNSET_MSEC;
12081295 conf->downstream_buf_size = NGX_CONF_UNSET_SIZE;
1296 conf->downstream_limit_rate = NGX_CONF_UNSET_SIZE;
12091297 conf->upstream_buf_size = NGX_CONF_UNSET_SIZE;
1298 conf->upstream_limit_rate = NGX_CONF_UNSET_SIZE;
12101299 conf->next_upstream_tries = NGX_CONF_UNSET_UINT;
12111300 conf->next_upstream = NGX_CONF_UNSET;
12121301 conf->proxy_protocol = NGX_CONF_UNSET;
12431332 ngx_conf_merge_size_value(conf->downstream_buf_size,
12441333 prev->downstream_buf_size, 16384);
12451334
1335 ngx_conf_merge_size_value(conf->downstream_limit_rate,
1336 prev->downstream_limit_rate, 0);
1337
12461338 ngx_conf_merge_size_value(conf->upstream_buf_size,
12471339 prev->upstream_buf_size, 16384);
1340
1341 ngx_conf_merge_size_value(conf->upstream_limit_rate,
1342 prev->upstream_limit_rate, 0);
12481343
12491344 ngx_conf_merge_uint_value(conf->next_upstream_tries,
12501345 prev->next_upstream_tries, 0);
8282 ngx_buf_t downstream_buf;
8383 ngx_buf_t upstream_buf;
8484 off_t received;
85 time_t start_sec;
8586 #if (NGX_STREAM_SSL)
8687 ngx_str_t ssl_name;
8788 #endif