小结:

1、在连接环节计数,有清零环节

有3个参量

max
burst
unit_delay

https://github.com/openresty/lua-resty-limit-traffic/blob/master/README.md

-- limit the requests under 200 req/sec with a burst of 100 req/sec,
-- that is, we delay requests under 300 req/sec and above 200
-- req/sec, and reject any requests exceeding 300 req/sec.

-- the following call must be per-request.
-- here we use the remote (IP) address as the limiting key
local key = ngx.var.binary_remote_addr
local delay, err = lim:incoming(key, true)
if not delay then
if err == "rejected" then
return ngx.exit(503)
end
ngx.log(ngx.ERR, "failed to limit req: ", err)
return ngx.exit(500)
end

if delay >= 0.001 then
-- the 2nd return value holds the number of excess requests
-- per second for the specified key. for example, number 31
-- means the current request rate is at 231 req/sec for the
-- specified key.
local excess = err

-- the request exceeding the 200 req/sec but below 300 req/sec,
-- so we intentionally delay it here a bit to conform to the
-- 200 req/sec rate.
ngx.sleep(delay)
end

delay
the delay in seconds (the caller should sleep before processing the current request)

https://github.com/openresty/lua-resty-limit-traffic/blob/master/lib/resty/limit/count.md

syntax: delay, err = obj:incoming(key, commit)

Fires a new request incoming event and calculates the delay needed (if any) for the current request upon the specified key or whether the user should reject it immediately.

This method accepts the following arguments:

key is the user specified key to limit the rate.

For example, one can use the host name (or server zone) as the key so that we limit rate per host name. Otherwise, we can also use the authorization header value as the key so that we can set a rate for individual user.

Please note that this module does not prefix nor suffix the user key so it is the user's responsibility to ensure the key is unique in the lua_shared_dict shm zone).

commit is a boolean value. If set to true, the object will actually record the event in the shm zone backing the current object; otherwise it would just be a "dry run" (which is the default).

The return values depend on the following cases:

If the request does not exceed the count value specified in the new method, then this method returns 0 as the delay and the remaining count of allowed requests at the current time (as the 2nd return value).

If the request exceeds the count limit specified in the new method then this method returns nil and the error string "rejected".

If an error occurred (like failures when accessing the lua_shared_dict shm zone backing the current object), then this method returns nil and a string describing the error.

在nginx粒度度限制,各个worker
The limiting works on the granularity of an individual NGINX server instance (including all its worker processes). Thanks to the shm mechanism; we can share state cheaply across all the workers in a single NGINX server instance.
限流是nginx粒度的,不是woker粒度的,需分析量化对qps的降低的影响程度

清零
https://github.com/openresty/lua-resty-limit-traffic/blob/master/lib/resty/limit/req.lua
-- we do not handle changing rate values specifically. the excess value
-- can get automatically adjusted by the following formula with new rate
-- values rather quickly anyway.
excess = max(tonumber(rec.excess) - rate * abs(elapsed) / 1000 + 1000,0)

if conn > max + self.burst then
conn, err = dict:incr(key, -1)
if not conn then
return nil, err
end
return nil, "rejected"
end
self.committed = true
https://github.com/openresty/lua-resty-limit-traffic/blob/master/lib/resty/limit/conn.lua

function _M.incoming(self, key, commit)
local dict = self.dict
local max = self.max self.committed = false local conn, err
if commit then
conn, err = dict:incr(key, 1, 0)
if not conn then
return nil, err
end if conn > max + self.burst then
conn, err = dict:incr(key, -1)
if not conn then
return nil, err
end
return nil, "rejected"
end
self.committed = true else
conn = (dict:get(key) or 0) + 1
if conn > max + self.burst then
return nil, "rejected"
end
end if conn > max then
-- make the exessive connections wait
return self.unit_delay * floor((conn - 1) / max), conn
end -- we return a 0 delay by default
return 0, conn
end function _M.is_committed(self)
return self.committed
end function _M.leaving(self, key, req_latency)
assert(key)
local dict = self.dict local conn, err = dict:incr(key, -1)
if not conn then
return nil, err
end if req_latency then
local unit_delay = self.unit_delay
self.unit_delay = (req_latency + unit_delay) / 2
end return conn
end

  

