]> piware.de Git - learn-rust.git/commitdiff
tokio-tutorial-mini-redis: Show return value in client
authorMartin Pitt <martin@piware.de>
Fri, 16 Sep 2022 12:42:09 +0000 (14:42 +0200)
committerMartin Pitt <martin@piware.de>
Fri, 16 Sep 2022 12:42:09 +0000 (14:42 +0200)
Use an oneshot message sender to deliver the result.

tokio-tutorial-mini-redis/src/bin/client.rs

index e7ec116f188b59a16ab249ce5bb35d8897eeba31..184170e77802e58a0da014f14d61f1a9d7b46edf 100644 (file)
@@ -1,15 +1,21 @@
 use bytes::Bytes;
 use mini_redis::client;
 use bytes::Bytes;
 use mini_redis::client;
-use tokio::sync::mpsc;
+use tokio::sync::{ mpsc, oneshot };
+
+/// Provided by the requester and used by the manager task to send
+/// the command response back to the requester.
+type Responder<T> = oneshot::Sender<mini_redis::Result<T>>;
 
 #[derive(Debug)]
 enum Command {
     Get {
         key: String,
 
 #[derive(Debug)]
 enum Command {
     Get {
         key: String,
+        resp: Responder<Option<Bytes>>,
     },
     Set {
         key: String,
         val: Bytes,
     },
     Set {
         key: String,
         val: Bytes,
+        resp: Responder<()>,
     }
 }
 
     }
 }
 
@@ -23,8 +29,8 @@ async fn main() {
 
         while let Some(cmd) = manager_rx.recv().await {
             match cmd {
 
         while let Some(cmd) = manager_rx.recv().await {
             match cmd {
-                Command::Get { key } => { client.get(&key).await.unwrap(); }
-                Command::Set { key, val } => { client.set(&key, val).await.unwrap(); }
+                Command::Get { key, resp } => { resp.send(client.get(&key).await).unwrap(); }
+                Command::Set { key, val, resp } => { resp.send(client.set(&key, val).await).unwrap(); }
             }
         }
     });
             }
         }
     });
@@ -32,11 +38,17 @@ async fn main() {
     let manager_tx2 = manager_tx.clone();
     // Spawn two tasks, one gets a key, the other sets a key
     let t1 = tokio::spawn(async move {
     let manager_tx2 = manager_tx.clone();
     // Spawn two tasks, one gets a key, the other sets a key
     let t1 = tokio::spawn(async move {
-        manager_tx.send(Command::Get { key: "hello".to_string() }).await.unwrap();
+        let (resp_tx, resp_rx) = oneshot::channel();
+        manager_tx.send(Command::Get { key: "hello".to_string(), resp: resp_tx }).await.unwrap();
+        let res = resp_rx.await;
+        println!("t1: got {:?}", res);
     });
 
     let t2 = tokio::spawn(async move {
     });
 
     let t2 = tokio::spawn(async move {
-        manager_tx2.send(Command::Set { key: "hello".to_string(), val: "bar".into() }).await.unwrap();
+        let (resp_tx, resp_rx) = oneshot::channel();
+        manager_tx2.send(Command::Set { key: "hello".to_string(), val: "bar".into(), resp: resp_tx }).await.unwrap();
+        let res = resp_rx.await.unwrap();
+        println!("t2: got {:?}", res);
     });
 
     t1.await.unwrap();
     });
 
     t1.await.unwrap();