diff src/http/ngx_http_write_filter_module.c @ 182:13710a1813ad NGINX_0_3_38

nginx 0.3.38 *) Feature: the ngx_http_dav_module. *) Change: the ngx_http_perl_module optimizations. Thanks to Sergey Skvortsov. *) Feature: the ngx_http_perl_module supports the $r->request_body_file method. *) Feature: the "client_body_in_file_only" directive. *) Workaround: now on disk overflow nginx tries to write access logs once a second only. Thanks to Anton Yuzhaninov and Maxim Dounin. *) Bugfix: now the "limit_rate" directive more precisely limits rate if rate is more than 100 Kbyte/s. Thanks to ForJest. *) Bugfix: now the IMAP/POP3 proxy escapes the "\r" and "\n" symbols in login and password to pass authorization server. Thanks to Maxim Dounin.
author Igor Sysoev <http://sysoev.ru>
date Fri, 14 Apr 2006 00:00:00 +0400
parents 36af50a5582d
children 71ff1e2b484a
line wrap: on
line diff
--- a/src/http/ngx_http_write_filter_module.c
+++ b/src/http/ngx_http_write_filter_module.c
@@ -47,7 +47,7 @@ ngx_module_t  ngx_http_write_filter_modu
 ngx_int_t
 ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in)
 {
-    off_t                      size, sent;
+    off_t                      size, sent, to_send;
     ngx_uint_t                 last, flush;
     ngx_chain_t               *cl, *ln, **ll, *chain;
     ngx_connection_t          *c;
@@ -209,25 +209,34 @@ ngx_http_write_filter(ngx_http_request_t
         return NGX_ERROR;
     }
 
+    to_send = r->limit_rate * (ngx_time() - r->start_time + 1) - c->sent;
+
+    if (to_send < 0) {
+        to_send = 0;
+    }
+
     sent = c->sent;
 
-    chain = c->send_chain(c, r->out, r->limit_rate);
+    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
+                   "http write filter to send %O", to_send);
+
+    chain = c->send_chain(c, r->out, to_send);
 
     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
                    "http write filter %p", chain);
 
-    if (r->limit_rate) {
+    if (chain == NGX_CHAIN_ERROR) {
+        c->error = 1;
+        return NGX_ERROR;
+    }
+
+    if (to_send) {
         sent = c->sent - sent;
         c->write->delayed = 1;
         ngx_add_timer(r->connection->write,
                       (ngx_msec_t) (sent * 1000 / r->limit_rate));
     }
 
-    if (chain == NGX_CHAIN_ERROR) {
-        c->error = 1;
-        return NGX_ERROR;
-    }
-
     for (cl = r->out; cl && cl != chain; /* void */) {
         ln = cl;
         cl = cl->next;