|
| 1 | +use crate::client::Client; |
| 2 | +use crate::cmd::{Command, Get, Set}; |
| 3 | +use crate::Result; |
| 4 | +use bytes::Bytes; |
| 5 | +use std::time::Duration; |
| 6 | +use tokio::sync::mpsc::{channel, Receiver, Sender}; |
| 7 | +use tokio::sync::oneshot; |
| 8 | +use tracing::error; |
| 9 | + |
| 10 | +/// create a new connection Pool from a Client |
| 11 | +pub fn create(client: Client) -> Pool { |
| 12 | + // Setting the message limit to a hard coded value of 32. |
| 13 | + // in a real-app, the buffer size should be configurable, but we don't need to do that here. |
| 14 | + let (tx, rx) = channel(32); |
| 15 | + tokio::spawn(async move { run(client, rx).await }); |
| 16 | + |
| 17 | + Pool { tx } |
| 18 | +} |
| 19 | + |
| 20 | +/// await for commands send through the channel and forward them to client, then send the result back to the oneshot Receiver |
| 21 | +async fn run( |
| 22 | + mut client: Client, |
| 23 | + mut rx: Receiver<(Command, oneshot::Sender<Result<Option<Bytes>>>)>, |
| 24 | +) { |
| 25 | + while let Some((cmd, tx)) = rx.recv().await { |
| 26 | + match cmd { |
| 27 | + Command::Get(get) => { |
| 28 | + let key = get.key(); |
| 29 | + let result = client.get(&key).await; |
| 30 | + if let Err(_) = tx.send(result) { |
| 31 | + error!("failed to send Client result, receiver has already been dropped"); |
| 32 | + } |
| 33 | + } |
| 34 | + Command::Set(set) => { |
| 35 | + let key = set.key(); |
| 36 | + let value = set.value(); |
| 37 | + let expires = set.expire(); |
| 38 | + let result = match expires { |
| 39 | + None => client.set(&key, value).await, |
| 40 | + Some(exp) => client.set_expires(&key, value, exp).await, |
| 41 | + }; |
| 42 | + if let Err(_) = tx.send(result.map(|_| None)) { |
| 43 | + error!("failed to send Client result, receiver has already been dropped"); |
| 44 | + } |
| 45 | + } |
| 46 | + _ => unreachable!(), |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +pub struct Pool { |
| 52 | + tx: Sender<(Command, oneshot::Sender<Result<Option<Bytes>>>)>, |
| 53 | +} |
| 54 | + |
| 55 | +impl Pool { |
| 56 | + /// get a Connection like object to the mini-redis server instance |
| 57 | + pub fn get_connection(&self) -> Connection { |
| 58 | + Connection { |
| 59 | + tx: self.tx.clone(), |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/// a Connection like object that proxies commands to the real connection |
| 65 | +/// Commands are send trough mspc Channel, along with the requested Command a oneshot Sender is sent |
| 66 | +/// the Result from the actual Client requested command is then sent through the oneshot Sender and Received on the Connection Receiver |
| 67 | +pub struct Connection { |
| 68 | + tx: Sender<(Command, oneshot::Sender<Result<Option<Bytes>>>)>, |
| 69 | +} |
| 70 | + |
| 71 | +impl Connection { |
| 72 | + pub async fn get(&mut self, key: &str) -> Result<Option<Bytes>> { |
| 73 | + let get = Get::new(key); |
| 74 | + let (tx, rx) = oneshot::channel(); |
| 75 | + self.tx.send((Command::Get(get), tx)).await?; |
| 76 | + match rx.await { |
| 77 | + Ok(res) => res, |
| 78 | + Err(err) => Err(err.into()), |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + pub async fn set(&mut self, key: &str, value: Bytes) -> Result<()> { |
| 83 | + let get = Set::new(key, value, None); |
| 84 | + let (tx, rx) = oneshot::channel(); |
| 85 | + self.tx.send((Command::Set(get), tx)).await?; |
| 86 | + match rx.await { |
| 87 | + Ok(res) => res.map(|_| ()), |
| 88 | + Err(err) => Err(err.into()), |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + pub async fn set_expires( |
| 93 | + &mut self, |
| 94 | + key: &str, |
| 95 | + value: Bytes, |
| 96 | + expiration: Duration, |
| 97 | + ) -> crate::Result<()> { |
| 98 | + let get = Set::new(key, value, Some(expiration)); |
| 99 | + let (tx, rx) = oneshot::channel(); |
| 100 | + self.tx.send((Command::Set(get), tx)).await?; |
| 101 | + match rx.await { |
| 102 | + Ok(res) => res.map(|_| ()), |
| 103 | + Err(err) => Err(err.into()), |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments