Klaus Demo nginx / 18e4d04
Upstream: proxy_limit_rate and friends. The directives limit the upstream read rate. For example, "proxy_limit_rate 42" limits proxy upstream read rate to 42 bytes per second. Roman Arutyunyan 6 years ago
8 changed file(s) with 131 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
6565 return NGX_ABORT;
6666 }
6767
68 if (rev->active && !rev->ready) {
69 ngx_add_timer(rev, p->read_timeout);
70
71 } else if (rev->timer_set) {
72 ngx_del_timer(rev);
68 if (!rev->delayed) {
69 if (rev->active && !rev->ready) {
70 ngx_add_timer(rev, p->read_timeout);
71
72 } else if (rev->timer_set) {
73 ngx_del_timer(rev);
74 }
7375 }
7476 }
7577
98100 static ngx_int_t
99101 ngx_event_pipe_read_upstream(ngx_event_pipe_t *p)
100102 {
103 off_t limit;
101104 ssize_t n, size;
102105 ngx_int_t rc;
103106 ngx_buf_t *b;
107 ngx_msec_t delay;
104108 ngx_chain_t *chain, *cl, *ln;
105109
106110 if (p->upstream_eof || p->upstream_error || p->upstream_done) {
167171 break;
168172 }
169173 #endif
174
175 if (p->limit_rate) {
176 if (p->upstream->read->delayed) {
177 break;
178 }
179
180 limit = (off_t) p->limit_rate * (ngx_time() - p->start_sec + 1)
181 - p->read_length;
182
183 if (limit <= 0) {
184 p->upstream->read->delayed = 1;
185 delay = (ngx_msec_t) (- limit * 1000 / p->limit_rate + 1);
186 ngx_add_timer(p->upstream->read, delay);
187 break;
188 }
189
190 } else {
191 limit = 0;
192 }
170193
171194 if (p->free_raw_bufs) {
172195
269292 break;
270293 }
271294
272 n = p->upstream->recv_chain(p->upstream, chain, 0);
295 n = p->upstream->recv_chain(p->upstream, chain, limit);
273296
274297 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0,
275298 "pipe recv chain: %z", n);
299322 break;
300323 }
301324 }
325
326 delay = p->limit_rate ? (ngx_msec_t) n * 1000 / p->limit_rate : 0;
302327
303328 p->read_length += n;
304329 cl = chain;
335360
336361 ln->next = p->free_raw_bufs;
337362 p->free_raw_bufs = cl;
363 }
364
365 if (delay > 0) {
366 p->upstream->read->delayed = 1;
367 ngx_add_timer(p->upstream->read, delay);
368 break;
338369 }
339370 }
340371
7979 size_t preread_size;
8080 ngx_buf_t *buf_to_file;
8181
82 size_t limit_rate;
83 time_t start_sec;
84
8285 ngx_temp_file_t *temp_file;
8386
8487 /* STUB */ int num;
332332 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.force_ranges),
333333 NULL },
334334
335 { ngx_string("fastcgi_limit_rate"),
336 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
337 ngx_conf_set_size_slot,
338 NGX_HTTP_LOC_CONF_OFFSET,
339 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.limit_rate),
340 NULL },
341
335342 #if (NGX_HTTP_CACHE)
336343
337344 { ngx_string("fastcgi_cache"),
23492356
23502357 conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
23512358 conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
2359 conf->upstream.limit_rate = NGX_CONF_UNSET_SIZE;
23522360
23532361 conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
23542362 conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
24442452 ngx_conf_merge_size_value(conf->upstream.buffer_size,
24452453 prev->upstream.buffer_size,
24462454 (size_t) ngx_pagesize);
2455
2456 ngx_conf_merge_size_value(conf->upstream.limit_rate,
2457 prev->upstream.limit_rate, 0);
24472458
24482459
24492460 ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,
393393 ngx_conf_set_flag_slot,
394394 NGX_HTTP_LOC_CONF_OFFSET,
395395 offsetof(ngx_http_proxy_loc_conf_t, upstream.force_ranges),
396 NULL },
397
398 { ngx_string("proxy_limit_rate"),
399 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
400 ngx_conf_set_size_slot,
401 NGX_HTTP_LOC_CONF_OFFSET,
402 offsetof(ngx_http_proxy_loc_conf_t, upstream.limit_rate),
396403 NULL },
397404
398405 #if (NGX_HTTP_CACHE)
24892496
24902497 conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
24912498 conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
2499 conf->upstream.limit_rate = NGX_CONF_UNSET_SIZE;
24922500
24932501 conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
24942502 conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
25992607 ngx_conf_merge_size_value(conf->upstream.buffer_size,
26002608 prev->upstream.buffer_size,
26012609 (size_t) ngx_pagesize);
2610
2611 ngx_conf_merge_size_value(conf->upstream.limit_rate,
2612 prev->upstream.limit_rate, 0);
26022613
26032614 ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,
26042615 8, ngx_pagesize);
187187 ngx_conf_set_flag_slot,
188188 NGX_HTTP_LOC_CONF_OFFSET,
189189 offsetof(ngx_http_scgi_loc_conf_t, upstream.force_ranges),
190 NULL },
191
192 { ngx_string("scgi_limit_rate"),
193 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
194 ngx_conf_set_size_slot,
195 NGX_HTTP_LOC_CONF_OFFSET,
196 offsetof(ngx_http_scgi_loc_conf_t, upstream.limit_rate),
190197 NULL },
191198
192199 #if (NGX_HTTP_CACHE)
11081115
11091116 conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
11101117 conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
1118 conf->upstream.limit_rate = NGX_CONF_UNSET_SIZE;
11111119
11121120 conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
11131121 conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
11981206 ngx_conf_merge_size_value(conf->upstream.buffer_size,
11991207 prev->upstream.buffer_size,
12001208 (size_t) ngx_pagesize);
1209
1210 ngx_conf_merge_size_value(conf->upstream.limit_rate,
1211 prev->upstream.limit_rate, 0);
12011212
12021213
12031214 ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,
242242 ngx_conf_set_flag_slot,
243243 NGX_HTTP_LOC_CONF_OFFSET,
244244 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.force_ranges),
245 NULL },
246
247 { ngx_string("uwsgi_limit_rate"),
248 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
249 ngx_conf_set_size_slot,
250 NGX_HTTP_LOC_CONF_OFFSET,
251 offsetof(ngx_http_uwsgi_loc_conf_t, upstream.limit_rate),
245252 NULL },
246253
247254 #if (NGX_HTTP_CACHE)
12881295
12891296 conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
12901297 conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
1298 conf->upstream.limit_rate = NGX_CONF_UNSET_SIZE;
12911299
12921300 conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
12931301 conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
13851393 ngx_conf_merge_size_value(conf->upstream.buffer_size,
13861394 prev->upstream.buffer_size,
13871395 (size_t) ngx_pagesize);
1396
1397 ngx_conf_merge_size_value(conf->upstream.limit_rate,
1398 prev->upstream.limit_rate, 0);
13881399
13891400
13901401 ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,
25782578 p->downstream = c;
25792579 p->pool = r->pool;
25802580 p->log = c->log;
2581 p->limit_rate = u->conf->limit_rate;
2582 p->start_sec = ngx_time();
25812583
25822584 p->cacheable = u->cacheable || u->store;
25832585
32523254 ngx_http_upstream_process_upstream(ngx_http_request_t *r,
32533255 ngx_http_upstream_t *u)
32543256 {
3257 ngx_event_t *rev;
3258 ngx_event_pipe_t *p;
32553259 ngx_connection_t *c;
32563260
32573261 c = u->peer.connection;
3262 p = u->pipe;
3263 rev = c->read;
32583264
32593265 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
32603266 "http upstream process upstream");
32613267
32623268 c->log->action = "reading upstream";
32633269
3264 if (c->read->timedout) {
3265 u->pipe->upstream_error = 1;
3266 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out");
3270 if (rev->timedout) {
3271
3272 if (rev->delayed) {
3273
3274 rev->timedout = 0;
3275 rev->delayed = 0;
3276
3277 if (!rev->ready) {
3278 ngx_add_timer(rev, p->read_timeout);
3279
3280 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
3281 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
3282 }
3283
3284 return;
3285 }
3286
3287 if (ngx_event_pipe(p, 0) == NGX_ABORT) {
3288 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
3289 return;
3290 }
3291
3292 } else {
3293 p->upstream_error = 1;
3294 ngx_connection_error(c, NGX_ETIMEDOUT, "upstream timed out");
3295 }
32673296
32683297 } else {
3269 if (ngx_event_pipe(u->pipe, 0) == NGX_ABORT) {
3298
3299 if (rev->delayed) {
3300
3301 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
3302 "http upstream delayed");
3303
3304 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
3305 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
3306 }
3307
3308 return;
3309 }
3310
3311 if (ngx_event_pipe(p, 0) == NGX_ABORT) {
32703312 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
32713313 return;
32723314 }
140140
141141 size_t send_lowat;
142142 size_t buffer_size;
143 size_t limit_rate;
143144
144145 size_t busy_buffers_size;
145146 size_t max_temp_file_size;