【源码】openresty 限流的更多相关文章

  1. Sentinel 源码分析-限流原理

    1. git clone senetinel 源码到本地,切换到release1.8分支 2. 找到FlowQpsDemo.java, 根据sentinel自带的案例来学习sentinel的原理 3. ...

  2. Spark Streaming源码解读之流数据不断接收全生命周期彻底研究和思考

    本期内容 : 数据接收架构设计模式 数据接收源码彻底研究 一.Spark Streaming数据接收设计模式   Spark Streaming接收数据也相似MVC架构: 1. Mode相当于Rece ...

  3. .27-浅析webpack源码之事件流make(2)

    上一节跑到了NormalModuleFactory模块,调用了原型方法create后,依次触发了before-rsolve.factory.resolver事件流,这节从resolver事件流开始讲. ...

  4. .26-浅析webpack源码之事件流make(1)

    compilation事件流中,依然只是针对细节步骤做事件流注入,代码流程如图: // apply => this-compilation // apply => compilation ...

  5. .24-浅析webpack源码之事件流compilation(2)

    下一个compilation来源于以下代码: compiler.apply(new EntryOptionPlugin()); compiler.applyPluginsBailResult(&quo ...

  6. .23-浅析webpack源码之事件流compilation(1)

    正式开始跑编译,依次解析,首先是: compiler.apply( new JsonpTemplatePlugin(options.output), // start new FunctionModu ...

  7. .22-浅析webpack源码之事件流compilation总览

    呃,终于到了这地方-- newCompilation(params) { // ... this.applyPlugins("this-compilation", compilat ...

  8. .21-浅析webpack源码之事件流this-compilation

    上一节生成Compilation实例后,添加了一些属性,随后触发this-compilation事件流,如下: Compiler.prototype.newCompilation = (params) ...

  9. .34-浅析webpack源码之事件流make(3)

    新年好呀~过个年光打游戏,function都写不顺溜了. 上一节的代码到这里了: // NormalModuleFactory的resolver事件流 this.plugin("resolv ...

随机推荐

  1. jar找不到问题解决

    1.File->Settings->搜maven->看Local repository的路径配置是否正确,再看User settings file路径配置是否正确,再看xml内容配置 ...

  2. JAVA笔记整理(十),JAVA中的File

    File类提供对针对目录和文件的读写改等一系列操作方法 创建: public class FileDemo01 { public static void main(String[] args) { t ...

  3. Django 中使用权限认证

    权限认证 权限概念 """ 在实际开发中,项目中都有后台运营站点,运营站点里面会存在多个管理员, 那么不同的管理员会具备不同的任务和能力,那么要实现这样的管理员功能,那么 ...

  4. TIME_WAIT状态全是3306解决办法

    刚吃完晚饭,手机短信一直响个不停,打开一看全是告警信息,立即打开电脑查看,发现很多网页很不稳定  一会能打开,一会打不开 登录服务器查看负载情况,cpu.内存 .磁盘io 负载都不高,查看日志发现ng ...

  5. EXCEL导入数据到SQL SERVER 2008

    项目中需要导入excel到SQL SERVER数据库 总是报截断, 本质问题是,SQL SERVER导入程序是根据EXCEL的第一行记录 (非标题行)来决定数据长度的 碰到这个问题,可以伪造第一行,然 ...

  6. java中的switch

    switch 语句由一个控制表达式和多个case标签组成. switch 控制表达式支持的类型有byte.short.char.int.enum(Java 5).String(Java 7). swi ...

  7. springboot整合shiro引用配置文件配置redis信息报空指针异常

    1.问题现象: 上面这些属性是从application.properties配置文件中获取的,按常理来说应该能顺利获取到,但均未赋上值. 2.解决办法:(不得不说百度,千篇一律,最后用谷歌找到的) 最 ...

  8. post提交主订单数据(gateway)实现httpapi

    models.proto syntax = "proto3"; package services; import "google/protobuf/timestamp.p ...

  9. 转,异常好的sql 基础知识整理

    转载自:http://blog.csdn.net/u011001084/article/details/51318434 最近从图书馆借了本介绍SQL的书,打算复习一下基本语法,记录一下笔记,整理一下 ...

  10. php利用webuploader实现超大文件分片上传、断点续传

    PHP用超级全局变量数组$_FILES来记录文件上传相关信息的. 1.file_uploads=on/off 是否允许通过http方式上传文件 2.max_execution_time=30 允许脚本 ...