openresty / lua-resty-limit-traffic

Lua library for limiting and controlling traffic in OpenResty/ngx_lua
819 stars 150 forks source link

Reset is not working with count.md code #45

Open darpangupta12 opened 5 years ago

darpangupta12 commented 5 years ago

For Nginx rate limiting, we are using code written on count.md. Also, we are using count.lua library. Below code, giving counter value from 199 to 1 from line local delay, err = lim:incoming(key, true) After that, above line giving rejected error hence, it returning 503 error. So, reset is not working in time_window & getting rejected value in err.Please check count.lua library as well.

Refer Code :- local lim, err = limit_count.new("my_limit_count_store", 200, 1) ngx.log(ngx.ERR, "lim instance value: ", err) if not lim then --ngx.log(ngx.ERR, "failed to instantiate a resty.limit.conn object: ", err) return ngx.exit(500) end --if header_authorization then local base64_enc = string.sub(header_authorization, 7) local base64_dec = ngx.decode_base64(base64_enc) ngx.log(ngx.ERR, "base64_dec value: ", base64_dec) local key = base64_dec local delay, err = lim:incoming(key, true) ngx.log(ngx.ERR,"new instance value: ",err)

      if not delay then
                if err == "rejected" then
                    ngx.log(ngx.ERR, "coming to this block: ", err)
                    ngx.header["X-RateLimit-Limit"] = "200"
                    ngx.header["X-RateLimit-Remaining"] = 0
                    return ngx.exit(503)
                end
          --      ngx.log(ngx.ERR, "failed to limit req: ", err)
                return ngx.exit(500)
      end

    local remaining = err
    ngx.log(ngx.ERR, "remaining value: ", remaining)

    ngx.header["X-RateLimit-Limit"] = "200"
    ngx.header["X-RateLimit-Remaining"] = remaining
    ngx.log(ngx.ERR, "X-RateLimit-Remaining: ", ngx.header["X-RateLimit-Remaining"])