ПРОЕКТЫ 


  АРХИВ 


Apache-Talk @lexa.ru 

Inet-Admins @info.east.ru 

Filmscanners @halftone.co.uk 

Security-alerts @yandex-team.ru 

nginx-ru @sysoev.ru 


  СТАТЬИ 


  ПЕРСОНАЛЬНОЕ 


  ПРОГРАММЫ 



ПИШИТЕ
ПИСЬМА












     АРХИВ :: nginx-ru
Nginx-ru mailing list archive (nginx-ru@sysoev.ru)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: nginx-0.7.20



On Tue, Nov 11, 2008 at 12:55:40AM +0300, Maxim Dounin wrote:

> Hello!
> 
> On Mon, Nov 10, 2008 at 11:07:22PM +0300, Igor Sysoev wrote:
> 
> > On Mon, Nov 10, 2008 at 09:43:43PM +0300, Igor Sysoev wrote:
> > 
> > > On Mon, Nov 10, 2008 at 09:16:23PM +0300, Anton Yuzhaninov wrote:
> > > 
> > > > On 10.11.2008 19:31, Igor Sysoev wrote:
> > > > >Изменения в nginx 0.7.20                                          
> > > > >10.11.2008
> > > > >
> > > > >    *) Изменения в модуле ngx_http_gzip_filter_module.
> > > > >
> > > > >    *) Добавление: модуль ngx_http_limit_req_module
> > > > 
> > > > А почему burst измеряется в запросах/секунду, а не просто в запросах.
> > > > 
> > > > Получается это не совсем leaky bucket?
> > > > 
> > > > В классическом leaky bucket можно послать burst запросов за любой, 
> > > > бесконечно малый отрезок времени,
> > > > поэтому время в burst не фигурирует.
> > > 
> > > Да, сейчас там сделано неправильно.
> > > Сегодня-завтра сделаю патч с исправлением burst и динамическим delay.
> > 
> > Патч. Формат директивы изменён:
> > 
> > -limit_req   zone=one  burst=5r/s  delay=5s;
> > +limit_req   zone=one  burst=5;
> > 
> > Задержка теперь вычисляется динамически, чтобы соответствовать заданному
> > rate: например, если rate=1r/s, а запросы приходят раз в полсекунды,
> > то второй запрос задержится на 0.5s, третий - на 1s, четвёртый - на 1.5s
> > и так далее, пока не будет превышен burst.
> 
> Игорь, а можно возможность сделать token bucket вернуть? Всмысле - 
> чтобы можно было разрешать до burst без задержек, а дальше - 
> ошибка.
> 
> До патча, насколько я понимаю, это достигалось установкой 
> delay=0.

Новый патч, два варианта директивы:

   limit_req   zone=one  burst=5;
   limit_req   zone=one  burst=5  nodelay;


-- 
Игорь Сысоев
http://sysoev.ru
Index: src/http/modules/ngx_http_limit_req_module.c
===================================================================
--- src/http/modules/ngx_http_limit_req_module.c        (revision 1618)
+++ src/http/modules/ngx_http_limit_req_module.c        (working copy)
@@ -33,7 +33,7 @@
 typedef struct {
     ngx_shm_zone_t     *shm_zone;
     float               burst;
-    ngx_msec_t          delay;
+    ngx_uint_t          nodelay;     /* unsigned  nodelay:1 */
 } ngx_http_limit_req_conf_t;
 
 
@@ -178,7 +178,7 @@
         ngx_shmtx_unlock(&ctx->shpool->mutex);
 
         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
-                      "limiting requests, %.3f r/s", rate);
+                      "limiting requests, excess: %.3f", rate);
 
         return NGX_HTTP_SERVICE_UNAVAILABLE;
     }
@@ -186,22 +186,22 @@
     if (rc == NGX_AGAIN) {
         ngx_shmtx_unlock(&ctx->shpool->mutex);
 
-        if (lzcf->delay) {
-            ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
-                          "delaying requests, %.3f r/s", rate);
+        if (lzcf->nodelay) {
+            return NGX_DECLINED;
+        }
 
-            if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) {
-                return NGX_HTTP_INTERNAL_SERVER_ERROR;
-            }
+        ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
+                      "delaying request, excess: %.3f", rate);
 
