Skip to content

Commit

Permalink
feat(rust): Add ability to use runtime config from rust consumer (#5539)
Browse files Browse the repository at this point in the history
  • Loading branch information
lynnagara authored Feb 22, 2024
1 parent 60daf1e commit 9b40b77
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion rust_snuba/src/runtime_config.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
use anyhow::Error;
use parking_lot::RwLock;
use pyo3::prelude::{PyModule, Python};
use std::collections::BTreeMap;
use std::time::Duration;

use rust_arroyo::utils::timing::Deadline;

#[allow(dead_code)]
static CONFIG: RwLock<BTreeMap<String, (Option<String>, Deadline)>> = RwLock::new(BTreeMap::new());

/// Runtime config is cached for 10 seconds
#[allow(dead_code)]
pub fn get_str_config(key: &str) -> Result<Option<String>, Error> {
let deadline = Deadline::new(Duration::from_secs(10));

if let Some(value) = CONFIG.read().get(key) {
let (config, deadline) = value;
if !deadline.has_elapsed() {
return Ok(config.clone());
}
}

Python::with_gil(|py| {
let snuba_state = PyModule::import(py, "snuba.state")?;
let config = snuba_state
.getattr("get_str_config")?
.call1((key,))?
.extract::<Option<String>>()?;

Ok(config)
CONFIG
.write()
.insert(key.to_string(), (config.clone(), deadline));
Ok(CONFIG.read().get(key).unwrap().0.clone())
})
}

Expand Down

0 comments on commit 9b40b77

Please sign in to comment.