[PATCH] Proxy: altered limit_rate to support variables

J Carter jordanc.carter at outlook.com
Thu Dec 28 10:48:20 UTC 2023


Hello,

Thanks for the review and feedback.

On Tue, 26 Dec 2023 19:07:37 +0400
Sergey Kandaurov <pluknet at nginx.com> wrote:

> > On 26 Nov 2023, at 03:37, J Carter <jordanc.carter at outlook.com> wrote:
> > 
> > # HG changeset patch
> > # User J Carter <jordan.carter at outlook.com>
> > # Date 1700949429 0
> > #      Sat Nov 25 21:57:09 2023 +0000
> > # Node ID 98306e705015758eab0a05103d90e6bdb1da2819
> > # Parent  f366007dd23a6ce8e8427c1b3042781b618a2ade
> > Proxy: altered limit_rate to support variables  
> 
> Since it changes the upstream interface, I'd rather say:
> 
> Upstream: variables support in proxy_limit_rate and friends.
> 

Yep, makes sense.

>
> The change looks good to me, also it compliments variables support
> in the stream proxy module's directives.
> Any particular use-cases you'd like to share for this functionality?
> 

There are several, many are quite 'involved'.

A variable proxy_limit_rate (driven by external means), combined with 
max_conns set on upstream servers gives a reasonable method of 
dynamically capping upstream bandwidth utilization.

One scenario in which this has come up is when nginx is used a caching 
server, as often upstream servers cannot support 100% of the 'real' 
traffic load. 

When the cache is cleared or many items expire concurrently, requests
reaching the upstream can spike and it is necessary to adjust (down) 
proxy_limit_rate in a semi continuous fashion to prevent overloading
upstreams as they serve responses.

A novel way of showing this using only core modules is: 

upstream backend {
    server 127.0.0.1:8080 max_conns=100;
    ...
}

... <proxy-cache-path, and related directives>

server {
    listen 80;
    location / {
        ...
        proxy_pass http://backend;
        proxy_hide_header rate;
        proxy_limit_rate $upstream_http_rate;
    }
}

...

server {
    listen 8080;
    location / {
        #serve a file
        ...
        add_header rate $my_rate;
    }
}

Of course, this is most useful where 'backend' isn't nginx, and doesn't
have it's own client side bandwidth limiting (ie. limit_rate, 
limit_rate_after, limit_conn) - I've just used it here for this example for
convenience.

