pdk-rate-limit-lib 1.7.0

PDK Rate Limit Library
Documentation
// Copyright (c) 2026, Salesforce, Inc.,
// All rights reserved.
// For full license text, see the LICENSE.txt file

use url::form_urlencoded;

use self::KeyManagerFactory::{Cluster, Local};

/// Factory to create key managers.
pub enum KeyManagerFactory {
    Local(String),
    Cluster(String),
}

impl KeyManagerFactory {
    /// Create a new key manager factory.
    pub fn new(prefix: &str, cluster: bool) -> Self {
        let prefix = prefix.to_string();
        if cluster {
            Cluster(prefix)
        } else {
            Local(prefix)
        }
    }

    /// Create a new key manager with the specified bucket and group.
    pub fn create(&self, bucket: &str, group: &str) -> KeyManager {
        match self {
            Local(prefix) => KeyManager::Local {
                data_key: format!("{prefix}-{bucket}-{group}"),
            },
            Cluster(prefix) => KeyManager::Cluster {
                data_key: format!("{prefix}-{bucket}-{group}"),
                lock_key: format!("{prefix}-{bucket}-{group}-lock"),
                storage_key: form_urlencoded::byte_serialize(
                    format!("{bucket}-{group}").as_bytes(),
                )
                .collect(),
            },
        }
    }
}

/// Key manager for the rate limiting.
pub enum KeyManager {
    Local {
        data_key: String,
    },
    Cluster {
        data_key: String,
        lock_key: String,
        storage_key: String,
    },
}

impl KeyManager {
    /// Get the data key for the key manager.
    pub fn data_key(&self) -> &str {
        match self {
            KeyManager::Local { data_key } => data_key,
            KeyManager::Cluster { data_key, .. } => data_key,
        }
    }

    /// Get the storage key for the key manager.
    pub fn storage_key(&self) -> &str {
        match self {
            KeyManager::Local { .. } => "",
            KeyManager::Cluster { storage_key, .. } => storage_key,
        }
    }

    /// Get the lock key for the key manager.
    pub fn lock_key(&self) -> &str {
        match self {
            KeyManager::Local { .. } => "",
            KeyManager::Cluster { lock_key, .. } => lock_key,
        }
    }
}