-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.rs
85 lines (71 loc) · 2.21 KB
/
client.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use bytes::Bytes;
use mini_redis::client;
use tokio::sync::{mpsc, oneshot};
#[derive(Debug)]
enum Command {
Get {
key: String,
resp: Responder<Option<Bytes>>,
},
Set {
key: String,
val: Bytes,
resp: Responder<()>,
},
}
/// 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>>;
#[tokio::main]
pub async fn main() {
// Create a new channel with a capacity of at most 32.
let (tx, mut rx) = mpsc::channel(32);
let tx2 = tx.clone();
// The `move` keyword is used to **move** ownership of `rx` into the task.
let manager = tokio::spawn(async move {
// Establish a connection to the server.
let mut client = client::connect("127.0.0.1:6379").await.unwrap();
while let Some(cmd) = rx.recv().await {
use Command::*;
match cmd {
Get { key, resp } => {
let res = client.get(&key).await;
// Ignore errors.
let _ = resp.send(res);
}
Set { key, val, resp } => {
let res = client.set(&key, val).await;
// Ignore errors.
let _ = resp.send(res);
}
}
}
});
// Spawn two tasks, one gets a key, the other sets a key
let t1 = tokio::spawn(async move {
let (resp_tx, resp_rx) = oneshot::channel();
let cmd = Command::Get {
key: "name".to_string(),
resp: resp_tx,
};
tx.send(cmd).await.unwrap();
// Await the response
let res = resp_rx.await;
println!("GOT = {:?}", res);
});
let t2 = tokio::spawn(async move {
let (resp_tx, resp_rx) = oneshot::channel();
let cmd = Command::Set {
key: "name".to_string(),
val: "Mini".into(),
resp: resp_tx,
};
tx2.send(cmd).await.unwrap();
// Await the response
let res = resp_rx.await;
println!("GOT = {:?}", res);
});
t2.await.unwrap();
t1.await.unwrap();
manager.await.unwrap();
}