1 | use crate::util::BoxCloneService; |
2 | use std::{fmt, sync::Arc}; |
3 | use tower_layer::{layer_fn, Layer}; |
4 | use tower_service::Service; |
5 | |
6 | /// A [`Clone`] + [`Send`] boxed [`Layer`]. |
7 | /// |
8 | /// [`BoxCloneServiceLayer`] turns a layer into a trait object, allowing both the [`Layer`] itself |
9 | /// and the output [`Service`] to be dynamic, while having consistent types. |
10 | /// |
11 | /// This [`Layer`] produces [`BoxCloneService`] instances erasing the type of the |
12 | /// [`Service`] produced by the wrapped [`Layer`]. |
13 | /// |
14 | /// This is similar to [`BoxLayer`](super::BoxLayer) except the layer and resulting |
15 | /// service implements [`Clone`]. |
16 | /// |
17 | /// # Example |
18 | /// |
19 | /// `BoxCloneServiceLayer` can, for example, be useful to create layers dynamically that otherwise wouldn't have |
20 | /// the same types, when the underlying service must be clone (for example, when building a MakeService) |
21 | /// In this example, we include a [`Timeout`] layer only if an environment variable is set. We can use |
22 | /// `BoxCloneService` to return a consistent type regardless of runtime configuration: |
23 | /// |
24 | /// ``` |
25 | /// use std::time::Duration; |
26 | /// use tower::{Service, ServiceBuilder, BoxError}; |
27 | /// use tower::util::{BoxCloneServiceLayer, BoxCloneService}; |
28 | /// |
29 | /// # |
30 | /// # struct Request; |
31 | /// # struct Response; |
32 | /// # impl Response { |
33 | /// # fn new() -> Self { Self } |
34 | /// # } |
35 | /// |
36 | /// fn common_layer<S, T>() -> BoxCloneServiceLayer<S, T, S::Response, BoxError> |
37 | /// where |
38 | /// S: Service<T> + Clone + Send + 'static, |
39 | /// S::Future: Send + 'static, |
40 | /// S::Error: Into<BoxError> + 'static, |
41 | /// { |
42 | /// let builder = ServiceBuilder::new() |
43 | /// .concurrency_limit(100); |
44 | /// |
45 | /// if std::env::var("SET_TIMEOUT" ).is_ok() { |
46 | /// let layer = builder |
47 | /// .timeout(Duration::from_secs(30)) |
48 | /// .into_inner(); |
49 | /// |
50 | /// BoxCloneServiceLayer::new(layer) |
51 | /// } else { |
52 | /// let layer = builder |
53 | /// .map_err(Into::into) |
54 | /// .into_inner(); |
55 | /// |
56 | /// BoxCloneServiceLayer::new(layer) |
57 | /// } |
58 | /// } |
59 | /// |
60 | /// // We can clone the layer (this is true of BoxLayer as well) |
61 | /// let boxed_clone_layer = common_layer(); |
62 | /// |
63 | /// let cloned_layer = boxed_clone_layer.clone(); |
64 | /// |
65 | /// // Using the `BoxCloneServiceLayer` we can create a `BoxCloneService` |
66 | /// let service: BoxCloneService<Request, Response, BoxError> = ServiceBuilder::new().layer(boxed_clone_layer) |
67 | /// .service_fn(|req: Request| async { |
68 | /// Ok::<_, BoxError>(Response::new()) |
69 | /// }); |
70 | /// |
71 | /// # let service = assert_service(service); |
72 | /// |
73 | /// // And we can still clone the service |
74 | /// let cloned_service = service.clone(); |
75 | /// # |
76 | /// # fn assert_service<S, R>(svc: S) -> S |
77 | /// # where S: Service<R> { svc } |
78 | /// |
79 | /// ``` |
80 | /// |
81 | /// [`Layer`]: tower_layer::Layer |
82 | /// [`Service`]: tower_service::Service |
83 | /// [`BoxService`]: super::BoxService |
84 | /// [`Timeout`]: crate::timeout |
85 | pub struct BoxCloneServiceLayer<In, T, U, E> { |
86 | boxed: Arc<dyn Layer<In, Service = BoxCloneService<T, U, E>> + Send + Sync + 'static>, |
87 | } |
88 | |
89 | impl<In, T, U, E> BoxCloneServiceLayer<In, T, U, E> { |
90 | /// Create a new [`BoxCloneServiceLayer`]. |
91 | pub fn new<L>(inner_layer: L) -> Self |
92 | where |
93 | L: Layer<In> + Send + Sync + 'static, |
94 | L::Service: Service<T, Response = U, Error = E> + Send + Clone + 'static, |
95 | <L::Service as Service<T>>::Future: Send + 'static, |
96 | { |
97 | let layer: LayerFn BoxCloneService<…, …, …>> = layer_fn(move |inner: In| { |
98 | let out: impl Service + Send + Clone + 'static + Send = inner_layer.layer(inner); |
99 | BoxCloneService::new(inner:out) |
100 | }); |
101 | |
102 | Self { |
103 | boxed: Arc::new(data:layer), |
104 | } |
105 | } |
106 | } |
107 | |
108 | impl<In, T, U, E> Layer<In> for BoxCloneServiceLayer<In, T, U, E> { |
109 | type Service = BoxCloneService<T, U, E>; |
110 | |
111 | fn layer(&self, inner: In) -> Self::Service { |
112 | self.boxed.layer(inner) |
113 | } |
114 | } |
115 | |
116 | impl<In, T, U, E> Clone for BoxCloneServiceLayer<In, T, U, E> { |
117 | fn clone(&self) -> Self { |
118 | Self { |
119 | boxed: Arc::clone(&self.boxed), |
120 | } |
121 | } |
122 | } |
123 | |
124 | impl<In, T, U, E> fmt::Debug for BoxCloneServiceLayer<In, T, U, E> { |
125 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
126 | fmt.debug_struct(name:"BoxCloneServiceLayer" ).finish() |
127 | } |
128 | } |
129 | |