diff --git a/src/configuration.rs b/src/configuration.rs index 6b8b790..9aef203 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -77,6 +77,7 @@ mod test { use core::time::Duration; use std::convert::TryInto; + use crate::configuration::api::v1::Cache; use threescalers::http::mapping_rule::{Method, RestRule}; use crate::threescale::{ @@ -121,6 +122,10 @@ mod test { token: "atoken".into(), ttl: Some(300), }), + cache: Some(Cache { + ttl: Some(10), + jitter: Some(15), + }), backend: Some(Backend { name: Some("backend-name".into()), upstream: Upstream { @@ -283,6 +288,10 @@ mod test { }, "token": "atoken" }, + "cache": { + "ttl": 10, + "jitter": 15 + }, "backend": { "name": "backend-name", "upstream": { diff --git a/src/configuration/api/v1.rs b/src/configuration/api/v1.rs index 10a194d..406becf 100644 --- a/src/configuration/api/v1.rs +++ b/src/configuration/api/v1.rs @@ -3,12 +3,19 @@ use serde::{Deserialize, Serialize}; use crate::configuration::MissingError; use crate::threescale::{Backend, Service, System}; +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Cache { + pub ttl: Option, + pub jitter: Option, +} + #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename = "3scale")] pub struct Configuration { pub system: Option, pub backend: Option, pub services: Option>, + pub cache: Option, // pass request to the next filter in the chain pub passthrough_metadata: Option, } diff --git a/src/proxy/root_context.rs b/src/proxy/root_context.rs index c917f3b..9217598 100644 --- a/src/proxy/root_context.rs +++ b/src/proxy/root_context.rs @@ -336,14 +336,13 @@ impl RootAuthThreescale { } fn get_next_tick(&self) -> Option<(Duration, Duration)> { - self.get_system_config().map(|sys| { - let jitter = self.rng.next_u32() as u64 & 0x0F; // add 0-15 seconds on top - - // ensure we only do this at most once per minute, and at least not within the timeout - let original_ttl = sys - .upstream() - .timeout - .clamp(Duration::from_secs(MIN_SYNC), sys.ttl()); + self.get_configuration().map(|config| { + let cache = config.cache.as_ref(); + let max_jitter = cache.and_then(|cache| cache.jitter).unwrap_or(15); + let original_ttl = + Duration::from_secs(cache.and_then(|cache| cache.ttl).unwrap_or(MIN_SYNC)); + + let jitter = self.rng.next_u32() as u64 & max_jitter; let ttl = original_ttl.saturating_add(Duration::from_secs(jitter)); info!( self,