ngdp_client/
cached_client.rs

1//! Cached client support with automatic Ribbit to TACT fallback
2//!
3//! This module provides a client that uses Ribbit as the primary protocol
4//! and automatically falls back to TACT if Ribbit fails. Both protocols
5//! return identical BPSV data, ensuring seamless operation.
6
7use crate::fallback_client::FallbackClient;
8use ribbit_client::Region;
9use std::sync::OnceLock;
10
11/// Global flag to control whether caching is enabled
12static CACHING_ENABLED: OnceLock<bool> = OnceLock::new();
13
14/// Set whether caching is enabled globally
15pub fn set_caching_enabled(enabled: bool) {
16    let _ = CACHING_ENABLED.set(enabled);
17}
18
19/// Check if caching is enabled (defaults to true)
20pub fn is_caching_enabled() -> bool {
21    *CACHING_ENABLED.get_or_init(|| true)
22}
23
24/// Create a client with automatic Ribbit->TACT fallback and optional caching
25///
26/// If caching is enabled (default), both the Ribbit and TACT clients will
27/// transparently cache all requests. The client will try Ribbit first (as
28/// it's the primary protocol) and fall back to TACT on failure.
29pub async fn create_client(region: Region) -> Result<FallbackClient, Box<dyn std::error::Error>> {
30    let mut client = FallbackClient::new(region).await?;
31
32    // If caching is disabled globally, disable it on the client
33    if !is_caching_enabled() {
34        client.set_caching_enabled(false);
35    }
36
37    Ok(client)
38}