From 4aa8668e313ecec4a55b18988f0d8698e11c2a39 Mon Sep 17 00:00:00 2001 From: euzu Date: Sun, 27 Apr 2025 15:47:15 +0200 Subject: [PATCH] provider max connection limit --- src/api/model/active_provider_manager.rs | 14 +++++---- src/api/model/active_user_manager.rs | 7 ++--- src/api/model/provider_config.rs | 36 ++++++++---------------- src/model/healthcheck.rs | 2 +- src/processing/parser/m3u.rs | 4 +-- 5 files changed, 26 insertions(+), 37 deletions(-) diff --git a/src/api/model/active_provider_manager.rs b/src/api/model/active_provider_manager.rs index aff1834e8..8dc20c7f4 100644 --- a/src/api/model/active_provider_manager.rs +++ b/src/api/model/active_provider_manager.rs @@ -230,10 +230,12 @@ impl MultiProviderLineup { } } ProviderPriorityGroup::MultiProviderGroup(index, pg) => { - let mut idx_guard = index.lock().await; - let mut idx = *idx_guard; let provider_count = pg.len(); + let mut idx = { + *index.lock().await + }; let start = idx; + for _ in start..provider_count { let p = pg.get(idx).unwrap(); idx = (idx + 1) % provider_count; @@ -241,12 +243,12 @@ impl MultiProviderLineup { match result { ProviderAllocation::Exhausted => {} ProviderAllocation::Available(_) | ProviderAllocation::GracePeriod(_) => { - *idx_guard = idx; + *index.lock().await = idx; return result; } } } - *idx_guard = idx; + *index.lock().await = idx; } } ProviderAllocation::Exhausted @@ -520,8 +522,8 @@ impl ActiveProviderManager { } } - pub async fn active_connections(&self) -> Option> { - let mut result = HashMap::::new(); + pub async fn active_connections(&self) -> Option> { + let mut result = HashMap::::new(); let mut add_provider = async |provider: &ProviderConfig| { let count = provider.get_current_connections().await; if count > 0 { diff --git a/src/api/model/active_user_manager.rs b/src/api/model/active_user_manager.rs index dbc945fb6..bd7a9f0af 100644 --- a/src/api/model/active_user_manager.rs +++ b/src/api/model/active_user_manager.rs @@ -98,11 +98,10 @@ impl ActiveUserManager { // Grace timeout still active, deny connection debug!("User access denied, grace exhausted, too many connections: {username}"); return UserConnectionPermission::Exhausted; - } else { - // Grace timeout expired, reset grace counters - connection_data.granted_grace = false; - connection_data.grace_ts = 0; } + // Grace timeout expired, reset grace counters + connection_data.granted_grace = false; + connection_data.grace_ts = 0; } if self.grace_period_millis > 0 && current_connections == max_connections { diff --git a/src/api/model/provider_config.rs b/src/api/model/provider_config.rs index a5bbd5e00..ba519923e 100644 --- a/src/api/model/provider_config.rs +++ b/src/api/model/provider_config.rs @@ -13,23 +13,13 @@ pub enum ProviderConfigAllocation { GracePeriod, } -#[derive(Debug)] +#[derive(Debug, Default)] struct ProviderConfigConnection { - current_connections: u16, + current_connections: usize, granted_grace: bool, grace_ts: u64, } -impl Default for ProviderConfigConnection { - fn default() -> Self { - Self { - current_connections: 0, - granted_grace: false, - grace_ts: 0, - } - } -} - /// This struct represents an individual provider configuration with fields like: /// /// `id`, `name`, `url`, `username`, `password` @@ -45,7 +35,7 @@ pub struct ProviderConfig { pub username: Option, pub password: Option, pub input_type: InputType, - max_connections: u16, + max_connections: usize, priority: i16, connection: RwLock, } @@ -59,7 +49,7 @@ impl ProviderConfig { username: cfg.username.clone(), password: cfg.password.clone(), input_type: cfg.input_type, - max_connections: cfg.max_connections, + max_connections: cfg.max_connections as usize, priority: cfg.priority, connection: RwLock::new(ProviderConfigConnection::default()), } @@ -73,7 +63,7 @@ impl ProviderConfig { username: alias.username.clone(), password: alias.password.clone(), input_type: cfg.input_type, - max_connections: alias.max_connections, + max_connections: alias.max_connections as usize, priority: alias.priority, connection: RwLock::new(ProviderConfigConnection::default()), } @@ -134,11 +124,10 @@ impl ProviderConfig { // Grace timeout still active, deny connection debug!("Provider access denied, grace exhausted, too many connections: {}", self.name); return ProviderConfigAllocation::Exhausted; - } else { - // Grace timeout expired, reset grace counters - guard.granted_grace = false; - guard.grace_ts = 0; } + // Grace timeout expired, reset grace counters + guard.granted_grace = false; + guard.grace_ts = 0; } guard.granted_grace = true; guard.grace_ts = now; @@ -162,11 +151,10 @@ impl ProviderConfig { // Grace timeout still active, deny connection debug!("Provider access denied, grace exhausted, too many connections: {}", self.name); return false; - } else { - // Grace timeout expired, reset grace counters - guard.granted_grace = false; - guard.grace_ts = 0; } + // Grace timeout expired, reset grace counters + guard.granted_grace = false; + guard.grace_ts = 0; } return true; } @@ -183,7 +171,7 @@ impl ProviderConfig { } #[inline] - pub(crate) async fn get_current_connections(&self) -> u16 { + pub(crate) async fn get_current_connections(&self) -> usize { self.connection.read().await.current_connections } diff --git a/src/model/healthcheck.rs b/src/model/healthcheck.rs index 47d68896a..d6892bbe5 100644 --- a/src/model/healthcheck.rs +++ b/src/model/healthcheck.rs @@ -23,5 +23,5 @@ pub struct StatusCheck { pub active_users: usize, pub active_user_connections: usize, #[serde(skip_serializing_if = "Option::is_none")] - pub active_provider_connections: Option>, + pub active_provider_connections: Option>, } \ No newline at end of file diff --git a/src/processing/parser/m3u.rs b/src/processing/parser/m3u.rs index 4c35ec122..1e5fd401c 100644 --- a/src/processing/parser/m3u.rs +++ b/src/processing/parser/m3u.rs @@ -23,7 +23,7 @@ fn get_value(stack: &mut String, it: &mut std::str::Chars) -> String { stack.push(c); } - let result = stack.to_string(); + let result = (*stack).to_string(); stack.clear(); result } @@ -52,7 +52,7 @@ fn token_till(stack: &mut String, it: &mut std::str::Chars, stop_char: char, sta if stack.is_empty() { None } else { - let result = stack.to_string(); + let result = (*stack).to_string(); stack.clear(); Some(result) }