diff --git a/docs/env-variables.md b/docs/env-variables.md index 904a05a1..c939f8f4 100644 --- a/docs/env-variables.md +++ b/docs/env-variables.md @@ -30,3 +30,10 @@ To configure the max payload size, you can set the `ROBYN_MAX_PAYLOAD_SIZE` envi ROBYN_MAX_PAYLOAD_SIZE=1000000 ``` +To configure the cache retention period, you can set the `ROBYN_CACHE_RETENTION` environment variable. The default value is `60` seconds. + +```bash +#robyn.env +ROBYN_MAX_PAYLOAD_SIZE=60 +``` + diff --git a/src/server.rs b/src/server.rs index 495d4e7f..6caffb9d 100644 --- a/src/server.rs +++ b/src/server.rs @@ -35,6 +35,8 @@ use pyo3::prelude::*; const MAX_PAYLOAD_SIZE: &str = "ROBYN_MAX_PAYLOAD_SIZE"; const DEFAULT_MAX_PAYLOAD_SIZE: usize = 1_000_000; // 1Mb +const CACHE_RETENTION: &str = "ROBYN_CACHE_RETENTION"; +const DEFAULT_CACHE_RETENTION: u64 = 60; // 1 Minute static STARTED: AtomicBool = AtomicBool::new(false); @@ -228,6 +230,16 @@ impl Server { }); let calls_count = self.calls_count.clone(); + let cache_retention = env::var(CACHE_RETENTION) + .unwrap_or(DEFAULT_CACHE_RETENTION.to_string()) + .trim() + .parse::() + .map_err(|e| { + PyValueError::new_err(format!( + "Failed to parse environment variable {MAX_PAYLOAD_SIZE} - {e}" + )) + })?; + Python::with_gil(|py| { py.allow_threads(|| { thread::spawn(move || loop { @@ -241,7 +253,7 @@ impl Server { calls_count.insert(item.key().clone(), valid_timestamps); calls_count.remove_if(item.key(), |_, t| t.is_empty()); } - thread::sleep(Duration::from_secs(60)); + thread::sleep(Duration::from_secs(cache_retention)); }); }); });