Skip to content
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

Add option to never reload configuration when configuration loader is lazy #657

Merged
merged 4 commits into from
Apr 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- OAuth2.0 Token Introspection policy [PR #619](https://github.com/3scale/apicast/pull/619)
- New `metrics` phase that runs when prometheus is collecting metrics [PR #629](https://github.com/3scale/apicast/pull/629)
- Validation of policy configs both in integration and unit tests [PR #646](https://github.com/3scale/apicast/pull/646)
- Option to avoid refreshing the config when using the lazy loader with `APICAST_CONFIGURATION_CACHE` < 0 [PR #657](https://github.com/3scale/apicast/pull/657)

## Fixed

Expand Down
3 changes: 2 additions & 1 deletion doc/parameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ Resilient will do so only on getting authorization denied from backend.

### `APICAST_CONFIGURATION_CACHE`

**Values:** _a number > 60_
**Values:** _a number_
**Default:** 0

Specifies the interval (in seconds) that the configuration will be stored for. The value should be set to 0 (not compatible with boot value of `APICAST_CONFIGURATION_LOADER`) or more than 60. For example, if `APICAST_CONFIGURATION_CACHE` is set to 120, the gateway will reload the configuration from the API manager every 2 minutes (120 seconds).
A value < 0 disables reloading.

### `APICAST_CONFIGURATION_LOADER`

Expand Down
9 changes: 8 additions & 1 deletion gateway/src/apicast/configuration_store.lua
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,15 @@ function _M.store(self, config, ttl)

local cache = self.cache

local cache_ttl = config.ttl or ttl or _M.ttl

-- In lrucache a value < 0 expires, but we use configs and ENVs for
-- setting the ttl where < 0 means 'never expire'. When ttl < 0,
-- we need to set it to nil (never expire in lrucache).
if cache_ttl and cache_ttl < 0 then cache_ttl = nil end

for host, services_for_host in pairs(by_host) do
cache:set(host, services_for_host, config.ttl or ttl or _M.ttl)
cache:set(host, services_for_host, cache_ttl)
end

return config
Expand Down
5 changes: 4 additions & 1 deletion spec/configuration_loader_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ insulate('Configuration object', function()

it('returns false when configuration is stale', function()
local config = configuration_store.new()
config:add({ id = 42, hosts = { 'example.com' } }, -1)

-- Can't add stale info with config.add. Need to do it through the
-- internals of the object.
config.cache:set('example.com', { { id = 42, hosts = { 'example.com' } } }, -1)

assert.falsy(_M.configured(config, 'example.com'))
end)
Expand Down
14 changes: 9 additions & 5 deletions spec/configuration_store_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -151,20 +151,24 @@ describe('Configuration Store', function()

it('returns stale records by default', function()
local store = configuration.new()
local service = { id = '21', hosts = { 'example.com', 'localhost' } }
local service = { id = '21', hosts = { 'example.com' } }

store:add(service, -1)
-- Can't add stale info with config.add. Need to do it through the
-- internals of the object.
store.cache:set('example.com', { service }, -1)

assert.same({ service }, store:find_by_host('example.com'))
end)

it('does not return stale records when disabled', function()
local store = configuration.new()
local service = { id = '21', hosts = { 'example.com', 'localhost' } }
local service = { id = '21', hosts = { 'example.com' } }

store:add(service, -1)
-- Can't add stale info with config.add. Need to do it through the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add one with expiry 0 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tried it and it does not work. I guess it needs a bit of time to delete it even with ttl=0.

-- internals of the object.
store.cache:set('example.com', { service }, -1)

assert.same({ }, store:find_by_host('example.com', false))
assert.same({}, store:find_by_host('example.com', false))
end)

it('normalizes hosts to lowercase', function()
Expand Down