feat: show client IP in logs

This commit is contained in:
Greg Heartsfield
2022-11-02 18:33:44 -05:00
parent caffbbbede
commit 8ecce3f566
5 changed files with 35 additions and 20 deletions
+11 -3
View File
@@ -14,6 +14,8 @@ const MAX_SUBSCRIPTION_ID_LEN: usize = 256;
/// State for a client connection
pub struct ClientConn {
/// Client IP (either from socket, or configured proxy header
client_ip: String,
/// Unique client identifier generated at connection time
client_id: Uuid,
/// The current set of active client subscriptions
@@ -24,16 +26,17 @@ pub struct ClientConn {
impl Default for ClientConn {
fn default() -> Self {
Self::new()
Self::new("unknown".to_owned())
}
}
impl ClientConn {
/// Create a new, empty connection state.
#[must_use]
pub fn new() -> Self {
pub fn new(client_ip: String) -> Self {
let client_id = Uuid::new_v4();
ClientConn {
client_ip,
client_id,
subscriptions: HashMap::new(),
max_subs: 32,
@@ -47,6 +50,11 @@ impl ClientConn {
self.client_id.to_string().chars().take(8).collect()
}
#[must_use]
pub fn ip(&self) -> &str {
&self.client_ip
}
/// Find all matching subscriptions.
#[must_use]
pub fn get_matching_subscriptions(&self, e: &Event) -> Vec<&str> {
@@ -102,7 +110,7 @@ impl ClientConn {
// TODO: return notice if subscription did not exist.
self.subscriptions.remove(&c.id);
debug!(
"removed subscription, currently have {} active subs (cid={})",
"removed subscription, currently have {} active subs (cid={:?})",
self.subscriptions.len(),
self.client_id
);