openresty / lua-resty-limit-traffic

Lua library for limiting and controlling traffic in OpenResty/ngx_lua
817 stars 149 forks source link

setting resty.limit.conn issue #36

Open DHB-liuhong opened 6 years ago

DHB-liuhong commented 6 years ago

Hello, I found some problems when i set resty.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 commented 6 years ago

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