-            r->read_event_handler = ngx_http_test_reading;
-            r->write_event_handler = ngx_http_limit_req_delay;
-            ngx_add_timer(r->connection->write, lzcf->delay);
-
-            return NGX_AGAIN;
+        if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) {
+            return NGX_HTTP_INTERNAL_SERVER_ERROR;
         }
 
-        return NGX_DECLINED;
+        r->read_event_handler = ngx_http_test_reading;
+        r->write_event_handler = ngx_http_limit_req_delay;
+        ngx_add_timer(r->connection->write, (ngx_msec_t) (rate * 1000));
+
+        return NGX_AGAIN;
     }
 
     if (rc == NGX_OK) {
@@ -365,7 +365,7 @@
                     return NGX_BUSY;
                 }
 
-                if (lz->rate > ctx->rate) {
+                if (lz->rate > 0.0) {
                     return NGX_AGAIN;
                 }
 
@@ -511,7 +511,7 @@
      *
      *     conf->shm_zone = NULL;
      *     conf->burst = 0.0;
-     *     conf->delay = 0;
+     *     conf->nodelay = 0;
      */
 
     return conf;
@@ -670,12 +670,9 @@
 {
     ngx_http_limit_req_conf_t  *lzcf = conf;
 
-    u_char                    *p;
-    size_t                     len;
-    ngx_int_t                  burst, scale, delay;
-    ngx_str_t                 *value, s;
-    ngx_uint_t                 i;
-    ngx_http_limit_req_ctx_t  *ctx;
+    ngx_int_t    burst;
+    ngx_str_t   *value, s;
+    ngx_uint_t   i;
 
     if (lzcf->shm_zone) {
         return "is duplicate";
@@ -684,8 +681,6 @@
     value = cf->args->elts;
 
     burst = 0;
-    scale = 1;
-    delay = 0;
 
     for (i = 1; i < cf->args->nelts; i++) {
 
@@ -705,19 +700,7 @@
 
         if (ngx_strncmp(value[i].data, "burst=", 6) == 0) {
 
-            len = value[i].len;
-            p = value[i].data + len - 3;
-
-            if (ngx_strncmp(p, "r/s", 3) == 0) {
-                scale = 1;
-                len -= 3;
-
-            } else if (ngx_strncmp(p, "r/m", 3) == 0) {
-                scale = 60;
-                len -= 3;
-            }
-
-            burst = ngx_atoi(value[i].data + 6, len - 6);
+            burst = ngx_atoi(value[i].data + 6, value[i].len - 6);
             if (burst <= 0) {
                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                                    "invalid burst rate \"%V\"", &value[i]);
@@ -727,18 +710,8 @@
             continue;
         }
 
-        if (ngx_strncmp(value[i].data, "delay=", 6) == 0) {
-
-            s.len = value[i].len - 6;
-            s.data = value[i].data + 6;
-
-            delay = ngx_parse_time(&s, 0);
-            if (delay < 0) {
-                ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
-                                 "invalid clean_time value \"%V\"", &value[i]);
-                return NGX_CONF_ERROR;
-            }
-
+        if (ngx_strncmp(value[i].data, "nodelay", 7) == 0) {
+            lzcf->nodelay = 1;
             continue;
         }
 
@@ -761,16 +734,8 @@
         return NGX_CONF_ERROR;
     }
 
-    if (burst) {
-        lzcf->burst = (float) burst / scale;
+    lzcf->burst = (float) burst;
 
-    } else {
-        ctx = lzcf->shm_zone->data;
-        lzcf->burst = ctx->rate;
-    }
-
-    lzcf->delay = (ngx_msec_t) delay;
-
     return NGX_CONF_OK;
 }
 


 




Copyright © Lexa Software, 1996-2009.