use std::collections::HashMap;
use std::sync::Arc;
use super::inner::KeyInner;
use super::lease::KeyLease;
use super::pool::KeyPool;
use crate::config::{KeyConfig, PoolConfig};
pub type ProviderId = String;
pub type ModelId = String;
pub struct PoolRegistry {
pools: HashMap<(ProviderId, ModelId), KeyPool>,
}
impl PoolRegistry {
pub fn new() -> Self {
Self {
pools: HashMap::new(),
}
}
pub fn register(
&mut self,
provider: ProviderId,
model: ModelId,
keys: Vec<KeyConfig>,
config: PoolConfig,
) {
let inner_keys: Vec<Arc<KeyInner>> = keys
.into_iter()
.map(|kc| Arc::new(KeyInner::new(kc.key, kc.label, kc.tpm_limit, kc.rpm_limit)))
.collect();
self.pools
.insert((provider, model), KeyPool::new(inner_keys, config));
}
pub fn acquire(&self, provider: &str, model: &str, estimated_tokens: u32) -> Option<KeyLease> {
self.pools
.get(&(provider.to_string(), model.to_string()))?
.acquire(estimated_tokens)
}
}
impl Default for PoolRegistry {
fn default() -> Self {
Self::new()
}
}