Skip to main content

tonic/service/
layered.rs

1/*
2 *
3 * Copyright 2025 gRPC authors.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to
7 * deal in the Software without restriction, including without limitation the
8 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 * sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24
25use std::{
26    marker::PhantomData,
27    task::{Context, Poll},
28};
29
30use tower_layer::Layer;
31use tower_service::Service;
32
33use crate::server::NamedService;
34
35/// A layered service to propagate [`NamedService`] implementation.
36#[derive(Debug, Clone)]
37pub struct Layered<S, T> {
38    inner: S,
39    _ty: PhantomData<T>,
40}
41
42impl<S, T: NamedService> NamedService for Layered<S, T> {
43    const NAME: &'static str = T::NAME;
44}
45
46impl<Req, S, T> Service<Req> for Layered<S, T>
47where
48    S: Service<Req>,
49{
50    type Response = S::Response;
51    type Error = S::Error;
52    type Future = S::Future;
53
54    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
55        self.inner.poll_ready(cx)
56    }
57
58    fn call(&mut self, req: Req) -> Self::Future {
59        self.inner.call(req)
60    }
61}
62
63/// Extension trait which adds utility methods to types which implement [`tower_layer::Layer`].
64pub trait LayerExt<L>: sealed::Sealed {
65    /// Applies the layer to a service and wraps it in [`Layered`].
66    fn named_layer<S>(&self, service: S) -> Layered<L::Service, S>
67    where
68        L: Layer<S>;
69}
70
71impl<L> LayerExt<L> for L {
72    fn named_layer<S>(&self, service: S) -> Layered<<L>::Service, S>
73    where
74        L: Layer<S>,
75    {
76        Layered {
77            inner: self.layer(service),
78            _ty: PhantomData,
79        }
80    }
81}
82
83mod sealed {
84    pub trait Sealed {}
85    impl<T> Sealed for T {}
86}
87
88#[cfg(test)]
89mod tests {
90    use super::*;
91
92    #[derive(Debug, Default)]
93    struct TestService {}
94
95    const TEST_SERVICE_NAME: &str = "test-service-name";
96
97    impl NamedService for TestService {
98        const NAME: &'static str = TEST_SERVICE_NAME;
99    }
100
101    // Checks if the argument implements `NamedService` and returns the implemented `NAME`.
102    fn get_name_of_named_service<S: NamedService>(_s: &S) -> &'static str {
103        S::NAME
104    }
105
106    #[test]
107    fn named_service_is_propagated_to_layered() {
108        use std::time::Duration;
109        use tower::{limit::ConcurrencyLimitLayer, timeout::TimeoutLayer};
110
111        let layered = TimeoutLayer::new(Duration::from_secs(5)).named_layer(TestService::default());
112        assert_eq!(get_name_of_named_service(&layered), TEST_SERVICE_NAME);
113
114        let layered = ConcurrencyLimitLayer::new(3).named_layer(layered);
115        assert_eq!(get_name_of_named_service(&layered), TEST_SERVICE_NAME);
116    }
117}