mirror of
https://github.com/telemt/telemt.git
synced 2026-11-22 23:36:36 +03:00
Process-wide concurrency + Cancellation ownership fixes
This commit is contained in:
+35
-12
@@ -38,11 +38,16 @@ struct UserIpShard {
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct CleanupShard {
|
||||
queue: Mutex<HashMap<(String, UserIncarnation), HashMap<IpAddr, usize>>>,
|
||||
queue: Mutex<CleanupQueue>,
|
||||
}
|
||||
|
||||
type CleanupQueue =
|
||||
HashMap<String, HashMap<UserIncarnation, HashMap<IpAddr, usize>>>;
|
||||
type CleanupBatch = HashMap<(String, UserIncarnation, IpAddr), usize>;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct UserIpLimitPolicy {
|
||||
source_generation: u64,
|
||||
max_ips: Arc<HashMap<String, usize>>,
|
||||
default_max_ips: usize,
|
||||
mode: UserMaxUniqueIpsMode,
|
||||
@@ -52,6 +57,7 @@ struct UserIpLimitPolicy {
|
||||
impl Default for UserIpLimitPolicy {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
source_generation: 0,
|
||||
max_ips: Arc::new(HashMap::new()),
|
||||
default_max_ips: 0,
|
||||
mode: UserMaxUniqueIpsMode::ActiveWindow,
|
||||
@@ -70,6 +76,7 @@ pub struct UserIpTracker {
|
||||
recent_cap_rejects: Arc<AtomicU64>,
|
||||
cleanup_deferred_releases: Arc<AtomicU64>,
|
||||
limit_policy: Arc<ArcSwap<UserIpLimitPolicy>>,
|
||||
policy_update: Arc<Mutex<()>>,
|
||||
last_compact_epoch_secs: Arc<AtomicU64>,
|
||||
cleanup_queue_len: Arc<AtomicU64>,
|
||||
cleanup_shards: Arc<Box<[CleanupShard]>>,
|
||||
@@ -121,6 +128,7 @@ impl UserIpTracker {
|
||||
recent_cap_rejects: Arc::new(AtomicU64::new(0)),
|
||||
cleanup_deferred_releases: Arc::new(AtomicU64::new(0)),
|
||||
limit_policy: Arc::new(ArcSwap::from_pointee(UserIpLimitPolicy::default())),
|
||||
policy_update: Arc::new(Mutex::new(())),
|
||||
last_compact_epoch_secs: Arc::new(AtomicU64::new(0)),
|
||||
cleanup_queue_len: Arc::new(AtomicU64::new(0)),
|
||||
cleanup_shards: Arc::new(cleanup_shards),
|
||||
@@ -196,19 +204,21 @@ impl UserIpTracker {
|
||||
}
|
||||
|
||||
pub(super) fn pop_one_cleanup(
|
||||
queue: &mut HashMap<(String, UserIncarnation), HashMap<IpAddr, usize>>,
|
||||
queue: &mut CleanupQueue,
|
||||
) -> Option<(String, UserIncarnation, IpAddr, usize)> {
|
||||
let owner = queue.keys().next().cloned()?;
|
||||
let ip = queue.get(&owner)?.keys().next().copied()?;
|
||||
let count = queue.get_mut(&owner)?.remove(&ip)?;
|
||||
let remove_user = queue
|
||||
.get(&owner)
|
||||
.map(|user_queue| user_queue.is_empty())
|
||||
.unwrap_or(false);
|
||||
if remove_user {
|
||||
queue.remove(&owner);
|
||||
let user = queue.keys().next().cloned()?;
|
||||
let incarnation = queue.get(&user)?.keys().next().copied()?;
|
||||
let ip = queue.get(&user)?.get(&incarnation)?.keys().next().copied()?;
|
||||
let incarnations = queue.get_mut(&user)?;
|
||||
let ips = incarnations.get_mut(&incarnation)?;
|
||||
let count = ips.remove(&ip)?;
|
||||
if ips.is_empty() {
|
||||
incarnations.remove(&incarnation);
|
||||
}
|
||||
Some((owner.0, owner.1, ip, count))
|
||||
if incarnations.is_empty() {
|
||||
queue.remove(&user);
|
||||
}
|
||||
Some((user, incarnation, ip, count))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -224,6 +234,19 @@ impl UserIpTracker {
|
||||
#[cfg(not(test))]
|
||||
pub(super) fn observe_cleanup_poison_for_tests(&self) {}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) async fn hold_user_shard_for_tests(
|
||||
&self,
|
||||
user: &str,
|
||||
entered: tokio::sync::oneshot::Sender<()>,
|
||||
release: tokio::sync::oneshot::Receiver<()>,
|
||||
) {
|
||||
let shard_idx = Self::shard_idx(user);
|
||||
let _guard = self.shards[shard_idx].write().await;
|
||||
let _ = entered.send(());
|
||||
let _ = release.await;
|
||||
}
|
||||
|
||||
pub(super) fn now_epoch_secs() -> u64 {
|
||||
std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
|
||||
Reference in New Issue
Block a user