From 475e34633272e0b5585c0f06beeb0caca793687a Mon Sep 17 00:00:00 2001 From: David Ortiz Date: Fri, 16 Mar 2018 13:03:54 +0100 Subject: [PATCH 1/4] configuration_store: convert ttl < 0 to nil This allows us to set keys without expiration in the cache setting the appropriate configs to -1 or anything < 0. --- gateway/src/apicast/configuration_store.lua | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/gateway/src/apicast/configuration_store.lua b/gateway/src/apicast/configuration_store.lua index c5e3ac25a..14e2c5764 100644 --- a/gateway/src/apicast/configuration_store.lua +++ b/gateway/src/apicast/configuration_store.lua @@ -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 From df0c8898513956bca0b6f005faa87fab0daee745 Mon Sep 17 00:00:00 2001 From: David Ortiz Date: Fri, 16 Mar 2018 15:54:10 +0100 Subject: [PATCH 2/4] spec/configuration_{loader,store}: adapt to ttl < 0 == not expire --- spec/configuration_loader_spec.lua | 5 ++++- spec/configuration_store_spec.lua | 14 +++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/spec/configuration_loader_spec.lua b/spec/configuration_loader_spec.lua index 7f0f3036b..1b13dfff9 100644 --- a/spec/configuration_loader_spec.lua +++ b/spec/configuration_loader_spec.lua @@ -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) diff --git a/spec/configuration_store_spec.lua b/spec/configuration_store_spec.lua index 288842dac..ea203d91b 100644 --- a/spec/configuration_store_spec.lua +++ b/spec/configuration_store_spec.lua @@ -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 + -- 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() From 51568fa1075c8042f21d5210cfce77126160c9e9 Mon Sep 17 00:00:00 2001 From: David Ortiz Date: Fri, 16 Mar 2018 15:59:37 +0100 Subject: [PATCH 3/4] doc/parameters: document APICAST_CONFIGURATION_CACHE < 0 --- doc/parameters.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/parameters.md b/doc/parameters.md index 174dc1702..c17ebb5db 100644 --- a/doc/parameters.md +++ b/doc/parameters.md @@ -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` From 142b620f896afa33c62132b43735afe2d0d9f904 Mon Sep 17 00:00:00 2001 From: David Ortiz Date: Fri, 16 Mar 2018 18:10:00 +0100 Subject: [PATCH 4/4] CHANGELOG: add entry for APICAST_CONFIGURATION_CACHE < 0 with lazy config loader --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54c3d7024..2528ea30a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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