-
Notifications
You must be signed in to change notification settings - Fork 170
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
Cache policy #546
Merged
Merged
Cache policy #546
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
eb351b8
Add caching policy
davidor fb3215a
policy/caching: add json schema
davidor c110c0c
t: add tests for caching policy
davidor add2112
spec: add tests for caching policy
davidor 7e2aaef
policy/caching: add 'none' cache type
davidor 661b9df
policy/spec: add tests with disabled cache
davidor 0fd95c5
t/apicast-policy-caching: test disabled cache
davidor 3b00ece
CHANGELOG: add caching policy entry
davidor 4cf52d4
cache_handler: remove empty log part
davidor 2016d41
policy/apicast: override proxy's cache_handler if received via context
davidor 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 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
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,80 @@ | ||
--- Caching policy | ||
-- Configures a cache for the authentication calls against the 3scale backend. | ||
-- The 3scale backend can authorize (status code = 200) and deny (status code = | ||
-- 4xx) calls. When it fails, it returns a 5xx code. | ||
-- This policy support three kinds of caching: | ||
-- - Strict: it only caches authorized calls. Denied and failed calls | ||
-- invalidate the cache entry. | ||
-- - Resilient: caches authorized and denied calls. Failed calls do not | ||
-- invalidate the cache. This allows us to authorize and deny calls | ||
-- according to the result of the last request made even when backend is | ||
-- down. | ||
-- - None: disables caching. | ||
|
||
local policy = require('apicast.policy') | ||
local _M = policy.new('Caching policy') | ||
|
||
local new = _M.new | ||
|
||
local function strict_handler(cache, cached_key, response, ttl) | ||
if response.status == 200 then | ||
ngx.log(ngx.INFO, 'apicast cache write key: ', cached_key, ', ttl: ', ttl) | ||
cache:set(cached_key, 200, ttl or 0) | ||
else | ||
ngx.log(ngx.NOTICE, 'apicast cache delete key: ', cached_key, | ||
' cause status ', response.status) | ||
cache:delete(cached_key) | ||
end | ||
end | ||
|
||
local function resilient_handler(cache, cached_key, response, ttl) | ||
local status = response.status | ||
|
||
if status and status < 500 then | ||
ngx.log(ngx.INFO, 'apicast cache write key: ', cached_key, | ||
' status: ', status, ', ttl: ', ttl) | ||
|
||
cache:set(cached_key, status, ttl or 0) | ||
end | ||
end | ||
|
||
local function disabled_cache_handler() | ||
ngx.log(ngx.DEBUG, 'Caching is disabled. Skipping cache handler.') | ||
end | ||
|
||
local handlers = { | ||
resilient = resilient_handler, | ||
strict = strict_handler, | ||
none = disabled_cache_handler | ||
} | ||
|
||
local function handler(config) | ||
if not config.caching_type then | ||
ngx.log(ngx.ERR, 'Caching type not specified. Disabling cache.') | ||
return handlers.none | ||
end | ||
|
||
local res = handlers[config.caching_type] | ||
|
||
if not res then | ||
ngx.log(ngx.ERR, 'Invalid caching type. Disabling cache.') | ||
res = handlers.none | ||
end | ||
|
||
return res | ||
end | ||
|
||
--- Initialize a Caching policy. | ||
-- @tparam[opt] table config | ||
-- @field caching_type Caching type (strict, resilient) | ||
function _M.new(config) | ||
local self = new() | ||
self.cache_handler = handler(config or {}) | ||
return self | ||
end | ||
|
||
function _M:rewrite(context) | ||
context.cache_handler = self.cache_handler | ||
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,11 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"title": "Caching policy configuration", | ||
"type": "object", | ||
"properties": { | ||
"exit": { | ||
"type": "caching_type", | ||
"enum": ["resilient", "strict", "none"] | ||
} | ||
} | ||
} |
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,111 @@ | ||
local resty_lrucache = require('resty.lrucache') | ||
|
||
describe('policy', function() | ||
describe('.new', function() | ||
local cache = resty_lrucache.new(1) | ||
|
||
it('disables caching when caching type is not specified', function() | ||
local caching_policy = require('apicast.policy.caching').new({}) | ||
local ctx = {} | ||
caching_policy:rewrite(ctx) | ||
|
||
ctx.cache_handler(cache, 'a_key', { status = 200 }, nil) | ||
assert.is_nil(cache:get('a_key')) | ||
end) | ||
|
||
it('disables caching when invalid caching type is specified', function() | ||
local config = { caching_type = 'invalid_caching_type' } | ||
local caching_policy = require('apicast.policy.caching').new(config) | ||
local ctx = {} | ||
caching_policy:rewrite(ctx) | ||
|
||
ctx.cache_handler(cache, 'a_key', { status = 200 }, nil) | ||
assert.is_nil(cache:get('a_key')) | ||
end) | ||
end) | ||
|
||
describe('.access', function() | ||
describe('when configured as strict', function() | ||
local caching_policy | ||
local cache | ||
local ctx -- the caching policy will add the handler here | ||
|
||
before_each(function() | ||
local config = { caching_type = 'strict' } | ||
caching_policy = require('apicast.policy.caching').new(config) | ||
ctx = { } | ||
caching_policy:rewrite(ctx) | ||
cache = resty_lrucache.new(1) | ||
end) | ||
|
||
it('caches authorized requests', function() | ||
ctx.cache_handler(cache, 'a_key', { status = 200 }, nil) | ||
assert.equals(200, cache:get('a_key')) | ||
end) | ||
|
||
it('clears the cache entry for a request when it is denied', function() | ||
cache:set('a_key', 200) | ||
|
||
ctx.cache_handler(cache, 'a_key', { status = 403 }, nil) | ||
assert.is_nil(cache:get('a_key')) | ||
end) | ||
|
||
it('clears the cache entry for a request when it fails', function() | ||
cache:set('a_key', 200) | ||
|
||
ctx.cache_handler(cache, 'a_key', { status = 500 }, nil) | ||
assert.is_nil(cache:get('a_key')) | ||
end) | ||
end) | ||
|
||
describe('when configured as resilient', function() | ||
local caching_policy | ||
local cache | ||
local ctx -- the caching policy will add the handler here | ||
|
||
before_each(function() | ||
local config = { caching_type = 'resilient' } | ||
caching_policy = require('apicast.policy.caching').new(config) | ||
ctx = { } | ||
caching_policy:rewrite(ctx) | ||
cache = resty_lrucache.new(1) | ||
end) | ||
|
||
it('caches authorized requests', function() | ||
ctx.cache_handler(cache, 'a_key', { status = 200 }, nil) | ||
assert.equals(200, cache:get('a_key')) | ||
end) | ||
|
||
it('caches denied requests', function() | ||
ctx.cache_handler(cache, 'a_key', { status = 403 }, nil) | ||
assert.equals(403, cache:get('a_key')) | ||
end) | ||
|
||
it('does not clear the cache entry for a request when it fails', function() | ||
cache:set('a_key', 200) | ||
|
||
ctx.cache_handler(cache, 'a_key', { status = 500 }, nil) | ||
assert.equals(200, cache:get('a_key')) | ||
end) | ||
end) | ||
|
||
describe('when disabled', function() | ||
local caching_policy | ||
local cache | ||
local ctx | ||
|
||
setup(function() | ||
local config = { caching_type = 'none' } | ||
caching_policy = require('apicast.policy.caching').new(config) | ||
ctx = {} | ||
caching_policy:rewrite(ctx) | ||
cache = resty_lrucache.new(1) | ||
end) | ||
|
||
it('does not cache anything', function() | ||
ctx.cache_handler(cache, 'a_key', { status = 200 }, nil) | ||
assert.is_nil(cache:get('a_key')) | ||
end) | ||
end) | ||
end) | ||
end) |
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.
@davidor we also should have "allow" handler that will allow the the request when backend is down.