-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
convert cloud hosted custom module into a policy #1
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
38c90cc
use policy chain
mikz 27e5cd1
add roverfile
mikz 44f038f
move source files to correct folder
mikz ddfbb70
run integration tests
mikz 161785d
convert cloud hosted to proper policies
mikz 5925384
circleci new apicast policies
mikz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
0.04
is an old version. Although the latest will be installed anyway unless we pin the version with==
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep. I think that is fine. This library is already installed in the apicast image so this just ensures it has the right version.