etcdv3client 0.4.0

a simple etcdv3 client
Documentation
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
use std::fmt;
use std::future::{Future, IntoFuture};
use std::pin::Pin;

use tokio::sync::mpsc::{channel, Sender};
use tonic::codec::Streaming;
use tonic::IntoRequest;
use tonic::IntoStreamingRequest;

use crate::error::{ErrKind, Error, Result};
use crate::grpc::GrpcService;
use crate::pb;

const MPSC_CHANNEL_SIZE: usize = 1;

#[derive(Debug, Clone)]
pub struct InnerLeaseClient<S> {
    service: S,
}
impl<S> InnerLeaseClient<S>
where
    S: GrpcService,
{
    pub fn new(service: S) -> Self {
        Self { service }
    }
    pub async fn lease_grant(
        &mut self,
        request: impl tonic::IntoRequest<pb::LeaseGrantRequest>,
    ) -> Result<tonic::Response<pb::LeaseGrantResponse>> {
        let path = http::uri::PathAndQuery::from_static("/etcdserverpb.Lease/LeaseGrant");
        self.service.unary(request.into_request(), path).await
    }
    pub async fn lease_revoke(
        &mut self,
        request: impl tonic::IntoRequest<pb::LeaseRevokeRequest>,
    ) -> Result<tonic::Response<pb::LeaseRevokeResponse>> {
        let path = http::uri::PathAndQuery::from_static("/etcdserverpb.Lease/LeaseRevoke");
        self.service.unary(request.into_request(), path).await
    }
    pub async fn lease_keep_alive(
        &mut self,
        request: impl tonic::IntoStreamingRequest<Message = pb::LeaseKeepAliveRequest>,
    ) -> Result<tonic::Response<tonic::codec::Streaming<pb::LeaseKeepAliveResponse>>> {
        let path = http::uri::PathAndQuery::from_static("/etcdserverpb.Lease/LeaseKeepAlive");
        self.service
            .streaming(request.into_streaming_request(), path)
            .await
    }
    pub async fn lease_time_to_live(
        &mut self,
        request: impl tonic::IntoRequest<pb::LeaseTimeToLiveRequest>,
    ) -> Result<tonic::Response<pb::LeaseTimeToLiveResponse>> {
        let path = http::uri::PathAndQuery::from_static("/etcdserverpb.Lease/LeaseTimeToLive");
        self.service.unary(request.into_request(), path).await
    }
    pub async fn lease_leases(
        &mut self,
        request: impl tonic::IntoRequest<pb::LeaseLeasesRequest>,
    ) -> Result<tonic::Response<pb::LeaseLeasesResponse>> {
        let path = http::uri::PathAndQuery::from_static("/etcdserverpb.Lease/LeaseLeases");
        self.service.unary(request.into_request(), path).await
    }
}
#[derive(Debug, Clone)]
pub struct LeaseClient<S> {
    inner: InnerLeaseClient<S>,
}
impl<S> LeaseClient<S>
where
    S: GrpcService,
{
    pub async fn lease_grant(
        &mut self,
        request: pb::LeaseGrantRequest,
    ) -> Result<pb::LeaseGrantResponse> {
        self.inner
            .lease_grant(request.into_request())
            .await
            .map(|rsp| rsp.into_inner())
    }
    pub async fn lease_revoke(
        &mut self,
        request: pb::LeaseRevokeRequest,
    ) -> Result<pb::LeaseRevokeResponse> {
        self.inner
            .lease_revoke(request.into_request())
            .await
            .map(|rsp| rsp.into_inner())
    }
    pub async fn lease_keep_alive(
        &mut self,
        request: impl tonic::IntoStreamingRequest<Message = pb::LeaseKeepAliveRequest>,
    ) -> Result<tonic::codec::Streaming<pb::LeaseKeepAliveResponse>> {
        self.inner
            .lease_keep_alive(request.into_streaming_request())
            .await
            .map(|rsp| rsp.into_inner())
    }
    pub async fn lease_time_to_live(
        &mut self,
        request: pb::LeaseTimeToLiveRequest,
    ) -> Result<pb::LeaseTimeToLiveResponse> {
        self.inner
            .lease_time_to_live(request.into_request())
            .await
            .map(|rsp| rsp.into_inner())
    }
    pub async fn lease_leases(
        &mut self,
        request: pb::LeaseLeasesRequest,
    ) -> Result<pb::LeaseLeasesResponse> {
        self.inner
            .lease_leases(request.into_request())
            .await
            .map(|rsp| rsp.into_inner())
    }
}

