nacos-sdk 0.7.0

Nacos client in Rust.
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
use std::pin::Pin;
use std::sync::Arc;
use std::task::{Context, Poll};

use futures::{Future, Stream};

use async_trait::async_trait;
use tokio::sync::oneshot::Sender;
use tower::{Layer, Service};
use want::Giver;

use crate::api::error::Error::ErrResult;
use crate::{api::error::Error, nacos_proto::v2::Payload};

pub(crate) enum NacosGrpcCall {
    RequestService((Payload, Callback<Result<Payload, Error>>)),

    BIRequestService(
        (
            GrpcStream<Payload>,
            Callback<Result<GrpcStream<Result<Payload, Error>>, Error>>,
        ),
    ),
}

pub(crate) struct GrpcStream<T> {
    inner: Pin<Box<dyn Stream<Item = T> + Send + 'static>>,
}

impl<T> GrpcStream<T> {
    pub(crate) fn new(inner: Pin<Box<dyn Stream<Item = T> + Send + 'static>>) -> Self {
        Self { inner }
    }
}

impl<T> Stream for GrpcStream<T> {
    type Item = T;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let pin = self.get_mut().inner.as_mut();
        pin.poll_next(cx)
    }
}

pub(crate) struct Callback<T> {
    gv: Giver,
    tx: Option<Sender<T>>,
}

impl<T> Callback<T> {
    pub(crate) fn new(gv: Giver, tx: Sender<T>) -> Self {
        Self { gv, tx: Some(tx) }
    }

    pub(crate) async fn can_send(&mut self) -> bool {
        if let Err(_closed) = self.gv.want().await {
            return false;
        }
        true
    }

    pub(crate) async fn send(&mut self, data: T) -> Result<(), Error> {
        let sender = self.tx.take();
        if let Some(sender) = sender
            && sender.send(data).is_err()
        {
            return Err(ErrResult(
                "callback send failed. the receiver has been dropped".to_string(),
            ));
        }
        Ok(())
    }
}

#[async_trait]
pub(crate) trait ServerRequestHandler: Send + Sync {
    async fn request_reply(&self, request: Payload) -> Option<Payload>;
}

pub(crate) type DynamicUnaryCallService = Box<
    dyn Service<
            Payload,
            Error = Error,
            Response = Payload,
            Future = Pin<Box<dyn Future<Output = Result<Payload, Error>> + Send + 'static>>,
        > + Sync
        + Send,
>;

pub(crate) type DynamicUnaryCallLayer =
    Arc<dyn Layer<DynamicUnaryCallService, Service = DynamicUnaryCallService> + Sync + Send>;

pub(crate) struct DynamicUnaryCallLayerWrapper(pub(crate) DynamicUnaryCallLayer);

impl Layer<DynamicUnaryCallService> for DynamicUnaryCallLayerWrapper {
    type Service = DynamicUnaryCallService;

    fn layer(&self, inner: DynamicUnaryCallService) -> Self::Service {
        self.0.layer(inner)
    }
}

pub(crate) struct UnaryCallIdentityLayer;
impl Layer<DynamicUnaryCallService> for UnaryCallIdentityLayer {
    type Service = DynamicUnaryCallService;

    fn layer(&self, inner: DynamicUnaryCallService) -> Self::Service {
        inner
    }
}

pub(crate) type DynamicBiStreamingCallService = Box<
    dyn Service<
            GrpcStream<Payload>,
            Error = Error,
            Response = GrpcStream<Result<Payload, Error>>,
            Future = Pin<
                Box<
                    dyn Future<Output = Result<GrpcStream<Result<Payload, Error>>, Error>>
                        + Send
                        + 'static,
                >,
            >,
        > + Sync
        + Send
        + 'static,
>;

pub(crate) type DynamicBiStreamingCallLayer = Arc<
    dyn Layer<DynamicBiStreamingCallService, Service = DynamicBiStreamingCallService> + Sync + Send,
>;

#[cfg(test)]
#[allow(clippy::disallowed_methods)] // Tests mock! has std::result::Result::unwrap
mod nacos_grpc_mock_tests {
    use super::*;

