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 super::bucket::cluster::ClusterLimit;

/// Distribution formula for the rate limiting.
pub enum DistributionFormula {
    /// As less requests are available in the cluster, less requests are given.
    RemainingPercentage(f32),
    /// Given requests is always a predetermined percentage of the max requests.
    #[allow(dead_code)]
    TotalPercentage(f32),
}

impl DistributionFormula {
    /// Get the portion of the requests to be given to the cluster.
    pub fn portion(&self, limit: &ClusterLimit) -> u64 {
        match self {
            DistributionFormula::RemainingPercentage(percentage) => {
                ((limit.cluster_remaining as f32) * percentage) as u64
            }
            DistributionFormula::TotalPercentage(percentage) => {
                ((limit.max_requests as f32) * percentage) as u64
            }
        }
    }
}