openresty / lua-resty-limit-traffic

Lua library for limiting and controlling traffic in OpenResty/ngx_lua
821 stars 153 forks source link

About limit reset-expire? #19

Open lebanggit opened 7 years ago

lebanggit commented 7 years ago

With this config

local lim, err = limit_conn.new("my_limit_conn_store", 10, 5, 0.5)

After somes request. I alway get 503. I want limit per IP request not over 15 concurrent connections

agentzh commented 7 years ago

@lebanggit I don't think I understand your question.

lebanggit commented 7 years ago

I having to config a streaming server on openresty. It's work fine. But with download manager is caused overload. I want to limit concurrent connection on per IP.

location /
    {
        access_by_lua_block
        {
            ngx.log(ngx.INFO, "log for: ", ngx.var.remote_addr)
            local limit_conn = require "resty.limit.conn"
            local lim, err = limit_conn.new("my_limit_conn_store", 10, 5, 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
            local delay, err = lim:incoming(key, true)
            if not delay then
                ngx.log(ngx.ERR, "failed to limit req: ", err)
                if err == "rejected" then
                    return ngx.exit(503)
                end
                return ngx.exit(500)
            end

            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

            if delay >= 0.001 then
                -- the request exceeding the 200 connections ratio but below
                -- 300 connections, so
                -- we intentionally delay it here a bit to conform to the
                -- 200 connection limit.
                ngx.log(ngx.WARN, "delaying")
                ngx.sleep(delay)
            end
        }
    }

Max 10 concurrent connections and Burst 5.

When i testing with some request (on browser and download via IDM). It's seems limited and get 503. After 24 hours still get 503.

I don't understand. How it works.

fankeke commented 7 years ago

@lebanggit
Have you ever called lim:leaving in log_by_lua phase ?

you could refer the example in README.md , it shows how to do this.

lebanggit commented 7 years ago

@fankeke: Yes, I have....

For testing i set: local lim, err = limit_conn.new("my_limit_conn_store", 2, 1, 0.5)

And i have open one tab on browser and concurrent downloading with IDM.

OK, total 03 connection limited.

But on browser loaded about 30% file, IDM loaded 45%..... it's error.

I have close the tab on browser and close IDM and re-access on browser. It still get 503.

I guess... lim:leaving working not exactly or leave out.

zhaodaiyang0574 commented 5 years ago

限制后,多少时间可以解禁(After the restriction, how long can the ban be lifted?)@agentzh