impl<S> LeaseClient<S>
where
    S: GrpcService,
{
    pub(crate) fn new(service: S) -> Self {
        LeaseClient {
            inner: InnerLeaseClient::new(service),
        }
    }

    pub fn do_grant(&mut self, ttl: i64) -> DoLeaseGrantRequest<S> {
        pb::LeaseGrantRequest::new(ttl, 0).build(self)
    }

    /// Grant a lease with the given ttl.
    pub async fn grant(&mut self, ttl: i64) -> Result<pb::LeaseGrantResponse> {
        self.do_grant(ttl).await
    }

    /// Grant a lease with the given ttl and lease id.
    pub async fn grant_with_lease_id(
        &mut self,
        ttl: i64,
        lease_id: i64,
    ) -> Result<pb::LeaseGrantResponse> {
        self.do_grant(ttl).with_id(lease_id).await
    }

    /// Revoke the given lease.
    pub async fn revoke(&mut self, lease_id: i64) -> Result<()> {
        let request = pb::LeaseRevokeRequest::new(lease_id);
        self.lease_revoke(request).await.map(|_| ())
    }

    /// Keep the lease alive.
    #[must_use]
    pub fn do_keep_alive(&mut self, lease_id: i64) -> DoLeaseKeepAlive<S> {
        DoLeaseKeepAlive::new(lease_id, self)
    }

    /// Keep the lease alive.
    /// When call, it sent the first keep alive message and return LeaseKeepAliver for further call.
    pub async fn keep_alive(&mut self, lease_id: i64) -> Result<LeaseKeepAliver> {
        self.do_keep_alive(lease_id).await
    }

    pub async fn get_lease_info(
        &mut self,
        lease_id: i64,
        keys: bool,
    ) -> Result<pb::LeaseTimeToLiveResponse> {
        self.lease_time_to_live(pb::LeaseTimeToLiveRequest::new(lease_id, keys))
            .await
    }

    pub async fn list(&mut self) -> Result<pb::LeaseLeasesResponse> {
        self.lease_leases(pb::LeaseLeasesRequest::new()).await
    }
}

impl pb::LeaseGrantRequest {
    fn new(ttl: i64, id: i64) -> Self {
        pb::LeaseGrantRequest { ttl, id }
    }

    pub fn build<S: GrpcService>(self, client: &mut LeaseClient<S>) -> DoLeaseGrantRequest<'_, S> {
        DoLeaseGrantRequest {
            request: self,
            client,
        }
    }
}

#[must_use]
pub struct DoLeaseGrantRequest<'a, S> {
    pub request: pb::LeaseGrantRequest,
    pub(crate) client: &'a mut LeaseClient<S>,
}
impl<'a, S> DoLeaseGrantRequest<'a, S>
where
    S: GrpcService,
{
    pub fn with_client(mut self, client: &'a mut LeaseClient<S>) -> Self {
        self.client = client;
        self
    }
    pub fn with_ttl(mut self, ttl: i64) -> Self {
        self.request.ttl = ttl;
        self
    }
    pub fn with_id(mut self, id: i64) -> Self {
        self.request.id = id;
        self
    }
}
impl<'a, S> std::future::IntoFuture for DoLeaseGrantRequest<'a, S>
where
    S: GrpcService,
{
    type Output = Result<pb::LeaseGrantResponse>;
    type IntoFuture = std::pin::Pin<
        Box<dyn std::future::Future<Output = crate::error::Result<pb::LeaseGrantResponse>> + 'a>,
    >;
    fn into_future(self) -> Self::IntoFuture {
        let DoLeaseGrantRequest { request, client } = self;
        Box::pin(async move { client.lease_grant(request).await })
    }
}

