Skip to content

Commit

Permalink
Generate unique UUID for requests
Browse files Browse the repository at this point in the history
This way it is easier to trace and debug things

Signed-off-by: Pavel Abramov <[email protected]>
  • Loading branch information
uncleDecart committed Sep 2, 2024
1 parent 54cb710 commit e080b6c
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 26 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ serde_json = "1.0.121"
tempdir = "0.3"
tempfile = "3.10.1"
tokio = { version = "1.39.1", features = ["full"] }
uuid = { version = "1.4", features = ["v4"] }

[lib]
name = "nkv"
Expand Down
6 changes: 3 additions & 3 deletions src/client/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async fn main() {
assert_eq!(
resp,
request_msg::ServerResponse::Base(request_msg::BaseResp {
id: 0,
id: "0".to_string(),
status: http::StatusCode::OK,
message: "No Error".to_string(),
})
Expand All @@ -62,7 +62,7 @@ async fn main() {
assert_eq!(
resp,
request_msg::ServerResponse::Base(request_msg::BaseResp {
id: 0,
id: "0".to_string(),
status: http::StatusCode::OK,
message: "No Error".to_string(),
})
Expand All @@ -77,7 +77,7 @@ async fn main() {
assert_eq!(
resp,
request_msg::ServerResponse::Base(request_msg::BaseResp {
id: 0,
id: "0".to_string(),
status: http::StatusCode::OK,
message: "No Error".to_string(),
})
Expand Down
24 changes: 19 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::fmt;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader, BufWriter};
use tokio::net::TcpStream;
use tokio::sync::watch;
use uuid::Uuid;

#[derive(Debug)]
pub enum NkvClientError {
Expand Down Expand Up @@ -40,29 +41,42 @@ impl NkvClient {
}
}

fn uuid() -> String {
"rust-nkv-client".to_string() + &Uuid::new_v4().to_string()
}

pub async fn get(&mut self, key: String) -> tokio::io::Result<ServerResponse> {
let req = ServerRequest::Get(BaseMessage { id: 0, key });
let req = ServerRequest::Get(BaseMessage {
id: Self::uuid(),
key,
});
self.send_request(&req).await
}

pub async fn put(&mut self, key: String, val: Box<[u8]>) -> tokio::io::Result<ServerResponse> {
let req = ServerRequest::Put(PutMessage {
base: BaseMessage { id: 0, key },
base: BaseMessage {
id: Self::uuid(),
key,
},
value: val,
});
self.send_request(&req).await
}

pub async fn delete(&mut self, key: String) -> tokio::io::Result<ServerResponse> {
let req = ServerRequest::Delete(BaseMessage { id: 0, key });
let req = ServerRequest::Delete(BaseMessage {
id: Self::uuid(),
key,
});
self.send_request(&req).await
}

pub async fn subscribe(&mut self, key: String) -> tokio::io::Result<ServerResponse> {
// we subscribe only once during client lifetime
if self.subscriptions.contains_key(&key) {
return Ok(ServerResponse::Base(BaseResp {
id: 0,
id: Self::uuid(),
status: http::StatusCode::FOUND,
message: "Already Subscribed".to_string(),
}));
Expand All @@ -77,7 +91,7 @@ impl NkvClient {
self.subscriptions.insert(key, rx);

Ok(ServerResponse::Base(BaseResp {
id: 0,
id: Self::uuid(),
status: http::StatusCode::OK,
message: "Subscribed".to_string(),
}))
Expand Down
2 changes: 1 addition & 1 deletion src/notifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl Subscriber {
let mut reader = BufReader::new(read_half);

let req = ServerRequest::Subscribe(BaseMessage {
id: 0,
id: "0".to_string(),
key: self.key.to_string(),
});
let req = serde_json::to_string(&req)?;
Expand Down
35 changes: 30 additions & 5 deletions src/request_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize)]
pub struct BaseMessage {
pub id: u32,
pub id: String,
pub key: String,
}

Expand All @@ -23,26 +23,51 @@ pub enum ServerRequest {
Subscribe(BaseMessage),
}

#[derive(Debug, serde::Deserialize, serde::Serialize, PartialEq)]
#[derive(Debug, serde::Deserialize, serde::Serialize)]
pub struct BaseResp {
pub id: u32,
pub id: String,

#[serde(with = "http_serde::status_code")]
pub status: StatusCode,
pub message: String,
}

#[derive(Debug, Deserialize, Serialize, PartialEq)]
impl PartialEq for BaseResp {
fn eq(&self, other: &Self) -> bool {
// Ignoring id intentionally
self.status == other.status && self.message == other.message
}
}

#[derive(Debug, Deserialize, Serialize)]
pub struct DataResp {
#[serde(flatten)]
pub base: BaseResp,
pub data: Vec<u8>,
}

#[derive(Debug, Deserialize, Serialize, PartialEq)]
impl PartialEq for DataResp {
fn eq(&self, other: &Self) -> bool {
self.base == other.base && self.data == other.data
}
}

#[derive(Debug, Deserialize, Serialize)]
pub enum ServerResponse {
Base(BaseResp),
Get(DataResp),
Put(DataResp),
Sub(DataResp),
}

impl PartialEq for ServerResponse {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Base(lhs), Self::Base(rhs)) => lhs == rhs,
(Self::Get(lhs), Self::Get(rhs)) => lhs == rhs,
(Self::Put(lhs), Self::Put(rhs)) => lhs == rhs,
(Self::Sub(lhs), Self::Sub(rhs)) => lhs == rhs,
_ => false,
}
}
}
24 changes: 12 additions & 12 deletions src/server/srv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl Server {
}
_ => {
let resp = request_msg::BaseResp {
id: 0,
id: "0".to_string(),
status: StatusCode::INTERNAL_SERVER_ERROR,
message: "wrong message for put handle".to_string(),
};
Expand Down Expand Up @@ -233,7 +233,7 @@ impl Server {
}
_ => {
let resp = request_msg::BaseResp {
id: 0,
id: "0".to_string(),
status: StatusCode::INTERNAL_SERVER_ERROR,
message: "wrong message for get handle".to_string(),
};
Expand Down Expand Up @@ -261,7 +261,7 @@ impl Server {
}
_ => {
let resp = request_msg::BaseResp {
id: 0,
id: "0".to_string(),
status: StatusCode::INTERNAL_SERVER_ERROR,
message: "wrong message for the handle".to_string(),
};
Expand All @@ -288,7 +288,7 @@ impl Server {
}
_ => {
let resp = request_msg::BaseResp {
id: 0,
id: "0".to_string(),
status: StatusCode::INTERNAL_SERVER_ERROR,
message: "wrong message for sub handle".to_string(),
};
Expand Down Expand Up @@ -413,7 +413,7 @@ mod tests {
assert_eq!(
resp,
request_msg::ServerResponse::Base(request_msg::BaseResp {
id: 0,
id: "0".to_string(),
status: http::StatusCode::OK,
message: "No Error".to_string(),
})
Expand All @@ -424,7 +424,7 @@ mod tests {
get_resp,
request_msg::ServerResponse::Get(request_msg::DataResp {
base: request_msg::BaseResp {
id: 0,
id: "0".to_string(),
status: http::StatusCode::OK,
message: "No Error".to_string(),
},
Expand All @@ -437,7 +437,7 @@ mod tests {
err_get_resp,
request_msg::ServerResponse::Get(request_msg::DataResp {
base: request_msg::BaseResp {
id: 0,
id: "0".to_string(),
status: http::StatusCode::NOT_FOUND,
message: "Not Found".to_string(),
},
Expand All @@ -452,7 +452,7 @@ mod tests {
assert_eq!(
sub_resp,
request_msg::ServerResponse::Base(request_msg::BaseResp {
id: 0,
id: "0".to_string(),
status: http::StatusCode::OK,
message: "Subscribed".to_string(),
})
Expand All @@ -470,7 +470,7 @@ mod tests {
assert_eq!(
sub_resp,
request_msg::ServerResponse::Base(request_msg::BaseResp {
id: 0,
id: "0".to_string(),
status: http::StatusCode::OK,
message: "Subscribed".to_string(),
})
Expand All @@ -483,7 +483,7 @@ mod tests {
assert_eq!(
resp,
request_msg::ServerResponse::Base(request_msg::BaseResp {
id: 0,
id: "0".to_string(),
status: http::StatusCode::OK,
message: "No Error".to_string(),
})
Expand All @@ -501,7 +501,7 @@ mod tests {
assert_eq!(
del_resp,
request_msg::ServerResponse::Base(request_msg::BaseResp {
id: 0,
id: "0".to_string(),
status: http::StatusCode::OK,
message: "No Error".to_string(),
})
Expand All @@ -518,7 +518,7 @@ mod tests {
del_get_resp,
request_msg::ServerResponse::Get(request_msg::DataResp {
base: request_msg::BaseResp {
id: 0,
id: "0".to_string(),
status: http::StatusCode::NOT_FOUND,
message: "Not Found".to_string(),
},
Expand Down

0 comments on commit e080b6c

Please sign in to comment.