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

Allow cache configuration #113

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
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