cache-loader-async-rs icon indicating copy to clipboard operation
cache-loader-async-rs copied to clipboard

Feedback!

Open huntc opened this issue 3 years ago • 1 comments

Thanks for providing this library!

I couldn't see how to feedback on my experience, but I just wanted to convey that using the library has been very pleasant. Here's a code snippet of how I set up my cache to read secrets from Hashicorp's Vault:

let cache: TtlCache = LoadingCache::with_meta_loader(
    TtlCacheBacking::with_backing(
        *unauthorized_timeout,
        LruCacheBacking::new(max_secrets_cached),
    ),
    move |secret_path| {
        let task_state = Arc::clone(&state);
        async move {
            let result = task_state
                .client
                .get(
                    task_state
                        .server
                        .join(&format!(
                            "{}v1/secret/data/{}",
                            task_state.server, secret_path
                        ))
                        .unwrap(),
                )
                .bearer_auth(&task_state.client_token)
                .send()
                .await;
            if let Ok(response) = result {
                let secret_reply = if response.status().is_success() {
                    response.json::<GetSecretReply>().await.ok()
                } else {
                    None
                };
                let lease_duration = secret_reply.as_ref().map(|sr| {
                    let mut lease_duration = None;
                    if let Some(ttl_field) = task_state.ttl_field.as_ref() {
                        if let Some(ttl) = sr.data.data.get(ttl_field) {
                            if let Ok(ttl_duration) = ttl.parse::<humantime::Duration>() {
                                lease_duration = Some(ttl_duration.into());
                            }
                        }
                    }
                    lease_duration.unwrap_or_else(|| Duration::from_secs(sr.lease_duration))
                });
                Ok(secret_reply).with_meta(lease_duration.map(TtlMeta::from))
            } else {
                Err(Error::Unavailable)
            }
        }
    },
);

Feel free to use this as a more (incomplete) complex example if that helps. Thanks once again!

huntc avatar May 26 '22 02:05 huntc

Hey! Sorry - Somehow this notification got lost in my inbox, - just some PR popped in and I saw this. Thank you for providing this example, I will see if I can link it somewhere!

ByteAlex avatar Sep 09 '22 11:09 ByteAlex