koios-sdk 0.1.1

A Rust SDK for the Koios Cardano API
Documentation
// src/client/rate_limit.rs

use governor::{
    clock::{Clock, DefaultClock},
    state::{InMemoryState, NotKeyed},
    RateLimiter as Governor,
};
use std::sync::Arc;
use std::time::Duration;

/// Custom rate limit error type
#[derive(Debug)]
pub struct RateLimitError {
    wait_time: Duration,
}

impl RateLimitError {
    pub fn wait_time(&self) -> Duration {
        self.wait_time
    }
}

/// Rate limiter implementation for the API client
#[derive(Debug, Clone)]
pub struct RateLimiter {
    limiter: Arc<Governor<NotKeyed, InMemoryState, DefaultClock>>,
}

impl RateLimiter {
    /// Create a new rate limiter with the specified quota
    pub fn new(quota: governor::Quota) -> Self {
        Self {
            limiter: Arc::new(Governor::direct(quota)),
        }
    }

    /// Check if a request can be made
    pub async fn check(&self) -> Result<(), RateLimitError> {
        self.limiter.check().map_err(|negative| RateLimitError {
            wait_time: negative.wait_time_from(self.limiter.clock().now()),
        })
    }
}

impl Default for RateLimiter {
    fn default() -> Self {
        Self::new(governor::Quota::per_second(100u32.try_into().unwrap()))
    }
}