Skip to content

Commit

Permalink
Allow cache configuration
Browse files Browse the repository at this point in the history
- Currently, you can configure ttl and jitter
  • Loading branch information
pehala committed Jun 13, 2023
1 parent 2918f08 commit 51e8038
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
9 changes: 9 additions & 0 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -283,6 +288,10 @@ mod test {
},
"token": "atoken"
},
"cache": {
"ttl": 10,
"jitter": 15
},
"backend": {
"name": "backend-name",
"upstream": {
Expand Down
7 changes: 7 additions & 0 deletions src/configuration/api/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64>,
pub jitter: Option<u64>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename = "3scale")]
pub struct Configuration {
pub system: Option<System>,
pub backend: Option<Backend>,
pub services: Option<Vec<Service>>,
pub cache: Option<Cache>,
// pass request to the next filter in the chain
pub passthrough_metadata: Option<bool>,
}
Expand Down
15 changes: 7 additions & 8 deletions src/proxy/root_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 51e8038

Please sign in to comment.