> > 
> > diff -r f366007dd23a -r 98306e705015 src/http/modules/ngx_http_fastcgi_module.c
> > --- a/src/http/modules/ngx_http_fastcgi_module.c	Tue Nov 14 15:26:02 2023 +0400
> > +++ b/src/http/modules/ngx_http_fastcgi_module.c	Sat Nov 25 21:57:09 2023 +0000
> > @@ -375,7 +375,7 @@
> > 
> >     { ngx_string("fastcgi_limit_rate"),
> >       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
> > -      ngx_conf_set_size_slot,
> > +      ngx_http_set_complex_value_size_slot,
> >       NGX_HTTP_LOC_CONF_OFFSET,
> >       offsetof(ngx_http_fastcgi_loc_conf_t, upstream.limit_rate),
> >       NULL },
> > @@ -2898,7 +2898,7 @@
> > 
> >     conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
> >     conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
> > -    conf->upstream.limit_rate = NGX_CONF_UNSET_SIZE;
> > +    conf->upstream.limit_rate = NGX_CONF_UNSET_PTR;
> > 
> >     conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
> >     conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
> > @@ -3015,8 +3015,8 @@
> >                               prev->upstream.buffer_size,
> >                               (size_t) ngx_pagesize);
> > 
> > -    ngx_conf_merge_size_value(conf->upstream.limit_rate,
> > -                              prev->upstream.limit_rate, 0);
> > +    ngx_conf_merge_ptr_value(conf->upstream.limit_rate,
> > +                              prev->upstream.limit_rate, NULL);
> > 
> > 
> >     ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,
> > diff -r f366007dd23a -r 98306e705015 src/http/modules/ngx_http_proxy_module.c
> > --- a/src/http/modules/ngx_http_proxy_module.c	Tue Nov 14 15:26:02 2023 +0400
> > +++ b/src/http/modules/ngx_http_proxy_module.c	Sat Nov 25 21:57:09 2023 +0000
> > @@ -494,7 +494,7 @@
> > 
> >     { ngx_string("proxy_limit_rate"),
> >       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
> > -      ngx_conf_set_size_slot,
> > +      ngx_http_set_complex_value_size_slot,
> >       NGX_HTTP_LOC_CONF_OFFSET,
> >       offsetof(ngx_http_proxy_loc_conf_t, upstream.limit_rate),
> >       NULL },
> > @@ -3371,7 +3371,7 @@
> > 
> >     conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
> >     conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
> > -    conf->upstream.limit_rate = NGX_CONF_UNSET_SIZE;
> > +    conf->upstream.limit_rate = NGX_CONF_UNSET_PTR;
> > 
> >     conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
> >     conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
> > @@ -3515,8 +3515,8 @@
> >                               prev->upstream.buffer_size,
> >                               (size_t) ngx_pagesize);
> > 
> > -    ngx_conf_merge_size_value(conf->upstream.limit_rate,
> > -                              prev->upstream.limit_rate, 0);
> > +    ngx_conf_merge_ptr_value(conf->upstream.limit_rate,
> > +                              prev->upstream.limit_rate, NULL);
> > 
> >     ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,
> >                               8, ngx_pagesize);
> > diff -r f366007dd23a -r 98306e705015 src/http/modules/ngx_http_scgi_module.c
> > --- a/src/http/modules/ngx_http_scgi_module.c	Tue Nov 14 15:26:02 2023 +0400
> > +++ b/src/http/modules/ngx_http_scgi_module.c	Sat Nov 25 21:57:09 2023 +0000
> > @@ -223,7 +223,7 @@
> > 
> >     { ngx_string("scgi_limit_rate"),
> >       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
> > -      ngx_conf_set_size_slot,
> > +      ngx_http_set_complex_value_size_slot,
> >       NGX_HTTP_LOC_CONF_OFFSET,
> >       offsetof(ngx_http_scgi_loc_conf_t, upstream.limit_rate),
> >       NULL },
> > @@ -1301,7 +1301,7 @@
> > 
> >     conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
> >     conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
> > -    conf->upstream.limit_rate = NGX_CONF_UNSET_SIZE;
> > +    conf->upstream.limit_rate = NGX_CONF_UNSET_PTR;
> > 
> >     conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
> >     conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
> > @@ -1413,8 +1413,8 @@
> >                               prev->upstream.buffer_size,
> >                               (size_t) ngx_pagesize);
> > 
> > -    ngx_conf_merge_size_value(conf->upstream.limit_rate,
> > -                              prev->upstream.limit_rate, 0);
> > +    ngx_conf_merge_ptr_value(conf->upstream.limit_rate,
> > +                              prev->upstream.limit_rate, NULL);
> > 
> > 
> >     ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,
> > diff -r f366007dd23a -r 98306e705015 src/http/modules/ngx_http_uwsgi_module.c
> > --- a/src/http/modules/ngx_http_uwsgi_module.c	Tue Nov 14 15:26:02 2023 +0400
> > +++ b/src/http/modules/ngx_http_uwsgi_module.c	Sat Nov 25 21:57:09 2023 +0000
> > @@ -289,7 +289,7 @@
> > 
> >     { ngx_string("uwsgi_limit_rate"),
> >       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
> > -      ngx_conf_set_size_slot,
> > +      ngx_http_set_complex_value_size_slot,
> >       NGX_HTTP_LOC_CONF_OFFSET,
> >       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.limit_rate),
> >       NULL },
> > @@ -1532,7 +1532,7 @@
> > 
> >     conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
> >     conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
> > -    conf->upstream.limit_rate = NGX_CONF_UNSET_SIZE;
> > +    conf->upstream.limit_rate = NGX_CONF_UNSET_PTR;
> > 
> >     conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
> >     conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
> > @@ -1656,8 +1656,8 @@
> >                               prev->upstream.buffer_size,
> >                               (size_t) ngx_pagesize);
> > 
> > -    ngx_conf_merge_size_value(conf->upstream.limit_rate,
> > -                              prev->upstream.limit_rate, 0);
> > +    ngx_conf_merge_ptr_value(conf->upstream.limit_rate,
> > +                              prev->upstream.limit_rate, NULL);
> > 
> > 
> >     ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,
> > diff -r f366007dd23a -r 98306e705015 src/http/ngx_http_upstream.c
> > --- a/src/http/ngx_http_upstream.c	Tue Nov 14 15:26:02 2023 +0400
> > +++ b/src/http/ngx_http_upstream.c	Sat Nov 25 21:57:09 2023 +0000
> > @@ -3236,7 +3236,7 @@
> >     p->downstream = c;
> >     p->pool = r->pool;
> >     p->log = c->log;
> > -    p->limit_rate = u->conf->limit_rate;
> > +    p->limit_rate = ngx_http_complex_value_size(r, u->conf->limit_rate, 0);
> >     p->start_sec = ngx_time();
> > 
> >     p->cacheable = u->cacheable || u->store;
> > diff -r f366007dd23a -r 98306e705015 src/http/ngx_http_upstream.h
> > --- a/src/http/ngx_http_upstream.h	Tue Nov 14 15:26:02 2023 +0400
> > +++ b/src/http/ngx_http_upstream.h	Sat Nov 25 21:57:09 2023 +0000
> > @@ -156,7 +156,7 @@
> > 
> >     size_t                           send_lowat;
> >     size_t                           buffer_size;
> > -    size_t                           limit_rate;
> > +    ngx_http_complex_value_t        *limit_rate;
> > 
> >     size_t                           busy_buffers_size;
> >     size_t                           max_temp_file_size;  
> 


More information about the nginx-devel mailing list