impl pb::LeaseRevokeRequest {
    fn new(id: i64) -> Self {
        pb::LeaseRevokeRequest { id }
    }

    pub fn build<S: GrpcService>(self, client: &mut LeaseClient<S>) -> DoLeaseRevokeRequest<'_, S> {
        DoLeaseRevokeRequest {
            request: self,
            client,
        }
    }
}

#[must_use]
pub struct DoLeaseRevokeRequest<'a, S> {
    pub request: pb::LeaseRevokeRequest,
    pub(crate) client: &'a mut LeaseClient<S>,
}
impl<'a, S> DoLeaseRevokeRequest<'a, S>
where
    S: GrpcService,
{
    pub fn with_client(mut self, client: &'a mut LeaseClient<S>) -> Self {
        self.client = client;
        self
    }
    pub fn with_id(mut self, id: i64) -> Self {
        self.request.id = id;
        self
    }
}
impl<'a, S> std::future::IntoFuture for DoLeaseRevokeRequest<'a, S>
where
    S: GrpcService,
{
    type Output = Result<pb::LeaseRevokeResponse>;
    type IntoFuture = std::pin::Pin<
        Box<dyn std::future::Future<Output = crate::error::Result<pb::LeaseRevokeResponse>> + 'a>,
    >;
    fn into_future(self) -> Self::IntoFuture {
        let DoLeaseRevokeRequest { request, client } = self;
        Box::pin(async move { client.lease_revoke(request).await })
    }
}

impl pb::LeaseKeepAliveRequest {
    pub fn new(lease_id: i64) -> Self {
        pb::LeaseKeepAliveRequest { id: lease_id }
    }
}

impl pb::LeaseTimeToLiveRequest {
    pub fn build<S: GrpcService>(
        self,
        client: &mut LeaseClient<S>,
    ) -> DoLeaseTimeToLiveRequest<'_, S> {
        DoLeaseTimeToLiveRequest {
            request: self,
            client,
        }
    }
}

#[must_use]
pub struct DoLeaseTimeToLiveRequest<'a, S> {
    pub request: pb::LeaseTimeToLiveRequest,
    pub(crate) client: &'a mut LeaseClient<S>,
}
impl<'a, S> DoLeaseTimeToLiveRequest<'a, S>
where
    S: GrpcService,
{
    pub fn with_client(mut self, client: &'a mut LeaseClient<S>) -> Self {
        self.client = client;
        self
    }
    pub fn with_id(mut self, id: i64) -> Self {
        self.request.id = id;
        self
    }
    pub fn with_keys(mut self, keys: bool) -> Self {
        self.request.keys = keys;
        self
    }
}

impl<'a, S> std::future::IntoFuture for DoLeaseTimeToLiveRequest<'a, S>
where
    S: GrpcService,
{
    type Output = Result<pb::LeaseTimeToLiveResponse>;
    type IntoFuture = std::pin::Pin<
        Box<
            dyn std::future::Future<Output = crate::error::Result<pb::LeaseTimeToLiveResponse>>
                + 'a,
        >,
    >;
    fn into_future(self) -> Self::IntoFuture {
        let DoLeaseTimeToLiveRequest { request, client } = self;
        Box::pin(async move { client.lease_time_to_live(request).await })
    }
}

impl pb::LeaseLeasesRequest {
    pub fn build<S: GrpcService>(self, client: &mut LeaseClient<S>) -> DoLeaseLeasesRequest<'_, S> {
        DoLeaseLeasesRequest {
            request: self,
            client,
        }
    }
}
#[must_use]
pub struct DoLeaseLeasesRequest<'a, S> {
    pub request: pb::LeaseLeasesRequest,
    pub(crate) client: &'a mut LeaseClient<S>,
}
impl<'a, S> DoLeaseLeasesRequest<'a, S>
where
    S: GrpcService,
{
    pub fn with_client(mut self, client: &'a mut LeaseClient<S>) -> Self {
        self.client = client;
        self
    }
}
impl<'a, S> std::future::IntoFuture for DoLeaseLeasesRequest<'a, S>
where
    S: GrpcService,
{
    type Output = Result<pb::LeaseLeasesResponse>;
    type IntoFuture = std::pin::Pin<
        Box<dyn std::future::Future<Output = crate::error::Result<pb::LeaseLeasesResponse>> + 'a>,
    >;
    fn into_future(self) -> Self::IntoFuture {
        let DoLeaseLeasesRequest { request, client } = self;
        Box::pin(async move { client.lease_leases(request).await })
    }
}

pub struct DoLeaseKeepAlive<'a, S> {
    pub lease_id: i64,
    client: &'a mut LeaseClient<S>,
}

impl<'a, S> DoLeaseKeepAlive<'a, S>
where
    S: GrpcService,
{
    pub fn new(lease_id: i64, client: &'a mut LeaseClient<S>) -> Self {
        DoLeaseKeepAlive { lease_id, client }
    }

    async fn send(self) -> Result<LeaseKeepAliver> {
        let DoLeaseKeepAlive { lease_id, client } = self;

        let (req_tx, req_rx) = channel::<pb::LeaseKeepAliveRequest>(MPSC_CHANNEL_SIZE);

        let request = pb::LeaseKeepAliveRequest::new(lease_id);
        req_tx
            .send(request)
            .await
            .map_err(|err| Error::new(ErrKind::LeaseRequestFailed, err))?;

        let rx = tokio_stream::wrappers::ReceiverStream::new(req_rx);
        let resp = client.lease_keep_alive(rx.into_streaming_request()).await?;

        Ok(LeaseKeepAliver::new(lease_id, req_tx, resp))
    }
}

impl<'a, S> fmt::Debug for DoLeaseKeepAlive<'a, S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("DoLeaseKeepAlive")
            .field("lease_id", &self.lease_id)
            .finish()
    }
}

impl<'a, S> IntoFuture for DoLeaseKeepAlive<'a, S>
where
    S: GrpcService,
{
    type Output = Result<LeaseKeepAliver>;
    type IntoFuture = Pin<Box<dyn Future<Output = Result<LeaseKeepAliver>> + 'a>>;

    fn into_future(self) -> Self::IntoFuture {
        Box::pin(self.send())
    }
}

pub struct LeaseKeepAliver {
    lease_id: i64,
    req_tx: Sender<pb::LeaseKeepAliveRequest>,
    inbound: Streaming<crate::pb::LeaseKeepAliveResponse>,
}

impl LeaseKeepAliver {
    pub fn new(
        lease_id: i64,
        req_tx: Sender<pb::LeaseKeepAliveRequest>,
        inbound: Streaming<crate::pb::LeaseKeepAliveResponse>,
    ) -> Self {
        LeaseKeepAliver {
            lease_id,
            req_tx,
            inbound,
        }
    }

    pub async fn keep_alive(&mut self) -> Result<()> {
        let request = pb::LeaseKeepAliveRequest::new(self.lease_id);

        self.req_tx
            .send(request)
            .await
            .map_err(|err| Error::new(ErrKind::LeaseRequestFailed, err))?;

        Ok(())
    }

    pub async fn message(&mut self) -> Result<Option<pb::LeaseKeepAliveResponse>> {
        match self.inbound.message().await? {
            Some(resp) => Ok(Some(resp)),
            None => Ok(None),
        }
    }
}

impl fmt::Debug for LeaseKeepAliver {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("LeaseKeepAliver")
            .field("lease_id", &self.lease_id)
            .finish()
    }
}

impl pb::LeaseTimeToLiveRequest {
    pub fn new(lease_id: i64, keys: bool) -> Self {
        pb::LeaseTimeToLiveRequest { id: lease_id, keys }
    }
}

impl pb::LeaseLeasesRequest {
    fn new() -> Self {
        pb::LeaseLeasesRequest {}
    }
}