    mockall::mock! {
        pub(crate) DynamicUnaryCallService {}

        impl Service<Payload> for DynamicUnaryCallService {
            type Response = Payload;

            type Error = Error;

            type Future =
                Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;

            fn poll_ready<'a>(&mut self, cx: &mut Context<'a>) -> Poll<Result<(), <Self as Service<Payload>>::Error>>;

            fn call(&mut self, req: Payload) -> <Self as Service<Payload>>::Future;
        }
    }

    mockall::mock! {
        pub(crate) DynamicUnaryCallLayer{}

        impl Layer<MockDynamicUnaryCallService> for DynamicUnaryCallLayer {
            type Service = MockDynamicUnaryCallService;

            fn layer(&self, inner: MockDynamicUnaryCallService) -> <Self as Layer<MockDynamicUnaryCallService>>::Service;
        }
    }

    mockall::mock! {
        pub(crate) DynamicBiStreamingCallService {}

        impl Service<GrpcStream<Payload>> for DynamicBiStreamingCallService {
            type Response = GrpcStream<Result<Payload, Error>>;

            type Error = Error;

            type Future =
                Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;

            fn poll_ready<'a>(&mut self, cx: &mut Context<'a>) -> Poll<Result<(), <Self as Service<GrpcStream<Payload>>>::Error>>;

            fn call(&mut self, req: GrpcStream<Payload>) -> <Self as Service<GrpcStream<Payload>>>::Future;
        }
    }

    mockall::mock! {
        pub(crate) DynamicBiStreamingCallLayer {}

        impl Layer<MockDynamicBiStreamingCallService> for DynamicBiStreamingCallLayer {
            type Service = MockDynamicBiStreamingCallService;

            fn layer(&self, inner: MockDynamicBiStreamingCallService) -> <Self as Layer<MockDynamicBiStreamingCallService>>::Service;
        }
    }
}

pub(crate) struct DynamicBiStreamingCallLayerWrapper(pub(crate) DynamicBiStreamingCallLayer);

impl Layer<DynamicBiStreamingCallService> for DynamicBiStreamingCallLayerWrapper {
    type Service = DynamicBiStreamingCallService;

    fn layer(&self, inner: DynamicBiStreamingCallService) -> Self::Service {
        self.0.layer(inner)
    }
}

pub(crate) struct BiStreamingCallIdentityLayer;
impl Layer<DynamicBiStreamingCallService> for BiStreamingCallIdentityLayer {
    type Service = DynamicBiStreamingCallService;

    fn layer(&self, inner: DynamicBiStreamingCallService) -> Self::Service {
        inner
    }
}

#[cfg(test)]
pub mod unary_call_layer_test {
    use std::{pin::Pin, sync::Arc, task::Poll};

    use futures::{Future, future::poll_fn};
    use tower::{Layer, Service, layer::util::Stack};
    use tracing::debug;

    use crate::{
        api::error::Error,
        common::remote::grpc::nacos_grpc_service::DynamicUnaryCallLayerWrapper,
        nacos_proto::v2::{Metadata, Payload},
        test_config,
    };

    use super::{DynamicUnaryCallLayer, DynamicUnaryCallService, UnaryCallIdentityLayer};

    struct DynamicUnaryCallServiceA {
        inner: DynamicUnaryCallService,
    }

    impl Service<Payload> for DynamicUnaryCallServiceA {
        type Response = Payload;

        type Error = Error;

        type Future = Pin<Box<dyn Future<Output = Result<Payload, Error>> + Send + 'static>>;

        fn poll_ready(
            &mut self,
            cx: &mut std::task::Context<'_>,
        ) -> std::task::Poll<Result<(), Self::Error>> {
            debug!("DynamicUnaryCallServiceA");
            self.inner.poll_ready(cx)
        }

        fn call(&mut self, mut req: Payload) -> Self::Future {
            let mut metadata = req
                .metadata
                .take()
                .expect("Metadata should be present in request");
            metadata
                .headers
                .insert("DynamicUnaryCallServiceA".to_string(), "ok".to_string());
            req.metadata = Some(metadata);
            self.inner.call(req)
        }
    }

    struct UnaryCallLayerA;

    impl Layer<DynamicUnaryCallService> for UnaryCallLayerA {
        type Service = DynamicUnaryCallService;

        fn layer(&self, inner: DynamicUnaryCallService) -> Self::Service {
            debug!("UnaryCallLayerA");
            Box::new(DynamicUnaryCallServiceA { inner })
        }
    }

    struct DynamicUnaryCallServiceB {
        inner: DynamicUnaryCallService,
    }

    impl Service<Payload> for DynamicUnaryCallServiceB {
        type Response = Payload;

        type Error = Error;

        type Future = Pin<Box<dyn Future<Output = Result<Payload, Error>> + Send + 'static>>;

        fn poll_ready(
            &mut self,
            cx: &mut std::task::Context<'_>,
        ) -> std::task::Poll<Result<(), Self::Error>> {
            debug!("DynamicUnaryCallServiceB");
            self.inner.poll_ready(cx)
        }

        fn call(&mut self, mut req: Payload) -> Self::Future {
            let mut metadata = req
                .metadata
                .take()
                .expect("Metadata should be present in request");
            metadata
                .headers
                .insert("DynamicUnaryCallServiceB".to_string(), "ok".to_string());
            req.metadata = Some(metadata);
            self.inner.call(req)
        }
    }

    struct UnaryCallLayerB;

    impl Layer<DynamicUnaryCallService> for UnaryCallLayerB {
        type Service = DynamicUnaryCallService;

        fn layer(&self, inner: DynamicUnaryCallService) -> Self::Service {
            debug!("UnaryCallLayerB");
            Box::new(DynamicUnaryCallServiceB { inner })
        }
    }
    struct DynamicUnaryCallServiceC {
        inner: DynamicUnaryCallService,
    }

    impl Service<Payload> for DynamicUnaryCallServiceC {
        type Response = Payload;

        type Error = Error;

        type Future = Pin<Box<dyn Future<Output = Result<Payload, Error>> + Send + 'static>>;

        fn poll_ready(
            &mut self,
            cx: &mut std::task::Context<'_>,
        ) -> std::task::Poll<Result<(), Self::Error>> {
            debug!("DynamicUnaryCallServiceC");
            self.inner.poll_ready(cx)
        }

        fn call(&mut self, mut req: Payload) -> Self::Future {
            let mut metadata = req
                .metadata
                .take()
                .expect("Metadata should be present in request");
            metadata
                .headers
                .insert("DynamicUnaryCallServiceC".to_string(), "ok".to_string());
            req.metadata = Some(metadata);
            self.inner.call(req)
        }
    }

    struct UnaryCallLayerC;

    impl Layer<DynamicUnaryCallService> for UnaryCallLayerC {
        type Service = DynamicUnaryCallService;

        fn layer(&self, inner: DynamicUnaryCallService) -> Self::Service {
            debug!("UnaryCallLayerC");
            Box::new(DynamicUnaryCallServiceC { inner })
        }
    }

    struct RealDynamicUnaryCallService;

    impl Service<Payload> for RealDynamicUnaryCallService {
        type Response = Payload;

        type Error = Error;

        type Future = Pin<Box<dyn Future<Output = Result<Payload, Error>> + Send + 'static>>;

        fn poll_ready(
            &mut self,
            _: &mut std::task::Context<'_>,
        ) -> std::task::Poll<Result<(), Self::Error>> {
            debug!("RealDynamicUnaryCallService");
            Poll::Ready(Ok(()))
        }

        fn call(&mut self, mut req: Payload) -> Self::Future {
            let mut metadata = req
                .metadata
                .take()
                .expect("Metadata should be present in request");
            metadata
                .headers
                .insert("RealDynamicUnaryCallService".to_string(), "ok".to_string());
            req.metadata = Some(metadata);

            let fut = async move { Ok(req) };

            Box::pin(fut)
        }
    }

    struct UnaryCallLayerBuilder {
        layer: DynamicUnaryCallLayer,
    }

    impl UnaryCallLayerBuilder {
        fn new() -> Self {
            Self {
                layer: Arc::new(UnaryCallIdentityLayer),
            }
        }

        fn add_layer(self, layer: DynamicUnaryCallLayer) -> Self {
            let stack = Arc::new(Stack::new(
                DynamicUnaryCallLayerWrapper(layer),
                DynamicUnaryCallLayerWrapper(self.layer),
            ));
            Self { layer: stack }
        }

        fn build(self) -> DynamicUnaryCallLayer {
            self.layer
        }
    }

    fn setup() {
        test_config::setup_log();
    }

    fn teardown() {}

    fn run_test<T, F>(test: F) -> T
    where
        F: FnOnce() -> T,
    {
        setup();
        let ret = test();
        teardown();
        ret
    }

    #[tokio::test]
    pub async fn test() {
        run_test(|| async {
            let builder = UnaryCallLayerBuilder::new()
                .add_layer(Arc::new(UnaryCallLayerA))
                .add_layer(Arc::new(UnaryCallLayerB))
                .add_layer(Arc::new(UnaryCallLayerC));
            let layer = builder.build();

            let mut service = layer.layer(Box::new(RealDynamicUnaryCallService));

            let mut payload = Payload::default();

            let mut metadata = Metadata::default();
            metadata
                .headers
                .insert("init".to_string(), "ok".to_string());

            payload.metadata = Some(metadata);

            let ready = poll_fn(|cx| service.poll_ready(cx)).await;
            assert!(ready.is_ok());

            let ret = service.call(payload).await;

            assert!(ret.is_ok());

            let mut ret = ret.expect("Service call should have succeeded");
            let metadata = ret.metadata.take();

            assert!(metadata.is_some());

            let metadata = metadata.expect("Metadata should be present in response");

            let init = metadata
                .headers
                .get("init")
                .expect("init header should be present");
            assert!(init.eq("ok"));

            let a = metadata
                .headers
                .get("DynamicUnaryCallServiceA")
                .expect("DynamicUnaryCallServiceA header should be present");
            assert!(a.eq("ok"));

            let b = metadata
                .headers
                .get("DynamicUnaryCallServiceB")
                .expect("DynamicUnaryCallServiceB header should be present");
            assert!(b.eq("ok"));

            let c = metadata
                .headers
                .get("DynamicUnaryCallServiceC")
                .expect("DynamicUnaryCallServiceC header should be present");
            assert!(c.eq("ok"));

            let d = metadata
                .headers
                .get("RealDynamicUnaryCallService")
                .expect("RealDynamicUnaryCallService header should be present");
            assert!(d.eq("ok"));
        })
        .await;
    }
}