switch to ms resolution for rate limiting

Jérôme Loyet jerome at loyet.net
Mon Nov 28 05:40:33 UTC 2022


Hello,

I'm using rate limiting within the stream module. While it works great
for long connections it does not work on request smaller than the rate
limite size for 1 second. I set up a 1gbps rate limit (limit_rate
125m) and request smaller than 125M or not limited. This is a normal
behavioir as the rate limiting is done with a second precision. This
patch change second precision to millisecond precision. From my first
tests (still on going) it seems to works better.

What guys do you think about this patch ?

Thanks
++ Jerome


# HG changeset patch
# User Jerome Loyet <jerome at loyet.net>
# Date 1669612614 -3600
#      Mon Nov 28 06:16:54 2022 +0100
# Node ID a474ad7ca3408d2b09cd7bfbc827c8df84e3bd0c
# Parent  0b360747c74e3fa7e439e0684a8cf1da2d14d8f6
switch to ms resolution for rate limiting

rate limiting use a second resolution which can be bypassed on
requests/response smaller than the configured rate for 1 second.

This patch switches to millisecond resolution to ensure
better precision of the rate limiting.

diff -r 0b360747c74e -r a474ad7ca340 src/event/ngx_event_pipe.c
--- a/src/event/ngx_event_pipe.c    Thu Nov 24 23:08:30 2022 +0400
+++ b/src/event/ngx_event_pipe.c    Mon Nov 28 06:16:54 2022 +0100
@@ -203,8 +203,8 @@
                     break;
                 }

-                limit = (off_t) p->limit_rate * (ngx_time() - p->start_sec + 1)
-                        - p->read_length;
+                limit = (off_t) p->limit_rate * (ngx_current_msec -
p->start_msec + 1)
+                        / 1000 - p->read_length;

                 if (limit <= 0) {
                     p->upstream->read->delayed = 1;
diff -r 0b360747c74e -r a474ad7ca340 src/event/ngx_event_pipe.h
--- a/src/event/ngx_event_pipe.h    Thu Nov 24 23:08:30 2022 +0400
+++ b/src/event/ngx_event_pipe.h    Mon Nov 28 06:16:54 2022 +0100
@@ -91,7 +91,7 @@
     ngx_buf_t         *buf_to_file;

     size_t             limit_rate;
-    time_t             start_sec;
+    ngx_msec_t         start_msec;

     ngx_temp_file_t   *temp_file;

diff -r 0b360747c74e -r a474ad7ca340 src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c    Thu Nov 24 23:08:30 2022 +0400
+++ b/src/http/ngx_http_upstream.c    Mon Nov 28 06:16:54 2022 +0100
@@ -3217,7 +3217,7 @@
     p->pool = r->pool;
     p->log = c->log;
     p->limit_rate = u->conf->limit_rate;
-    p->start_sec = ngx_time();
+    p->start_msec = ngx_current_msec;

     p->cacheable = u->cacheable || u->store;

diff -r 0b360747c74e -r a474ad7ca340 src/stream/ngx_stream_proxy_module.c
--- a/src/stream/ngx_stream_proxy_module.c    Thu Nov 24 23:08:30 2022 +0400
+++ b/src/stream/ngx_stream_proxy_module.c    Mon Nov 28 06:16:54 2022 +0100
@@ -435,7 +435,7 @@
     }

     u->peer.type = c->type;
-    u->start_sec = ngx_time();
+    u->start_msec = ngx_current_msec;

     c->write->handler = ngx_stream_proxy_downstream_handler;
     c->read->handler = ngx_stream_proxy_downstream_handler;
@@ -1679,8 +1679,8 @@
             && !src->read->error)
         {
             if (limit_rate) {
-                limit = (off_t) limit_rate * (ngx_time() - u->start_sec + 1)
-                        - *received;
+                limit = (off_t) limit_rate * (ngx_current_msec -
u->start_msec + 1)
+                        / 1000 - *received;

                 if (limit <= 0) {
                     src->read->delayed = 1;
diff -r 0b360747c74e -r a474ad7ca340 src/stream/ngx_stream_upstream.h
--- a/src/stream/ngx_stream_upstream.h    Thu Nov 24 23:08:30 2022 +0400
+++ b/src/stream/ngx_stream_upstream.h    Mon Nov 28 06:16:54 2022 +0100
@@ -127,7 +127,7 @@
     ngx_chain_t                       *downstream_busy;

     off_t                              received;
-    time_t                             start_sec;
+    ngx_msec_t                         start_msec;
     ngx_uint_t                         requests;
     ngx_uint_t                         responses;
     ngx_msec_t                         start_time;



More information about the nginx-devel mailing list