Несколько непонятностей по nginx

Igor Sysoev is at rambler-co.ru
Tue Apr 24 02:24:49 MSD 2007


On Mon, Apr 23, 2007 at 08:16:12PM +0300, Александр Ворона wrote:

> Igor Sysoev пишет:
> >On Mon, Apr 23, 2007 at 07:15:17PM +0300, Александр Ворона wrote:
> >
> >>Igor Sysoev пишет:
> >>
> >>>А если поставить "limit_rate  1M" ? В этом случае nginx сам больше 1M
> >>>за раз передавать не будет..
> >>но он же и будет лимитировать скорость в 1М :)
> >>А проблема имеет место только в случае когда отдача по сети быстрее чем 
> >>диск - nonblock не даёт ничего тогда
> >
> >Ну, можно поставить 10M.
> 
> Чистое ядро 64bit, чистый nginx, limit_rate 10М, отдача 4G файла с диска 
> по 1G линку.
> Наблюдаются замирания закачки после тех моментов, когда sendfile выслал 
> ровно столько сколько его запросили.
> также проблему длинных sendfile не решило :)
> sendfile(11, 14, [1647065810], 187942190) = 187942190
> epoll_wait(13, {}, 512, 17924)          = 0
> sendfile(11, 14, [1835008000], 220200960) = 220200960
> epoll_wait(13, {}, 512, 21001)          = 0
> sendfile(11, 14, [2055208960], 251658240) = 251658240
> 
> Отдача естественно рывками идёт. Средняя скорость в итоге 9.41М

Можно попробовать прилагаемый патч. Он ограничивает размер передаваемых
данных за один раз 128K без ограничения скорости.

Настраивается в src/os/unix/ngx_linux_init.c

off_t ngx_linux_sendfile_limit = 128 * 1024;


-- 
Игорь Сысоев
http://sysoev.ru
-------------- next part --------------
Index: src/http/ngx_http_write_filter_module.c
===================================================================
--- src/http/ngx_http_write_filter_module.c	(revision 502)
+++ src/http/ngx_http_write_filter_module.c	(working copy)
@@ -47,7 +47,7 @@
 ngx_int_t
 ngx_http_write_filter(ngx_http_request_t *r, ngx_chain_t *in)
 {
-    off_t                      size, sent, to_send;
+    off_t                      size, sent, limit;
     ngx_uint_t                 last, flush;
     ngx_chain_t               *cl, *ln, **ll, *chain;
     ngx_connection_t          *c;
@@ -210,28 +210,35 @@
     }
 
     if (r->limit_rate) {
-        to_send = r->limit_rate * (ngx_time() - r->start_sec + 1) - c->sent;
+        limit = r->limit_rate * (ngx_time() - r->start_sec + 1) - c->sent;
 
-        if (to_send <= 0) {
+        if (limit <= 0) {
             c->write->delayed = 1;
             ngx_add_timer(c->write,
-                          (ngx_msec_t) (- to_send * 1000 / r->limit_rate + 1));
+                          (ngx_msec_t) (- limit * 1000 / r->limit_rate + 1));
 
             c->buffered |= NGX_HTTP_WRITE_BUFFERED;
 
             return NGX_AGAIN;
         }
 
+#if (NGX_LINUX)
+
+    } else if (ngx_linux_sendfile_limit) {
+        limit = ngx_linux_sendfile_limit;
+
+#endif
+
     } else {
-        to_send = 0;
+        limit = 0;
     }
 
     sent = c->sent;
 
     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
-                   "http write filter to send %O", to_send);
+                   "http write filter to send %O", limit);
 
-    chain = c->send_chain(c, r->out, to_send);
+    chain = c->send_chain(c, r->out, limit);
 
     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
                    "http write filter %p", chain);
@@ -241,12 +248,21 @@
         return NGX_ERROR;
     }
 
-    if (to_send) {
+    if (r->limit_rate) {
         sent = c->sent - sent;
         c->write->delayed = 1;
         ngx_add_timer(c->write, (ngx_msec_t) (sent * 1000 / r->limit_rate + 1));
     }
 
+#if (NGX_LINUX)
+
+      else if (ngx_linux_sendfile_limit) {
+        c->write->delayed = 1;
+        ngx_add_timer(c->write, 0);
+    }
+
+#endif
+
     for (cl = r->out; cl && cl != chain; /* void */) {
         ln = cl;
         cl = cl->next;
Index: src/os/unix/ngx_linux_init.c
===================================================================
--- src/os/unix/ngx_linux_init.c	(revision 502)
+++ src/os/unix/ngx_linux_init.c	(working copy)
@@ -17,6 +17,7 @@
 
 int   ngx_linux_rtsig_max;
 
+off_t ngx_linux_sendfile_limit = 128 * 1024;
 
 static ngx_os_io_t ngx_linux_io = {
     ngx_unix_recv,
Index: src/os/unix/ngx_linux.h
===================================================================
--- src/os/unix/ngx_linux.h	(revision 502)
+++ src/os/unix/ngx_linux.h	(working copy)
@@ -13,5 +13,7 @@
 
 extern int ngx_linux_rtsig_max;
 
+extern off_t ngx_linux_sendfile_limit;
 
+
 #endif /* _NGX_LINUX_H_INCLUDED_ */


More information about the nginx-ru mailing list