mirror of
https://github.com/YGGverse/nexy.git
synced 2026-04-02 02:05:29 +00:00
rename members
This commit is contained in:
parent
40bc6f773f
commit
d82f09d659
7 changed files with 33 additions and 33 deletions
44
src/session/stats/connections.rs
Normal file
44
src/session/stats/connections.rs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
use std::{
|
||||
collections::HashMap,
|
||||
net::{IpAddr, SocketAddr},
|
||||
sync::RwLock,
|
||||
};
|
||||
|
||||
/// Count peer connections (for the current server session)
|
||||
pub struct Connections(Option<RwLock<HashMap<IpAddr, usize>>>);
|
||||
|
||||
impl Connections {
|
||||
pub fn init(is_enabled: bool) -> Self {
|
||||
if is_enabled {
|
||||
Self(Some(RwLock::new(HashMap::with_capacity(100))))
|
||||
} else {
|
||||
Self(None)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add(&self, peer: &SocketAddr) {
|
||||
if let Some(ref this) = self.0 {
|
||||
this.write()
|
||||
.unwrap()
|
||||
.entry(peer.ip())
|
||||
.and_modify(|c| *c += 1)
|
||||
.or_insert(1);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn count(&self) -> usize {
|
||||
if let Some(ref this) = self.0 {
|
||||
this.read().unwrap().len()
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
pub fn total(&self) -> usize {
|
||||
if let Some(ref this) = self.0 {
|
||||
this.read().unwrap().values().sum()
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue