pollux/src/logging.rs
Jeena 33ae576b25 Implement rate limiting with 41 responses and comprehensive logging
- Add concurrent connection handling with tokio::spawn for proper rate limiting
- Send '41 Server unavailable' responses instead of dropping connections
- Move request logger initialization earlier to enable rate limiting logs
- Add logging for rate limited requests: 'Concurrent request limit exceeded'
- Fix clippy warnings: needless borrows and match simplification
- Update test script analysis to expect 41 responses for rate limiting
2026-01-16 06:00:18 +00:00

49 lines
No EOL
1.2 KiB
Rust

use tokio::net::TcpStream;
use tokio_rustls::server::TlsStream;
pub struct RequestLogger {
client_ip: String,
request_url: String,
}
impl RequestLogger {
pub fn new(stream: &TlsStream<TcpStream>, request_url: String) -> Self {
let client_ip = extract_client_ip(stream);
Self {
client_ip,
request_url,
}
}
pub fn log_success(self, status_code: u8) {
println!("{} \"{}\" {}", self.client_ip, self.request_url, status_code);
}
pub fn log_error(self, status_code: u8, error_message: &str) {
eprintln!("{} \"{}\" {} \"{}\"", self.client_ip, self.request_url, status_code, error_message);
}
}
fn extract_client_ip(stream: &TlsStream<TcpStream>) -> String {
let (tcp_stream, _) = stream.get_ref();
match tcp_stream.peer_addr() {
Ok(addr) => addr.to_string(),
Err(_) => "unknown".to_string(),
}
}
pub fn init_logging(_level: &str) {
// Simple logging using stdout/stderr - systemd will capture this
}
#[cfg(test)]
mod tests {
#[test]
fn test_basic_functionality() {
// Basic test to ensure logging module compiles
assert!(true);
}
}