接入层限流之OpenResty提供的Lua限流模块lua-resty-limit-traffic

【转载请注明出处】:https://blog.csdn.net/huahao1989/article/details/106009761

限制接口总并发数

场景:
按照 ip 限制其并发连接数

lua_shared_dict my_limit_conn_store 100m;
...
location /hello {
    access_by_lua_block {
        local limit_conn = require "resty.limit.conn"
        -- 限制一个 ip 客户端最大 1 个并发请求
        -- burst 设置为 0,如果超过最大的并发请求数,则直接返回503,
        -- 如果此处要允许突增的并发数,可以修改 burst 的值(漏桶的桶容量)
        -- 最后一个参数其实是你要预估这些并发(或者说单个请求)要处理多久,以便于对桶里面的请求应用漏桶算法
        
        local lim, err = limit_conn.new("my_limit_conn_store", 1, 0, 0.5)              
        if not lim then
            ngx.log(ngx.ERR, "failed to instantiate a resty.limit.conn object: ", err)
            return ngx.exit(500)
        end

        local key = ngx.var.binary_remote_addr
        -- commit 为true 代表要更新shared dict中key的值,
        -- false 代表只是查看当前请求要处理的延时情况和前面还未被处理的请求数
        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

        -- 如果请求连接计数等信息被加到shared dict中,则在ctx中记录下,
        -- 因为后面要告知连接断开,以处理其他连接
        if lim:is_committed() then
            local ctx = ngx.ctx
            ctx.limit_conn = lim
            ctx.limit_conn_key = key
            ctx.limit_conn_delay = delay
        end

        local conn = err
        -- 其实这里的 delay 肯定是上面说的并发处理时间的整数倍,
        -- 举个例子,每秒处理100并发,桶容量200个,当时同时来500个并发,则200个拒掉
        -- 100个在被处理,然后200个进入桶中暂存,被暂存的这200个连接中,0-100个连接其实应该延后0.5秒处理,
        -- 101-200个则应该延后0.5*2=1秒处理(
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值