Skip to content

limit_req is not as expected in openresty/1.15.8.2 #53

Description

@zhangqiang-01

hello @agentzh , I have a question to disturb you。

I found some problems use limit_req, I set a limit of 500, but the normal number of requests is 3000.

or version: openresty/1.15.8.2

nginx.conf:
lua_shared_dict limit_req_store 1m;

limit_config:
{
"conn_limit": -1,
"req_total_limit": 500,
"req_host_limit": -1,
"org_host_limit": -1,
"limit_rate_after": 0,
"limit_rate": -1,
"exception": {}
}

lua script:
`
_M.req_limit_process = function(host)
local conf = _M.conf

if not conf.req_total_limit or conf.req_total_limit < 0 then
    return
end

if conf.req_total_limit and conf.req_total_limit >= 0 then
ngx_logger(NGX_ERR, conf.req_total_limit)
    local limiter, err = limit_req.new(limit_req_store, conf.req_total_limit, 0)
    if not limiter then
        ngx_logger(NGX_ERR, "create req total limiter failed, err: ", err)
        return bdapp.exit(500, errno.UNKNOW_ERROR)
    end

    local key = "total_request"
    local delay, err = limiter:incoming(key, true)
    if not delay then
        ngx_logger(NGX_WARN, "req limit")
        return bdapp.exit(429, errno.REQ_TOTAL_LIMT_REJECT)
    end

    -- no delay in req_total_limit
end

-- req_host_limit被设置为-1, 可以忽略下列逻辑
local host_limit = conf.req_host_limit
local elem = conf.exception[host]
if elem then
    host_limit = elem.req_limit
end

if host_limit >= 0 then
    local burst = 50
    if host_limit < 100 then
        burst = 0
    end

    local limiter, err = limit_req.new(limit_req_store, host_limit, burst)
    if not limiter then
        return bdapp.exit(500, errno.UNKNOW_ERROR)
    end

    local delay, err = limiter:incoming(host, true)
    if not delay then
        return bdapp.exit(429, errno.REQ_HOST_LIMIT_REJECT)
    end

    if delay >= 0.001 then
        ngx.sleep(delay)
    end
end

end
`

testing and result:
`
wrk -c 100 -d 100 -t 4 http://127.0.0.1/1.txt -H "Host: www.ortest.com"

Running 2m test @ http://127.0.0.1/1.txt
4 threads and 100 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 14.82ms 18.13ms 344.20ms 91.07%
Req/Sec 2.15k 546.94 5.88k 73.02%
853821 requests in 1.67m, 259.09MB read
Non-2xx or 3xx responses: 502925
Requests/sec: 8530.88
Transfer/sec: 2.59MB

200 status qps is (853821 - 502925) / 100 ~= 3500
`

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions