forked from nkval/nkv
-
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.
Merge pull request nkval#4 from uncleDecart/remove-nats
Remove nats and use TcpStream from Tokio. Less is more
- Loading branch information
Showing
7 changed files
with
491 additions
and
340 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 |
---|---|---|
@@ -1,69 +1,62 @@ | ||
pub mod nkv; | ||
mod notifier; | ||
pub mod request_msg; | ||
pub mod notifier; | ||
mod persist_value; | ||
pub mod request_msg; | ||
|
||
use crate::request_msg::*; | ||
|
||
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader, BufWriter}; | ||
use tokio::net::TcpStream; | ||
|
||
pub struct NatsClient { | ||
client: async_nats::Client, | ||
writer: BufWriter<tokio::net::tcp::OwnedWriteHalf>, | ||
reader: BufReader<tokio::net::tcp::OwnedReadHalf>, | ||
} | ||
|
||
impl NatsClient { | ||
pub async fn new(server: &str) -> Result<Self, async_nats::Error> { | ||
let client = async_nats::connect(server).await?; | ||
Ok(Self { client }) | ||
pub async fn new(addr: &str) -> tokio::io::Result<Self> { | ||
let stream = TcpStream::connect(addr).await?; | ||
let (read, write) = stream.into_split(); | ||
let writer = BufWriter::new(write); | ||
let reader = BufReader::new(read); | ||
|
||
Ok(Self { writer, reader }) | ||
} | ||
|
||
pub async fn get(&self, key: String) -> Result<ServerResponse, async_nats::Error> { | ||
let req = MessageBody::Get(BaseMessage{ | ||
id: 0, | ||
key: key, | ||
}); | ||
pub async fn get(&mut self, key: String) -> tokio::io::Result<ServerResponse> { | ||
let req = ServerRequest::Get(BaseMessage { id: 0, key }); | ||
self.send_request(&req).await | ||
} | ||
|
||
pub async fn put(&self, key: String, val: Box<[u8]>) -> Result<ServerResponse, async_nats::Error> { | ||
let req = MessageBody::Put(PutMessage{ | ||
base: BaseMessage{ | ||
id: 0, | ||
key: key, | ||
}, | ||
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 }, | ||
value: val, | ||
}); | ||
self.send_request(&req).await | ||
} | ||
|
||
pub async fn delete(&self, key: String) -> Result<ServerResponse, async_nats::Error> { | ||
let req = MessageBody::Delete(BaseMessage{ | ||
id: 0, | ||
key: key, | ||
}); | ||
pub async fn delete(&mut self, key: String) -> tokio::io::Result<ServerResponse> { | ||
let req = ServerRequest::Delete(BaseMessage { id: 0, key }); | ||
self.send_request(&req).await | ||
} | ||
|
||
pub async fn subscribe(&self, key: String) -> Result<ServerResponse, async_nats::Error> { | ||
let req = MessageBody::Subscribe(BaseMessage{ | ||
id: 0, | ||
key: key, | ||
}); | ||
pub async fn subscribe(&mut self, key: String) -> tokio::io::Result<ServerResponse> { | ||
let req = ServerRequest::Subscribe(BaseMessage { id: 0, key }); | ||
self.send_request(&req).await | ||
} | ||
async fn send_request(&self, request: &MessageBody) -> Result<ServerResponse, async_nats::Error> { | ||
|
||
async fn send_request(&mut self, request: &ServerRequest) -> tokio::io::Result<ServerResponse> { | ||
let req = serde_json::to_string(&request)?; | ||
let subj = match request { | ||
MessageBody::Put( .. ) => "pubsub.put", | ||
MessageBody::Get( .. ) => "pubsub.get", | ||
MessageBody::Delete( .. ) => "pubsub.delete", | ||
MessageBody::Subscribe( .. ) => "pubsub.subscribe", | ||
}; | ||
let reply = self.client | ||
.request(subj, req.into()) | ||
.await?; | ||
self.writer.write_all(req.as_bytes()).await?; | ||
self.writer.write_all(b"\n").await?; | ||
self.writer.flush().await?; | ||
|
||
let mut response_buf = String::new(); | ||
|
||
self.reader.read_line(&mut response_buf).await?; | ||
let response: ServerResponse = serde_json::from_str(&response_buf)?; | ||
|
||
|
||
let response_data: ServerResponse = serde_json::from_slice(&reply.payload)?; | ||
Ok(response_data) | ||
Ok(response) | ||
} | ||
} |
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
Oops, something went wrong.