<div dir="ltr"><div dir="ltr"><div dir="ltr">Hello,<div><br></div><div>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.</div><div>Thank you</div><div>Miroslav Nový</div><div><br></div><div>Example of configration:</div><div><div>  location / {</div><div><span style="white-space:pre">       </span>    root /var/www/default/;</div><div>            index  index.html index.htm;</div><div><br></div><div><span style="white-space:pre">  </span>    set $my_limit_rate 4k;</div><div><span style="white-space:pre">  </span>    set $my_limit_rate_after 4m;</div><div><br></div><div><span style="white-space:pre">   </span>    limit_rate $my_limit_rate;</div><div><span style="white-space:pre">      </span>    limit_rate_after $my_limit_rate_after;</div><div><br></div><div><span style="white-space:pre"> </span>    access_by_lua_block {</div><div><span style="white-space:pre">           </span>ngx.var.my_limit_rate = '2k'</div><div><span style="white-space:pre">          </span>ngx.var.my_limit_rate_after = '10m'</div><div><span style="white-space:pre">   </span>    }</div><div>        }<br></div></div><div><br></div><div><br></div><div><div># HG changeset patch</div><div># User Miroslav Nový <<a href="mailto:miranovy@gmail.com">miranovy@gmail.com</a>></div><div># Date 1539773045 0</div><div>#      Wed Oct 17 10:44:05 2018 +0000</div><div># Node ID 0de0d409a946b9f33284c036fdf3dcdaec0853c2</div><div># Parent  8b68d50090e4f134a35da60146fefd5e63770759</div><div>limit_rate and limit_rate_after support variables</div><div><br></div><div>diff -r 8b68d50090e4 -r 0de0d409a946 src/http/ngx_http_core_module.c</div><div>--- a/src/http/ngx_http_core_module.c<span style="white-space:pre">   </span>Wed Oct 03 14:08:51 2018 +0300</div><div>+++ b/src/http/ngx_http_core_module.c<span style="white-space:pre">   </span>Wed Oct 17 10:44:05 2018 +0000</div><div>@@ -479,18 +479,18 @@</div><div>     { ngx_string("limit_rate"),</div><div>       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF</div><div>                         |NGX_CONF_TAKE1,</div><div>-      ngx_conf_set_size_slot,</div><div>+      ngx_http_set_complex_value_slot,</div><div>       NGX_HTTP_LOC_CONF_OFFSET,</div><div>       offsetof(ngx_http_core_loc_conf_t, limit_rate),</div><div>-      NULL },</div><div>+      &ngx_http_complex_value_size_p },</div><div> </div><div>     { ngx_string("limit_rate_after"),</div><div>       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF</div><div>                         |NGX_CONF_TAKE1,</div><div>-      ngx_conf_set_size_slot,</div><div>+      ngx_http_set_complex_value_slot,</div><div>       NGX_HTTP_LOC_CONF_OFFSET,</div><div>       offsetof(ngx_http_core_loc_conf_t, limit_rate_after),</div><div>-      NULL },</div><div>+      &ngx_http_complex_value_size_p },</div><div> </div><div>     { ngx_string("keepalive_timeout"),</div><div>       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,</div><div>@@ -1281,10 +1281,6 @@</div><div>         r->connection->tcp_nopush = NGX_TCP_NOPUSH_DISABLED;</div><div>     }</div><div> </div><div>-    if (r->limit_rate == 0) {</div><div>-        r->limit_rate = clcf->limit_rate;</div><div>-    }</div><div>-</div><div>     if (clcf->handler) {</div><div>         r->content_handler = clcf->handler;</div><div>     }</div><div>@@ -3362,6 +3358,8 @@</div><div>      *     clcf->exact_match = 0;</div><div>      *     clcf->auto_redirect = 0;</div><div>      *     clcf->alias = 0;</div><div>+     *     clcf->limit_rate = NULL;</div><div>+     *     clcf->limit_rate_after = NULL;</div><div>      *     clcf->gzip_proxied = 0;</div><div>      *     clcf->keepalive_disable = 0;</div><div>      */</div><div>@@ -3392,8 +3390,6 @@</div><div>     clcf->send_timeout = NGX_CONF_UNSET_MSEC;</div><div>     clcf->send_lowat = NGX_CONF_UNSET_SIZE;</div><div>     clcf->postpone_output = NGX_CONF_UNSET_SIZE;</div><div>-    clcf->limit_rate = NGX_CONF_UNSET_SIZE;</div><div>-    clcf->limit_rate_after = NGX_CONF_UNSET_SIZE;</div><div>     clcf->keepalive_timeout = NGX_CONF_UNSET_MSEC;</div><div>     clcf->keepalive_header = NGX_CONF_UNSET;</div><div>     clcf->keepalive_requests = NGX_CONF_UNSET_UINT;</div><div>@@ -3581,6 +3577,14 @@</div><div>     ngx_conf_merge_msec_value(conf->client_body_timeout,</div><div>                               prev->client_body_timeout, 60000);</div><div> </div><div>+    if (conf->limit_rate == NULL) {</div><div>+        conf->limit_rate = prev->limit_rate;</div><div>+    }</div><div>+</div><div>+    if (conf->limit_rate_after == NULL) {</div><div>+        conf->limit_rate_after = prev->limit_rate_after;</div><div>+    }</div><div>+</div><div>     ngx_conf_merge_bitmask_value(conf->keepalive_disable,</div><div>                               prev->keepalive_disable,</div><div>                               (NGX_CONF_BITMASK_SET</div><div>@@ -3622,9 +3626,7 @@</div><div>     ngx_conf_merge_size_value(conf->send_lowat, prev->send_lowat, 0);</div><div>     ngx_conf_merge_size_value(conf->postpone_output, prev->postpone_output,</div><div>                               1460);</div><div>-    ngx_conf_merge_size_value(conf->limit_rate, prev->limit_rate, 0);</div><div>-    ngx_conf_merge_size_value(conf->limit_rate_after, prev->limit_rate_after,</div><div>-                              0);</div><div>+</div><div>     ngx_conf_merge_msec_value(conf->keepalive_timeout,</div><div>                               prev->keepalive_timeout, 75000);</div><div>     ngx_conf_merge_sec_value(conf->keepalive_header,</div><div>diff -r 8b68d50090e4 -r 0de0d409a946 src/http/ngx_http_core_module.h</div><div>--- a/src/http/ngx_http_core_module.h<span style="white-space:pre">        </span>Wed Oct 03 14:08:51 2018 +0300</div><div>+++ b/src/http/ngx_http_core_module.h<span style="white-space:pre">   </span>Wed Oct 17 10:44:05 2018 +0000</div><div>@@ -350,8 +350,9 @@</div><div>     size_t        client_body_buffer_size; /* client_body_buffer_size */</div><div>     size_t        send_lowat;              /* send_lowat */</div><div>     size_t        postpone_output;         /* postpone_output */</div><div>-    size_t        limit_rate;              /* limit_rate */</div><div>-    size_t        limit_rate_after;        /* limit_rate_after */</div><div>+    ngx_http_complex_value_t  *limit_rate; /* limit_rate */</div><div>+    ngx_http_complex_value_t  *limit_rate_after;</div><div>+                                           /* limit_rate_after */</div><div>     size_t        sendfile_max_chunk;      /* sendfile_max_chunk */</div><div>     size_t        read_ahead;              /* read_ahead */</div><div>     size_t        subrequest_output_buffer_size;</div><div>diff -r 8b68d50090e4 -r 0de0d409a946 src/http/ngx_http_script.c</div><div>--- a/src/http/ngx_http_script.c<span style="white-space:pre">    </span>Wed Oct 03 14:08:51 2018 +0300</div><div>+++ b/src/http/ngx_http_script.c<span style="white-space:pre">        </span>Wed Oct 17 10:44:05 2018 +0000</div><div>@@ -9,7 +9,8 @@</div><div> #include <ngx_core.h></div><div> #include <ngx_http.h></div><div> </div><div>-</div><div>+static char *ngx_http_complex_value_set_size(ngx_conf_t *cf, void *post,</div><div>+    void *data);</div><div> static ngx_int_t ngx_http_script_init_arrays(ngx_http_script_compile_t *sc);</div><div> static ngx_int_t ngx_http_script_done(ngx_http_script_compile_t *sc);</div><div> static ngx_int_t ngx_http_script_add_copy_code(ngx_http_script_compile_t *sc,</div><div>@@ -31,6 +32,8 @@</div><div> </div><div> static uintptr_t ngx_http_script_exit_code = (uintptr_t) NULL;</div><div> </div><div>+ngx_conf_post_handler_pt  ngx_http_complex_value_size_p =</div><div>+    ngx_http_complex_value_set_size;</div><div> </div><div> void</div><div> ngx_http_script_flush_complex_value(ngx_http_request_t *r,</div><div>@@ -103,6 +106,23 @@</div><div>     return NGX_OK;</div><div> }</div><div> </div><div>+ngx_int_t</div><div>+ngx_http_complex_value_size(ngx_http_request_t *r,</div><div>+    ngx_http_complex_value_t *val, ngx_str_t *value, ssize_t *size)</div><div>+{</div><div>+    if (val->lengths == NULL) {</div><div>+        *size = val->u.size;</div><div>+        return NGX_OK;</div><div>+    }</div><div>+</div><div>+    if (ngx_http_complex_value(r, val, value) != NGX_OK) {</div><div>+        return NGX_ERROR;</div><div>+    }</div><div>+</div><div>+    *size = ngx_parse_size(value);</div><div>+</div><div>+    return NGX_OK;</div><div>+}</div><div> </div><div> ngx_int_t</div><div> ngx_http_compile_complex_value(ngx_http_compile_complex_value_t *ccv)</div><div>@@ -214,6 +234,7 @@</div><div>     char  *p = conf;</div><div> </div><div>     ngx_str_t                          *value;</div><div>+    ngx_conf_post_t                    *post;</div><div>     ngx_http_complex_value_t          **cv;</div><div>     ngx_http_compile_complex_value_t    ccv;</div><div> </div><div>@@ -240,6 +261,29 @@</div><div>         return NGX_CONF_ERROR;</div><div>     }</div><div> </div><div>+    if (cmd->post) {</div><div>+        post = cmd->post;</div><div>+        return post->post_handler(cf, post, *cv);</div><div>+    }</div><div>+</div><div>+    return NGX_CONF_OK;</div><div>+}</div><div>+</div><div>+</div><div>+static char *</div><div>+ngx_http_complex_value_set_size(ngx_conf_t *cf, void *post, void *data)</div><div>+{</div><div>+    ngx_http_complex_value_t  *cv = data;</div><div>+</div><div>+    if (cv->lengths) {</div><div>+        return NGX_CONF_OK;</div><div>+    }</div><div>+</div><div>+    cv->u.size = ngx_parse_size(&cv->value);</div><div>+    if (cv->u.size == (size_t) NGX_ERROR) {</div><div>+        return "invalid value";</div><div>+    }</div><div>+</div><div>     return NGX_CONF_OK;</div><div> }</div><div> </div><div>diff -r 8b68d50090e4 -r 0de0d409a946 src/http/ngx_http_script.h</div><div>--- a/src/http/ngx_http_script.h<span style="white-space:pre">     </span>Wed Oct 03 14:08:51 2018 +0300</div><div>+++ b/src/http/ngx_http_script.h<span style="white-space:pre">        </span>Wed Oct 17 10:44:05 2018 +0000</div><div>@@ -68,6 +68,10 @@</div><div>     ngx_uint_t                 *flushes;</div><div>     void                       *lengths;</div><div>     void                       *values;</div><div>+</div><div>+    union {</div><div>+        size_t                  size;</div><div>+    } u;</div><div> } ngx_http_complex_value_t;</div><div> </div><div> </div><div>@@ -205,6 +209,8 @@</div><div> </div><div> void ngx_http_script_flush_complex_value(ngx_http_request_t *r,</div><div>     ngx_http_complex_value_t *val);</div><div>+ngx_int_t ngx_http_complex_value_size(ngx_http_request_t *r,</div><div>+    ngx_http_complex_value_t *val, ngx_str_t *value, ssize_t *size);</div><div> ngx_int_t ngx_http_complex_value(ngx_http_request_t *r,</div><div>     ngx_http_complex_value_t *val, ngx_str_t *value);</div><div> ngx_int_t ngx_http_compile_complex_value(ngx_http_compile_complex_value_t *ccv);</div><div>@@ -253,5 +259,6 @@</div><div> void ngx_http_script_var_code(ngx_http_script_engine_t *e);</div><div> void ngx_http_script_nop_code(ngx_http_script_engine_t *e);</div><div> </div><div>+extern ngx_conf_post_handler_pt  ngx_http_complex_value_size_p;</div><div> </div><div> #endif /* _NGX_HTTP_SCRIPT_H_INCLUDED_ */</div><div>diff -r 8b68d50090e4 -r 0de0d409a946 src/http/ngx_http_write_filter_module.c</div><div>--- a/src/http/ngx_http_write_filter_module.c<span style="white-space:pre">     </span>Wed Oct 03 14:08:51 2018 +0300</div><div>+++ b/src/http/ngx_http_write_filter_module.c<span style="white-space:pre">   </span>Wed Oct 17 10:44:05 2018 +0000</div><div>@@ -48,6 +48,8 @@</div><div> ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in)</div><div> {</div><div>     off_t                      size, sent, nsent, limit;</div><div>+    ssize_t                    limit_rate, limit_rate_after;</div><div>+    ngx_str_t                  val;</div><div>     ngx_uint_t                 last, flush, sync;</div><div>     ngx_msec_t                 delay;</div><div>     ngx_chain_t               *cl, *ln, **ll, *chain;</div><div>@@ -218,9 +220,38 @@</div><div>         return NGX_ERROR;</div><div>     }</div><div> </div><div>+    if (r->limit_rate == 0</div><div>+        && clcf->limit_rate</div><div>+        && ngx_http_complex_value_size(r, clcf->limit_rate, &val,</div><div>+                                       &limit_rate)</div><div>+        == NGX_OK)</div><div>+    {</div><div>+        if (limit_rate_after != NGX_ERROR) {</div><div>+            r->limit_rate = limit_rate;</div><div>+</div><div>+        } else if (val.len) {</div><div>+            ngx_log_error(NGX_LOG_ERR, c->log, 0,</div><div>+                          "invalid \"limit_rate\" value \"%V\"",</div><div>+                          &val);</div><div>+        }</div><div>+    }</div><div>+</div><div>+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "http limit rate \"%z\"", r->limit_rate);</div><div>+</div><div>     if (r->limit_rate) {</div><div>-        if (r->limit_rate_after == 0) {</div><div>-            r->limit_rate_after = clcf->limit_rate_after;</div><div>+        if (r->limit_rate_after == 0</div><div>+            && clcf->limit_rate_after</div><div>+            && ngx_http_complex_value_size(r, clcf->limit_rate_after, &val,</div><div>+                                           &limit_rate_after)</div><div>+               == NGX_OK)</div><div>+        {</div><div>+            if (limit_rate_after != NGX_ERROR) {</div><div>+                r->limit_rate_after = limit_rate_after;</div><div>+            } else if (val.len) {</div><div>+                ngx_log_error(NGX_LOG_ERR, c->log, 0,</div><div>+                              "invalid \"limit_rate_after\" value \"%V\"",</div><div>+                              &val);</div><div>+            }</div><div>         }</div><div> </div><div>         limit = (off_t) r->limit_rate * (ngx_time() - r->start_sec + 1)</div></div><div><br></div></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
</blockquote></div></div></div>