aboutsummaryrefslogtreecommitdiffstats
path: root/crates/rule-executor/src/processor/rule/configuration.rs
blob: 6e11248dab89ddc111cd07c39026e7a5fdebd435 (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
41
42
use anyhow::{Result, anyhow};
use tracing::{Instrument, instrument, trace, trace_span};
use warden_core::configuration::rule::{RuleConfiguration, RuleConfigurationRequest};

use crate::state::AppHandle;

#[instrument(skip(state))]
pub(super) async fn get_configuration(
    request: RuleConfigurationRequest,
    state: AppHandle,
) -> Result<RuleConfiguration> {
    trace!("checking cache for rule configuration");
    let cache = state.local_cache.read().await;
    let config = cache.get(&request).await;
    if let Some(config) = config {
        trace!("cache hit");
        return Ok(config);
    }
    trace!("cache miss, asking config service");

    let mut client = state.query_rule_client.clone();

    let span = trace_span!(
        "get.rule.config",
        "otel.kind" = "client",
        "rpc.service" = "configuration"
    );
    let resp = client
        .get_rule_configuration(request.clone())
        .instrument(span)
        .await?
        .into_inner();

    let config = resp
        .configuration
        .ok_or_else(|| anyhow!("missing configuration"))?;

    let mut cache = state.local_cache.write().await;
    cache.insert(request, config.clone()).await;

    Ok(config)
}