use std::collections::HashMap;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::RwLock;
use crate::fingerprint::browser_catalog::HeaderProfile;
pub struct SessionPool {
profiles: Vec<&'static HeaderProfile>,
rotate_after_requests: u32,
bindings: RwLock<HashMap<String, (usize, u32)>>,
cursor: AtomicUsize,
}
impl SessionPool {
#[must_use]
pub fn new(profiles: Vec<&'static HeaderProfile>, rotate_after_requests: u32) -> Self {
assert!(
!profiles.is_empty(),
"SessionPool::new requires at least one profile"
);
Self {
profiles,
rotate_after_requests: rotate_after_requests.max(1),
bindings: RwLock::new(HashMap::new()),
cursor: AtomicUsize::new(0),
}
}
pub fn profile_for(&self, host: &str) -> &'static HeaderProfile {
debug_assert!(!self.profiles.is_empty());
let mut bindings = self
.bindings
.write()
.unwrap_or_else(|poisoned| poisoned.into_inner());
if let Some(entry) = bindings.get_mut(host) {
let (idx, count) = *entry;
if count.saturating_add(1) < self.rotate_after_requests {
entry.1 += 1;
return self.profiles[idx];
}
}
let idx = self.cursor.fetch_add(1, Ordering::Relaxed) % self.profiles.len();
bindings.insert(host.to_string(), (idx, 1));
self.profiles[idx]
}
pub fn clear(&self) {
self.bindings
.write()
.unwrap_or_else(|poisoned| poisoned.into_inner())
.clear();
}
#[must_use]
pub fn snapshot(&self) -> Vec<(String, &'static str, u32)> {
let bindings = self
.bindings
.read()
.unwrap_or_else(|poisoned| poisoned.into_inner());
bindings
.iter()
.map(|(host, (idx, count))| (host.clone(), self.profiles[*idx].name, *count))
.collect()
}
}