-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to have a plugins folder (#15)
This refactors and demonstrates how a lot of plugins can be added to this repository.
- Loading branch information
1 parent
a5d1539
commit 964ac21
Showing
6 changed files
with
98 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod redis; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use anyhow::Result; | ||
use std::{collections::HashMap, sync::Arc}; | ||
use tokio::sync::Mutex; | ||
|
||
use crate::tun::{Metrics, Plugin}; | ||
|
||
use super::resp_parser::{parse_resp, RespValue}; | ||
|
||
pub struct RespHandler { | ||
port: u16, | ||
key_map: Arc<Mutex<HashMap<u32, RespValue>>>, | ||
} | ||
|
||
impl RespHandler { | ||
pub fn new(port: u16) -> Self { | ||
RespHandler { | ||
port, | ||
key_map: Arc::new(Mutex::new(HashMap::new())), | ||
} | ||
} | ||
} | ||
|
||
impl Plugin<RespValue> for RespHandler { | ||
async fn port(&self) -> u16 { | ||
self.port | ||
} | ||
|
||
async fn parse_packet(&self, buf: Vec<u8>) -> Result<RespValue> { | ||
let resp = parse_resp(&buf).map_err(|_| anyhow::anyhow!("Failed to parse packet"))?; | ||
Ok(resp.1) | ||
} | ||
|
||
async fn process(&self, input: RespValue, metrics: Option<Metrics>) -> Result<()> { | ||
// Return if none and unpack the metrics | ||
if metrics.is_none() { | ||
return Ok(()); | ||
} | ||
// We already know that metrics is not None | ||
let metrics = metrics.unwrap(); | ||
|
||
let mut store = self.key_map.lock().await; | ||
if !store.contains_key(&metrics.identifier) { | ||
// Check if the identifier exists and save it in the store | ||
store.insert(metrics.identifier, input.clone()); | ||
} | ||
|
||
if let Some(latency) = metrics.latency { | ||
let status = if input.to_string().contains("ERR") { | ||
"ERR" | ||
} else { | ||
"OK" | ||
}; | ||
// Print the latency and the key | ||
let stored_value = store | ||
.get(&metrics.identifier) | ||
.ok_or_else(|| anyhow::anyhow!("Failed to get value from store"))?; | ||
println!( | ||
"Key: {}, Latency: {}ms, Status: {}", | ||
stored_value.key.as_ref().unwrap(), | ||
latency.as_millis(), | ||
status, | ||
); | ||
// clean up the store | ||
store.remove(&metrics.identifier); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod handler; | ||
mod resp_parser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters