Skip to main content

spin_factor_key_value/
runtime_config.rs

1pub mod spin;
2
3use std::{collections::HashMap, sync::Arc, time::Duration};
4
5use crate::StoreManager;
6
7/// Runtime configuration for all key value stores.
8#[derive(Default, Clone)]
9pub struct RuntimeConfig {
10    /// Map of store names to store managers.
11    store_managers: HashMap<String, Arc<dyn StoreManager>>,
12    /// Maximum number of concurrent in-flight key-value operations for this app.
13    max_concurrent_operations: Option<usize>,
14    /// Optional timeout for waiting to acquire a key-value operation permit.
15    wait_timeout: Option<Duration>,
16}
17
18impl RuntimeConfig {
19    /// Adds a store manager for the store with the given label to the runtime configuration.
20    ///
21    /// If a store manager already exists for the given label, it will be replaced.
22    pub fn add_store_manager(&mut self, label: String, store_manager: Arc<dyn StoreManager>) {
23        self.store_managers.insert(label, store_manager);
24    }
25
26    /// Returns whether a store manager exists for the store with the given label.
27    pub fn has_store_manager(&self, label: &str) -> bool {
28        self.store_managers.contains_key(label)
29    }
30
31    /// Returns the store manager for the store with the given label.
32    pub fn get_store_manager(&self, label: &str) -> Option<Arc<dyn StoreManager>> {
33        self.store_managers.get(label).cloned()
34    }
35
36    /// Sets the maximum number of concurrent in-flight key-value operations for this app.
37    pub fn set_max_concurrent_operations(&mut self, max: Option<usize>) {
38        self.max_concurrent_operations = max;
39    }
40
41    /// Sets the timeout used when waiting to acquire a key-value operation permit.
42    pub fn set_wait_timeout(&mut self, wait_timeout: Option<Duration>) {
43        self.wait_timeout = wait_timeout;
44    }
45
46    /// Returns the maximum number of concurrent in-flight key-value operations for this app.
47    pub fn max_concurrent_operations(&self) -> Option<usize> {
48        self.max_concurrent_operations
49    }
50
51    /// Returns the timeout used when waiting to acquire a key-value operation permit.
52    pub fn wait_timeout(&self) -> Option<Duration> {
53        self.wait_timeout
54    }
55}
56
57impl IntoIterator for RuntimeConfig {
58    type Item = (String, Arc<dyn StoreManager>);
59    type IntoIter = std::collections::hash_map::IntoIter<String, Arc<dyn StoreManager>>;
60
61    fn into_iter(self) -> Self::IntoIter {
62        self.store_managers.into_iter()
63    }
64}
65
66#[cfg(test)]
67mod tests {
68    use super::RuntimeConfig;
69    use std::time::Duration;
70
71    #[test]
72    fn runtime_config_can_set_operation_limits() {
73        let mut config = RuntimeConfig::default();
74        assert_eq!(config.max_concurrent_operations(), None);
75        assert_eq!(config.wait_timeout(), None);
76
77        config.set_max_concurrent_operations(Some(7));
78        config.set_wait_timeout(Some(Duration::from_millis(250)));
79
80        assert_eq!(config.max_concurrent_operations(), Some(7));
81        assert_eq!(config.wait_timeout(), Some(Duration::from_millis(250)));
82    }
83}