nginx proxy limit module development --2

本文详细解析了一种用于限制文件上传速率的算法实现原理。通过调整变量`u->limit_rate`和`u->limit_rate_after`,可以有效地控制客户端上传的数据速率。当超出设定的速率后,使用`ngx_add_timer`函数进行延时处理,确保数据传输符合预设的速率限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Core algorithm of limit rate:

code:

if (u->limit_rate) {
excess = u->received - u->limit_rate_after
- u->limit_rate * (ngx_time() - r->start_sec + 1);

if (excess > 0) {
if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) {
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
delay = excess * 1000 /u->limit_rate;
if (delay > 0) {
c->read->delayed = 1;
ngx_add_timer(r->connection->read, delay);
return NGX_AGAIN;
}
}
}

Variable and description:

a. u->limit_rate

Description:

This variable can be set by user. The configure file include the phrase to set.

Limit the rate to upload file.

Example: proxy_limit_rate 10k;

b. u->limit_rate_after

Description:

This variable can be set by user. The configure file include the phrase to set.

Limit rate after 10k byte.

Example: proxy_limit_rate_after 10k;

Function and description:

ngx_add_timer(r->connection->read, delay);

http://blog.youkuaiyun.com/marcky/article/details/7623335

ngx_add_timer函数就是用来完成将一个新的定时事件加入定时器红黑树中,定时事件被执行后,就会从树中移除.

Summary:

After above, we only need to rewrite the 

ngx_http_read_client_request_body ==> ngx_http_proxy_read_client_request_body

Notice:

a. ngx_http_proxy_read_client_request_body_handler

This function should be modify. Because ngx_handle_read_event(rev, clcf->send_lowat) 

need to add for process read delay.

This code is important to deal read timeout. Or your code will be exit.

r->connection->read->timedout,   r->connection->read->delayed = 0;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值