-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from 3scale/apicast-policy-chain
convert cloud hosted custom module into a policy
- Loading branch information
Showing
23 changed files
with
447 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
lua_modules | ||
t/servroot |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
APICAST_LOADED_ENVIRONMENTS=cloud_hosted |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
.idea | ||
*.swp | ||
lua_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
luarocks { | ||
group 'production' { | ||
module { 'lua-resty-iputils' }, | ||
}, | ||
|
||
group { 'development', 'test' } { | ||
module { 'apicast' }, | ||
module { 'lua-resty-repl' }, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
apicast scm-1|348144131998f97e2190fa3b3f1c8ba70d2339d3|development,test | ||
argparse 0.5.0-1||development,test | ||
inspect 3.1.1-0||development,test | ||
liquid scm-1|811a73e38fdd9fdea116be4baf310ca326b96c77|development,test | ||
lua-resty-env 0.4.0-1||development,test | ||
lua-resty-execvp 0.1.0-1||development,test | ||
lua-resty-http 0.12-0||development,test | ||
lua-resty-iputils 0.3.0-1||production | ||
lua-resty-jwt 0.1.11-0||development,test | ||
lua-resty-repl 0.0.6-0|3878f41b7e8f97b1c96919db19dbee9496569dda|development,test | ||
lua-resty-url 0.2.0-1||development,test | ||
luafilesystem 1.7.0-2||development,test | ||
penlight 1.5.4-1||development,test | ||
router 2.1-0||development,test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
local PolicyChain = require('apicast.policy_chain') | ||
local policy_chain = context.policy_chain | ||
|
||
if not arg then -- {arg} is defined only when executing the CLI | ||
policy_chain:insert(PolicyChain.load_policy('cloud_hosted.rate_limit', '0.1', { | ||
limit = os.getenv('RATE_LIMIT') or 5, | ||
burst = os.getenv('RATE_LIMIT_BURST') or 50 }), 1) | ||
policy_chain:insert(PolicyChain.load_policy('cloud_hosted.balancer_blacklist', '0.1'), 1) | ||
end | ||
|
||
return { | ||
policy_chain = policy_chain | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
requires 'Test::APIcast', '0.04'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
return require('balancer_blacklist') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
return require('rate_limit') |
69 changes: 69 additions & 0 deletions
69
apicast/policies/cloud_hosted.rate_limit/0.1/rate_limit.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
local tonumber = tonumber | ||
|
||
local limit_req = require "resty.limit.req" | ||
|
||
local _M = require('apicast.policy').new('Rate Limit', '0.1') | ||
|
||
local new = _M.new | ||
|
||
local function new_limiter(limit, burst) | ||
local limiter, err = limit_req.new("rate_limit_req_store", tonumber(limit), tonumber(burst) or 0) | ||
|
||
if limiter then | ||
ngx.log(ngx.NOTICE, 'rate limit: ', limit, '/s', ' burst: ', burst or limit, '/s') | ||
elseif not arg then -- if not being loaded on the CLI | ||
ngx.log(ngx.ERR, 'error loading rate limiter: ', err) | ||
end | ||
|
||
return limiter | ||
end | ||
|
||
local empty = {} | ||
|
||
function _M.new(configuration) | ||
local policy = new(configuration) | ||
local config = configuration or empty | ||
|
||
local limit = config.limit | ||
local burst = config.burst | ||
|
||
policy.status = config.status | ||
|
||
if limit then | ||
policy.limiter = new_limiter(limit, burst) | ||
else | ||
ngx.log(ngx.NOTICE, 'rate limit not set') | ||
end | ||
|
||
return policy | ||
end | ||
|
||
function _M:access(context) | ||
local limiter = self.limiter | ||
|
||
if not limiter then return nil, 'missing limiter' end | ||
|
||
local key = context.host or ngx.var.host | ||
local status = self.status or 503 | ||
|
||
local delay, err = limiter:incoming(key, true) | ||
|
||
if not delay then | ||
ngx.log(ngx.WARN, err, ' request over limit, key: ', key) | ||
if err == "rejected" then | ||
return ngx.exit(status) | ||
end | ||
ngx.log(ngx.ERR, "failed to limit req: ", err) | ||
return ngx.exit(500) | ||
end | ||
|
||
if delay >= 0.001 then | ||
local excess = err | ||
|
||
ngx.log(ngx.WARN, 'delaying request: ', key, ' for ', delay, 's, excess: ', excess) | ||
ngx.sleep(delay) | ||
end | ||
end | ||
|
||
|
||
return _M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
return require('upstream') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
local resty_resolver = require('resty.resolver') | ||
local resty_url = require('resty.url') | ||
local format = string.format | ||
|
||
local _M = require('apicast.policy').new('Upstream', '0.1') | ||
|
||
local new = _M.new | ||
|
||
local empty = {} | ||
function _M.new(configuration) | ||
local policy = new(configuration) | ||
local config = configuration or empty | ||
|
||
local url = resty_url.parse(config.url) or empty | ||
local host = config.host or url.host | ||
|
||
policy.host = host | ||
policy.url = url | ||
|
||
return policy | ||
end | ||
|
||
function _M:content() | ||
local url = self.url | ||
local host = self.host | ||
|
||
ngx.ctx.upstream = resty_resolver:instance():get_servers(url.host, { port = url.port }) | ||
ngx.var.proxy_pass = format('%s://upstream%s', url.scheme, url.path or '') | ||
ngx.req.set_header('Host', host or ngx.var.host) | ||
|
||
if not ngx.headers_sent then | ||
ngx.exec("@upstream") | ||
end | ||
end | ||
|
||
|
||
return _M |
Oops, something went wrong.