Using rate limitation for files smaller than the defined limit.

leki75 nginx-forum at nginx.us
Tue Jul 31 15:18:18 UTC 2012


Dear Maxim,

thank you for your reply. I changed the code you suggested:
1. changed ngx_cached_time to ngx_timeofday()
2. added 1ms to send some bytes at request start

--- src/http/ngx_http_write_filter_module.c	2012-01-18
16:07:43.000000000 +0100
+++ src/http/ngx_http_write_filter_module.c.new	2012-07-31
16:38:11.074836346 +0200
@@ -211,8 +211,17 @@
     }
 
     if (r->limit_rate) {
-        limit = r->limit_rate * (ngx_time() - r->start_sec + 1)
-                - (c->sent - clcf->limit_rate_after);
+        ngx_time_t  *t = ngx_timeofday();
+
+        if (t->msec >= r->start_msec) {
+            limit = r->limit_rate * (t->sec - r->start_sec)
+                    + r->limit_rate * (t->msec - r->start_msec + 1) /
1000
+                    - (c->sent - clcf->limit_rate_after);
+        } else {
+            limit = r->limit_rate * (t->sec - r->start_sec - 1)
+                    + r->limit_rate * (t->msec - r->start_msec + 1001)
/ 1000
+                    - (c->sent - clcf->limit_rate_after);
+        }
 
         if (limit <= 0) {
             c->write->delayed = 1;

Can we avoid network segmentation by setting the following configuration
directives?
    tcp_nodelay  off;
    tcp_nopush  on;

Do you have any other suggestions how to rate limit objects, which size
is smaller than specified in limit_rate, other way?

Regards,
    Gabor

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,229094,229147#msg-229147



More information about the nginx mailing list