lua-resty-limit-traffic icon indicating copy to clipboard operation
lua-resty-limit-traffic copied to clipboard

setting resty.limit.conn issue

Open DHB-liuhong opened this issue 5 years ago • 1 comments

Hello, I found some problems when i set resty.limit.conn。

  • Purpose: i want to limit the number of concurrent. It can return 503 when requests exceed concurrent

  • nginx.config:

http {
    lua_shared_dict limit_conn_store 100m;
    server {
        listen       9090;
        server_name  localhost;

        location / {
            access_by_lua_file /usr/local/openresty/nginx/conf/access.lua;
            #log_by_lua_file /usr/local/openresty/nginx/conf/log.lua;
            proxy_pass http://10.128.3.68;
  • access.lua
local limit_conn = require "limit_conn"

if ngx.req.is_internal() then
    return
end

limit_conn.incoming()
  • log.lua
local limit_conn = require "limit_conn"

limit_conn.leaving()

-limit_conn.lua

ngx.var.limit_rate = "100K"

local limit_conn = require "resty.limit.conn"
local limit, limit_err = limit_conn.new("limit_conn_store", 10, 2, 0.5)
if not limit then
    print(limit_err)
    ngx.log(ngx.ERR,"failed to instantiate a resty.limit.conn object: ", limit_err)
    return ngx.exit(500)
end

local _conn = {}

function _conn.incoming()
    local key = ngx.var.binary_remote_addr
    local delay, err = limit: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

    ngx.log(ngx.INFO, "delay= ", delay)
    if limit:is_committed() then
        local ctx = ngx.ctx
        ctx.limit_conn_key = key
        ctx.limit_conn_delay = delay
    end

    local conn = err

    if delay >= 0.001 then
        ngx.log(ngx.WARN, "delaying conn, excess ", delay, "s per binary_remote_addr by limit_conn_store")
        ngx.sleep(delay)
    end
end

function _conn.leaving()
    local ctx = ngx.ctx
    local key = ctx.limit_conn_key
    ngx.log(ngx.INFO, "key= ", key)
    if key then
        local latency = tonumber(ngx.var.request_time) - ctx.limit_conn_delay
        local conn, err = limit:leaving(key, latency)
        if not conn then
            ngx.log(ngx.ERR,
                    "failed to record the connection leaving ",
                    "request: ", err)
        end
    end
end


return _conn
  • issue: No use log.lua First execute this command for i in {0..50};do (curl -Is http://10.139.8.112:9090 | head -n1 &) 2>/dev/null; done It can return 12 OK, the rest return 503.
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 200 OK
HTTP/1.1 503 Service Temporarily Unavailable
HTTP/1.1 503 Service Temporarily Unavailable
HTTP/1.1 503 Service Temporarily Unavailable
HTTP/1.1 503 Service Temporarily Unavailable
HTTP/1.1 503 Service Temporarily Unavailable
HTTP/1.1 503 Service Temporarily Unavailable
HTTP/1.1 503 Service Temporarily Unavailable

Second execute this command for i in {0..50};do (curl -Is http://10.139.8.112:9090 | head -n1 &) 2>/dev/null; done It all return 503

HTTP/1.1 503 Service Temporarily Unavailable
HTTP/1.1 503 Service Temporarily Unavailable
HTTP/1.1 503 Service Temporarily Unavailable
HTTP/1.1 503 Service Temporarily Unavailable
HTTP/1.1 503 Service Temporarily Unavailable
HTTP/1.1 503 Service Temporarily Unavailable
HTTP/1.1 503 Service Temporarily Unavailable
  • issue two user log.lua No matter how many times i execute this command, it both return 200 OK command: for i in {0..50};do (curl -Is http://10.139.8.112:9090 | head -n1 &) 2>/dev/null; done

DHB-liuhong avatar Aug 08 '18 07:08 DHB-liuhong

@agentzh Can you help me have a look? if there is something wrong with the configuration. thanks.

DHB-liuhong avatar Aug 08 '18 08:08 DHB-liuhong