Skip to content

Commit

Permalink
fix: change retention period to be env var
Browse files Browse the repository at this point in the history
  • Loading branch information
Ido Slonimsky committed Jul 23, 2023
1 parent dbc4aa3 commit d285f5f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
7 changes: 7 additions & 0 deletions docs/env-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

14 changes: 13 additions & 1 deletion src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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::<u64>()
.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 {
Expand All @@ -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));
});
});
});
Expand Down

0 comments on commit d285f5f

Please sign in to comment.