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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use std::sync::Arc;
use async_trait::async_trait;
use futures::future::FutureExt;
use futures::Stream;
use tokio::sync::{
mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender},
oneshot,
};
use tokio_stream::wrappers::UnboundedReceiverStream;
use tonic::transport::Channel;
pub use grant::{LeaseGrantRequest, LeaseGrantResponse};
pub use keep_alive::{LeaseKeepAliveRequest, LeaseKeepAliveResponse};
pub use revoke::{LeaseRevokeRequest, LeaseRevokeResponse};
use crate::lazy::{Lazy, Shutdown};
use crate::proto::etcdserverpb;
use crate::proto::etcdserverpb::lease_client::LeaseClient;
use crate::{Error, Result};
mod grant;
mod keep_alive;
mod revoke;
struct LeaseKeepAliveTunnel {
req_sender: Option<UnboundedSender<etcdserverpb::LeaseKeepAliveRequest>>,
resp_receiver: Option<UnboundedReceiver<Result<LeaseKeepAliveResponse>>>,
shutdown: Option<oneshot::Sender<()>>,
}
impl LeaseKeepAliveTunnel {
fn new(mut client: LeaseClient<Channel>) -> Self {
let (req_sender, req_receiver) = unbounded_channel::<etcdserverpb::LeaseKeepAliveRequest>();
let (resp_sender, resp_receiver) = unbounded_channel::<Result<LeaseKeepAliveResponse>>();
let (shutdown_tx, shutdown_rx) = oneshot::channel();
let request = tonic::Request::new(UnboundedReceiverStream::new(req_receiver));
tokio::spawn(async move {
let mut shutdown_rx = shutdown_rx.fuse();
let mut inbound = futures::select! {
res = client.lease_keep_alive(request).fuse() => res.unwrap().into_inner(),
_ = shutdown_rx => { return; }
};
loop {
let resp = futures::select! {
resp = inbound.message().fuse() => resp,
_ = shutdown_rx => { return; }
};
match resp {
Ok(Some(resp)) => {
resp_sender.send(Ok(From::from(resp))).unwrap();
}
Ok(None) => {
return;
}
Err(e) => {
resp_sender.send(Err(From::from(e))).unwrap();
}
};
}
});
Self {
req_sender: Some(req_sender),
resp_receiver: Some(resp_receiver),
shutdown: Some(shutdown_tx),
}
}
}
#[async_trait]
impl Shutdown for LeaseKeepAliveTunnel {
async fn shutdown(&mut self) -> Result<()> {
self.req_sender.take().ok_or(Error::ChannelClosed)?;
self.shutdown.take().ok_or(Error::ChannelClosed)?;
Ok(())
}
}
#[derive(Clone)]
pub struct Lease {
client: LeaseClient<Channel>,
keep_alive_tunnel: Arc<Lazy<LeaseKeepAliveTunnel>>,
}
impl Lease {
pub(crate) fn new(client: LeaseClient<Channel>) -> Self {
let keep_alive_tunnel = {
let client = client.clone();
Arc::new(Lazy::new(move || LeaseKeepAliveTunnel::new(client.clone())))
};
Self {
client,
keep_alive_tunnel,
}
}
pub async fn grant(&mut self, req: LeaseGrantRequest) -> Result<LeaseGrantResponse> {
let resp = self
.client
.lease_grant(tonic::Request::new(req.into()))
.await?;
Ok(resp.into_inner().into())
}
pub async fn revoke(&mut self, req: LeaseRevokeRequest) -> Result<LeaseRevokeResponse> {
let resp = self
.client
.lease_revoke(tonic::Request::new(req.into()))
.await?;
Ok(resp.into_inner().into())
}
pub async fn keep_alive_responses(
&mut self,
) -> Result<impl Stream<Item = Result<LeaseKeepAliveResponse>>> {
self.keep_alive_tunnel
.write()
.await
.resp_receiver
.take()
.ok_or(Error::ChannelClosed)
.map(UnboundedReceiverStream::new)
}
pub async fn keep_alive(&mut self, req: LeaseKeepAliveRequest) -> Result<()> {
self.keep_alive_tunnel
.write()
.await
.req_sender
.as_mut()
.ok_or(Error::ChannelClosed)?
.send(req.into())
.map_err(|_| Error::ChannelClosed)
}
pub async fn shutdown(&mut self) -> Result<()> {
self.keep_alive_tunnel.evict().await
}
}