Skip to content

Instantly share code, notes, and snippets.

@zhuizhuhaomeng
Created February 23, 2021 14:44
Show Gist options
  • Save zhuizhuhaomeng/c230cb1771351b1cfe56fe67f29aff16 to your computer and use it in GitHub Desktop.
Save zhuizhuhaomeng/c230cb1771351b1cfe56fe67f29aff16 to your computer and use it in GitHub Desktop.
location /hello {
content_by_lua_block {
local lim = require "limit_conn"
-- local key = ngx.var.binary_remote_addr
local exceptions = ngx.worker.exceptions()
local key = string.char(exceptions % 256) .. ngx.var.binary_remote_addr
local delay, err = lim: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
if lim:is_committed() then
local ctx = ngx.ctx
ctx.limit_conn = lim
ctx.limit_conn_key = key
ctx.limit_conn_delay = delay
end
if delay >= 0.001 then
ngx.sleep(delay)
end
ngx.say("hello")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment