limit_rate_after support variables
Miroslav Novy
miranovy at gmail.com
Wed Oct 17 10:49:13 UTC 2018
Hello,
I prepare patch on actual sources. Settings limit_rate and limit_rate_after
works good. Please make code review, our testing and merge to main branche.
Thank you
Miroslav Nový
Example of configration:
location / {
root /var/www/default/;
index index.html index.htm;
set $my_limit_rate 4k;
set $my_limit_rate_after 4m;
limit_rate $my_limit_rate;
limit_rate_after $my_limit_rate_after;
access_by_lua_block {
ngx.var.my_limit_rate = '2k'
ngx.var.my_limit_rate_after = '10m'
}
}
# HG changeset patch
# User Miroslav Nový <miranovy at gmail.com>
# Date 1539773045 0
# Wed Oct 17 10:44:05 2018 +0000
# Node ID 0de0d409a946b9f33284c036fdf3dcdaec0853c2
# Parent 8b68d50090e4f134a35da60146fefd5e63770759
limit_rate and limit_rate_after support variables
diff -r 8b68d50090e4 -r 0de0d409a946 src/http/ngx_http_core_module.c
--- a/src/http/ngx_http_core_module.c Wed Oct 03 14:08:51 2018 +0300
+++ b/src/http/ngx_http_core_module.c Wed Oct 17 10:44:05 2018 +0000
@@ -479,18 +479,18 @@
{ ngx_string("limit_rate"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
|NGX_CONF_TAKE1,
- ngx_conf_set_size_slot,
+ ngx_http_set_complex_value_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_core_loc_conf_t, limit_rate),
- NULL },
+ &ngx_http_complex_value_size_p },
{ ngx_string("limit_rate_after"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
|NGX_CONF_TAKE1,
- ngx_conf_set_size_slot,
+ ngx_http_set_complex_value_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_core_loc_conf_t, limit_rate_after),
- NULL },
+ &ngx_http_complex_value_size_p },
{ ngx_string("keepalive_timeout"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
@@ -1281,10 +1281,6 @@
r->connection->tcp_nopush = NGX_TCP_NOPUSH_DISABLED;
}
- if (r->limit_rate == 0) {
- r->limit_rate = clcf->limit_rate;
- }
-
if (clcf->handler) {
r->content_handler = clcf->handler;
}
@@ -3362,6 +3358,8 @@
* clcf->exact_match = 0;
* clcf->auto_redirect = 0;
* clcf->alias = 0;
+ * clcf->limit_rate = NULL;
+ * clcf->limit_rate_after = NULL;
* clcf->gzip_proxied = 0;
* clcf->keepalive_disable = 0;
*/
@@ -3392,8 +3390,6 @@
clcf->send_timeout = NGX_CONF_UNSET_MSEC;
clcf->send_lowat = NGX_CONF_UNSET_SIZE;
clcf->postpone_output = NGX_CONF_UNSET_SIZE;
- clcf->limit_rate = NGX_CONF_UNSET_SIZE;
- clcf->limit_rate_after = NGX_CONF_UNSET_SIZE;
clcf->keepalive_timeout = NGX_CONF_UNSET_MSEC;
clcf->keepalive_header = NGX_CONF_UNSET;
clcf->keepalive_requests = NGX_CONF_UNSET_UINT;
@@ -3581,6 +3577,14 @@
ngx_conf_merge_msec_value(conf->client_body_timeout,
prev->client_body_timeout, 60000);
+ if (conf->limit_rate == NULL) {
+ conf->limit_rate = prev->limit_rate;
+ }
+
+ if (conf->limit_rate_after == NULL) {
+ conf->limit_rate_after = prev->limit_rate_after;
+ }
+
ngx_conf_merge_bitmask_value(conf->keepalive_disable,
prev->keepalive_disable,
(NGX_CONF_BITMASK_SET
@@ -3622,9 +3626,7 @@
ngx_conf_merge_size_value(conf->send_lowat, prev->send_lowat, 0);
ngx_conf_merge_size_value(conf->postpone_output, prev->postpone_output,
1460);
- ngx_conf_merge_size_value(conf->limit_rate, prev->limit_rate, 0);
- ngx_conf_merge_size_value(conf->limit_rate_after,
prev->limit_rate_after,
- 0);
+
ngx_conf_merge_msec_value(conf->keepalive_timeout,
prev->keepalive_timeout, 75000);
ngx_conf_merge_sec_value(conf->keepalive_header,
diff -r 8b68d50090e4 -r 0de0d409a946 src/http/ngx_http_core_module.h
--- a/src/http/ngx_http_core_module.h Wed Oct 03 14:08:51 2018 +0300
+++ b/src/http/ngx_http_core_module.h Wed Oct 17 10:44:05 2018 +0000
@@ -350,8 +350,9 @@
size_t client_body_buffer_size; /* client_body_buffer_size */
size_t send_lowat; /* send_lowat */
size_t postpone_output; /* postpone_output */
- size_t limit_rate; /* limit_rate */
- size_t limit_rate_after; /* limit_rate_after */
+ ngx_http_complex_value_t *limit_rate; /* limit_rate */
+ ngx_http_complex_value_t *limit_rate_after;
+ /* limit_rate_after */
size_t sendfile_max_chunk; /* sendfile_max_chunk */
size_t read_ahead; /* read_ahead */
size_t subrequest_output_buffer_size;
diff -r 8b68d50090e4 -r 0de0d409a946 src/http/ngx_http_script.c
--- a/src/http/ngx_http_script.c Wed Oct 03 14:08:51 2018 +0300
+++ b/src/http/ngx_http_script.c Wed Oct 17 10:44:05 2018 +0000
@@ -9,7 +9,8 @@
#include <ngx_core.h>
#include <ngx_http.h>
-
+static char *ngx_http_complex_value_set_size(ngx_conf_t *cf, void *post,
+ void *data);
static ngx_int_t ngx_http_script_init_arrays(ngx_http_script_compile_t
*sc);
static ngx_int_t ngx_http_script_done(ngx_http_script_compile_t *sc);
static ngx_int_t ngx_http_script_add_copy_code(ngx_http_script_compile_t
*sc,
@@ -31,6 +32,8 @@
static uintptr_t ngx_http_script_exit_code = (uintptr_t) NULL;
+ngx_conf_post_handler_pt ngx_http_complex_value_size_p =
+ ngx_http_complex_value_set_size;
void
ngx_http_script_flush_complex_value(ngx_http_request_t *r,
@@ -103,6 +106,23 @@
return NGX_OK;
}
+ngx_int_t
+ngx_http_complex_value_size(ngx_http_request_t *r,
+ ngx_http_complex_value_t *val, ngx_str_t *value, ssize_t *size)
+{
+ if (val->lengths == NULL) {
+ *size = val->u.size;
+ return NGX_OK;
+ }
+
+ if (ngx_http_complex_value(r, val, value) != NGX_OK) {
+ return NGX_ERROR;
+ }
+
+ *size = ngx_parse_size(value);
+
+ return NGX_OK;
+}
ngx_int_t
ngx_http_compile_complex_value(ngx_http_compile_complex_value_t *ccv)
@@ -214,6 +234,7 @@
char *p = conf;
ngx_str_t *value;
+ ngx_conf_post_t *post;
ngx_http_complex_value_t **cv;
ngx_http_compile_complex_value_t ccv;
@@ -240,6 +261,29 @@
return NGX_CONF_ERROR;
}
+ if (cmd->post) {
+ post = cmd->post;
+ return post->post_handler(cf, post, *cv);
+ }
+
+ return NGX_CONF_OK;
+}
+
+
+static char *
+ngx_http_complex_value_set_size(ngx_conf_t *cf, void *post, void *data)
+{
+ ngx_http_complex_value_t *cv = data;
+
+ if (cv->lengths) {
+ return NGX_CONF_OK;
+ }
+
+ cv->u.size = ngx_parse_size(&cv->value);
+ if (cv->u.size == (size_t) NGX_ERROR) {
+ return "invalid value";
+ }
+
return NGX_CONF_OK;
}
diff -r 8b68d50090e4 -r 0de0d409a946 src/http/ngx_http_script.h
--- a/src/http/ngx_http_script.h Wed Oct 03 14:08:51 2018 +0300
+++ b/src/http/ngx_http_script.h Wed Oct 17 10:44:05 2018 +0000
@@ -68,6 +68,10 @@
ngx_uint_t *flushes;
void *lengths;
void *values;
+
+ union {
+ size_t size;
+ } u;
} ngx_http_complex_value_t;
@@ -205,6 +209,8 @@
void ngx_http_script_flush_complex_value(ngx_http_request_t *r,
ngx_http_complex_value_t *val);
+ngx_int_t ngx_http_complex_value_size(ngx_http_request_t *r,
+ ngx_http_complex_value_t *val, ngx_str_t *value, ssize_t *size);
ngx_int_t ngx_http_complex_value(ngx_http_request_t *r,
ngx_http_complex_value_t *val, ngx_str_t *value);
ngx_int_t ngx_http_compile_complex_value(ngx_http_compile_complex_value_t
*ccv);
@@ -253,5 +259,6 @@
void ngx_http_script_var_code(ngx_http_script_engine_t *e);
void ngx_http_script_nop_code(ngx_http_script_engine_t *e);
+extern ngx_conf_post_handler_pt ngx_http_complex_value_size_p;
#endif /* _NGX_HTTP_SCRIPT_H_INCLUDED_ */
diff -r 8b68d50090e4 -r 0de0d409a946 src/http/ngx_http_write_filter_module.c
--- a/src/http/ngx_http_write_filter_module.c Wed Oct 03 14:08:51 2018 +0300
+++ b/src/http/ngx_http_write_filter_module.c Wed Oct 17 10:44:05 2018 +0000
@@ -48,6 +48,8 @@
ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in)
{
off_t size, sent, nsent, limit;
+ ssize_t limit_rate, limit_rate_after;
+ ngx_str_t val;
ngx_uint_t last, flush, sync;
ngx_msec_t delay;
ngx_chain_t *cl, *ln, **ll, *chain;
@@ -218,9 +220,38 @@
return NGX_ERROR;
}
+ if (r->limit_rate == 0
+ && clcf->limit_rate
+ && ngx_http_complex_value_size(r, clcf->limit_rate, &val,
+ &limit_rate)
+ == NGX_OK)
+ {
+ if (limit_rate_after != NGX_ERROR) {
+ r->limit_rate = limit_rate;
+
+ } else if (val.len) {
+ ngx_log_error(NGX_LOG_ERR, c->log, 0,
+ "invalid \"limit_rate\" value \"%V\"",
+ &val);
+ }
+ }
+
+ ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http limit rate
\"%z\"", r->limit_rate);
+
if (r->limit_rate) {
- if (r->limit_rate_after == 0) {
- r->limit_rate_after = clcf->limit_rate_after;
+ if (r->limit_rate_after == 0
+ && clcf->limit_rate_after
+ && ngx_http_complex_value_size(r, clcf->limit_rate_after, &val,
+ &limit_rate_after)
+ == NGX_OK)
+ {
+ if (limit_rate_after != NGX_ERROR) {
+ r->limit_rate_after = limit_rate_after;
+ } else if (val.len) {
+ ngx_log_error(NGX_LOG_ERR, c->log, 0,
+ "invalid \"limit_rate_after\" value \"%V\"",
+ &val);
+ }
}
limit = (off_t) r->limit_rate * (ngx_time() - r->start_sec + 1)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20181017/8796ded0/attachment-0001.html>
More information about the nginx-devel
mailing list