mirror of
https://github.com/YGGverse/nexy.git
synced 2026-03-31 17:25:27 +00:00
update option level
This commit is contained in:
parent
9643adbe99
commit
57dfe79568
3 changed files with 62 additions and 72 deletions
|
|
@ -39,7 +39,9 @@ impl Connection {
|
|||
"[{}] < [{}] request `{q}`...",
|
||||
self.address.server, self.address.client
|
||||
));
|
||||
self.session.request.add(&self.address.client, &q);
|
||||
if let Some(ref i) = self.session.request {
|
||||
i.add(&self.address.client, &q)
|
||||
}
|
||||
self.session
|
||||
.clone()
|
||||
.public
|
||||
|
|
@ -74,14 +76,14 @@ impl Connection {
|
|||
&if is_root {
|
||||
self.session.template.welcome(
|
||||
Some(s),
|
||||
Some(self.session.request.count()),
|
||||
Some(self.session.request.total(None)),
|
||||
self.session.request.as_ref().map(|i| i.count()),
|
||||
self.session.request.as_ref().map(|i| i.total(None)),
|
||||
)
|
||||
} else {
|
||||
self.session.template.index(
|
||||
Some(s),
|
||||
Some(self.session.request.count()),
|
||||
Some(self.session.request.total(Some(q))),
|
||||
self.session.request.as_ref().map(|i| i.count()),
|
||||
self.session.request.as_ref().map(|i| i.total(Some(q))),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ pub struct Session {
|
|||
pub access_log: AccessLog,
|
||||
pub debug: Debug,
|
||||
pub public: Public,
|
||||
pub request: Request,
|
||||
pub request: Option<Request>,
|
||||
pub template: Template,
|
||||
}
|
||||
|
||||
|
|
@ -22,13 +22,15 @@ impl Session {
|
|||
access_log: AccessLog::init(config)?,
|
||||
debug: Debug::init(config)?,
|
||||
public: Public::init(config)?,
|
||||
request: Request::init(
|
||||
// do not int request collector if its features not in use
|
||||
template.welcome.contains("{hosts}")
|
||||
request: if template.welcome.contains("{hosts}")
|
||||
|| template.welcome.contains("{hits}")
|
||||
|| template.index.contains("{hosts}")
|
||||
|| template.index.contains("{hits}"),
|
||||
),
|
||||
|| template.index.contains("{hits}")
|
||||
{
|
||||
Some(Request::new())
|
||||
} else {
|
||||
None // do not int request collector if its features not in use
|
||||
},
|
||||
template,
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,28 +9,23 @@ use std::{
|
|||
|
||||
/// Collect peer requests for stats and visitors count
|
||||
pub struct Request {
|
||||
index: Option<RwLock<HashMap<IpAddr, Vec<Query>>>>,
|
||||
index: RwLock<HashMap<IpAddr, Vec<Query>>>,
|
||||
// prevent log file overflow by recording error events once
|
||||
is_max_peers_reported: RwLock<bool>,
|
||||
is_max_peer_queries_reported: RwLock<bool>,
|
||||
}
|
||||
|
||||
impl Request {
|
||||
pub fn init(is_enabled: bool) -> Self {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
index: if is_enabled {
|
||||
Some(RwLock::new(HashMap::with_capacity(100)))
|
||||
} else {
|
||||
None
|
||||
},
|
||||
index: RwLock::new(HashMap::with_capacity(100)),
|
||||
is_max_peers_reported: RwLock::new(false),
|
||||
is_max_peer_queries_reported: RwLock::new(false),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add(&self, peer: &SocketAddr, query: &str) {
|
||||
if let Some(ref this) = self.index {
|
||||
let mut index = this.write().unwrap();
|
||||
let mut index = self.index.write().unwrap();
|
||||
|
||||
// Critical limits to forcefully free one memory slot(s) for the new record
|
||||
// * the query len is already limited by the read buffer (1024 bytes * LIMIT)
|
||||
|
|
@ -52,9 +47,7 @@ impl Request {
|
|||
let mut r = self.is_max_peer_queries_reported.write().unwrap();
|
||||
if !*r {
|
||||
*r = true;
|
||||
eprintln!(
|
||||
"Max queries limit ({INDEX_MAX_PEER_QUERIES}) reached for `{peer}`"
|
||||
);
|
||||
eprintln!("Max queries limit ({INDEX_MAX_PEER_QUERIES}) reached for `{peer}`");
|
||||
}
|
||||
queries.truncate(INDEX_MAX_PEER_QUERIES - 1) // free last slot for one query
|
||||
}
|
||||
|
|
@ -74,20 +67,14 @@ impl Request {
|
|||
.and_modify(|c| c.push(Query::new(query)))
|
||||
.or_insert(vec![Query::new(query)]);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn count(&self) -> usize {
|
||||
if let Some(ref i) = self.index {
|
||||
i.read().unwrap().len()
|
||||
} else {
|
||||
0
|
||||
}
|
||||
self.index.read().unwrap().len()
|
||||
}
|
||||
|
||||
pub fn total(&self, query_prefix: Option<&str>) -> usize {
|
||||
let mut t = 0;
|
||||
if let Some(ref i) = self.index {
|
||||
for queries in i.read().unwrap().values() {
|
||||
for queries in self.index.read().unwrap().values() {
|
||||
match query_prefix {
|
||||
Some(p) => {
|
||||
for q in queries {
|
||||
|
|
@ -99,7 +86,6 @@ impl Request {
|
|||
None => t += queries.len(),
|
||||
}
|
||||
}
|
||||
}
|
||||
t
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue