aboutsummaryrefslogtreecommitdiffstats
path: root/crates/typologies/src/processor/driver.rs
blob: d150620e5ce594db85c57fe521a035337c6f83e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use tonic::IntoRequest;
use warden_core::configuration::typology::{TypologyConfiguration, TypologyConfigurationRequest};

use crate::state::AppHandle;

pub trait GetTypologyConfiguration {
    fn get_typology_config(
        &self,
        typology_key: TypologyConfigurationRequest,
    ) -> impl std::future::Future<Output = anyhow::Result<TypologyConfiguration>> + Send;
}

impl GetTypologyConfiguration for AppHandle {
    async fn get_typology_config(
        &self,
        typology_key: TypologyConfigurationRequest,
    ) -> anyhow::Result<TypologyConfiguration> {
        {
            let local_cache = self.local_cache.read().await;
            if let Some(result) = local_cache.get(&typology_key).await.map(Ok) {
                return result;
            }
        }

        let local_cache = self.local_cache.write().await;
        let mut client = self.query_typology_client.clone();

        let value = client
            .get_typology_configuration(typology_key.clone().into_request())
            .await?
            .into_inner()
            .configuration
            .ok_or_else(|| anyhow::anyhow!("configuration unavailable"))?;
        local_cache
            .insert(typology_key.clone(), value.clone())
            .await;

        Ok(value)
    }
}