@@ -261,9 +261,10 @@ ngx_http_limit_traffic_rate_filter_handler(ngx_http_request_t *r)
261
261
static ngx_int_t
262
262
ngx_http_limit_traffic_rate_body_filter (ngx_http_request_t * r , ngx_chain_t * in )
263
263
{
264
- size_t len ;
265
- uint32_t hash ;
266
- ngx_int_t rc ;
264
+ size_t len ;
265
+ time_t sec ;
266
+ uint32_t hash ;
267
+ ngx_int_t rc , num ;
267
268
ngx_slab_pool_t * shpool ;
268
269
ngx_rbtree_node_t * node , * sentinel ;
269
270
ngx_http_variable_value_t * vv ;
@@ -345,12 +346,21 @@ static ngx_int_t
345
346
}
346
347
p = ngx_queue_next (p );
347
348
}
349
+
350
+ sec = ngx_time () - lir -> start_sec + 1 ;
351
+ sec = sec > 0 ? sec : 1 ;
352
+ num = lircf -> limit_traffic_rate - sent_sum / sec ;
353
+ num = num / lir -> conn + r -> connection -> sent / sec ;
354
+
355
+ num = num > 0 ? num : 1024 ;
356
+ num = ((size_t )num > lircf -> limit_traffic_rate ) ? (ngx_int_t )lircf -> limit_traffic_rate : num ;
357
+
358
+ r -> limit_rate = num ;
359
+
360
+ ngx_log_debug5 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 ,
361
+ "limit traffic d:%z n:%O c:%d r:%z:::%z" , lircf -> limit_traffic_rate ,
362
+ sent_sum , lir -> conn , lir -> start_sec ,r -> limit_rate );
348
363
349
- r -> limit_rate = (lircf -> limit_traffic_rate - sent_sum /
350
- ( ngx_time () - lir -> start_sec + 1 ) ) /lir -> conn +
351
- r -> connection -> sent /(ngx_time () - lir -> start_sec + 1 );
352
- r -> limit_rate = r -> limit_rate > lircf -> limit_traffic_rate ?
353
- lircf -> limit_traffic_rate : r -> limit_rate ;
354
364
goto done ;
355
365
}
356
366
0 commit comments