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
use futures_util::{future::BoxFuture, Future, FutureExt};
use std::{convert::Infallible, sync::Arc};
use tower::{
layer::{layer_fn, util::Stack},
service_fn,
util::BoxService,
Layer, Service,
};
use super::{
error::HrpcError,
socket::{self, Socket, SocketHandler},
};
use crate::{request::BoxRequest, response::BoxResponse, Request, Response};
pub(crate) type CallFuture<'a> = BoxFuture<'a, Result<BoxResponse, Infallible>>;
pub struct HrpcService {
svc: BoxService<BoxRequest, BoxResponse, Infallible>,
}
impl HrpcService {
pub fn new<S>(svc: S) -> Self
where
S: Service<BoxRequest, Response = BoxResponse, Error = Infallible> + Send + 'static,
S::Future: Send,
{
super::utils::downcast::if_downcast_into!(S, HrpcService, svc, {
return Self { svc: svc.svc };
});
Self {
svc: BoxService::new(svc),
}
}
pub fn layer<L, S>(self, layer: L) -> Self
where
L: Layer<Self, Service = S>,
S: Service<BoxRequest, Response = BoxResponse, Error = Infallible> + Send + 'static,
S::Future: Send,
{
HrpcService::new(layer.layer(self))
}
}
impl Service<BoxRequest> for HrpcService {
type Response = BoxResponse;
type Error = Infallible;
type Future = CallFuture<'static>;
fn poll_ready(
&mut self,
cx: &mut std::task::Context<'_>,
) -> std::task::Poll<Result<(), Self::Error>> {
self.svc.poll_ready(cx)
}
fn call(&mut self, req: BoxRequest) -> Self::Future {
Service::call(&mut self.svc, req)
}
}
#[derive(Clone)]
pub struct HrpcLayer {
inner: Arc<dyn Layer<HrpcService, Service = HrpcService> + Sync + Send + 'static>,
}
impl HrpcLayer {
pub fn new<L, S>(layer: L) -> Self
where
L: Layer<HrpcService, Service = S> + Sync + Send + 'static,
S: Service<BoxRequest, Response = BoxResponse, Error = Infallible> + Send + 'static,
S::Future: Send,
{
super::utils::downcast::if_downcast_into!(S, HrpcLayer, layer, {
return Self { inner: layer.inner };
});
let layer = layer_fn(move |svc| {
let new_svc = layer.layer(svc);
HrpcService::new(new_svc)
});
Self {
inner: Arc::new(layer),
}
}
pub(crate) fn stack(inner: HrpcLayer, outer: HrpcLayer) -> Self {
Self {
inner: Arc::new(Stack::new(inner, outer)),
}
}
}
impl<S> Layer<S> for HrpcLayer
where
S: Service<BoxRequest, Response = BoxResponse, Error = Infallible> + Send + 'static,
S::Future: Send,
{
type Service = HrpcService;
fn layer(&self, inner: S) -> Self::Service {
self.inner.layer(HrpcService::new(inner))
}
}
pub trait HrpcLayerExt: Sized {
fn into_hrpc_layer(self) -> HrpcLayer;
}
impl<L, S> HrpcLayerExt for L
where
L: Layer<HrpcService, Service = S> + Sync + Send + 'static,
S: Service<BoxRequest, Response = BoxResponse, Error = Infallible> + Send + 'static,
S::Future: Send,
{
fn into_hrpc_layer(self) -> HrpcLayer {
HrpcLayer::new(self)
}
}
pub fn not_found() -> HrpcService {
HrpcService::new(tower::service_fn(|_| {
futures_util::future::ready(Ok(HrpcError::new_not_found("not found").into()))
}))
}
#[doc(hidden)]
pub fn unary_handler<Req, Resp, HandlerFn, HandlerFut>(handler: HandlerFn) -> HrpcService
where
Req: prost::Message + Default,
Resp: prost::Message,
HandlerFut: Future<Output = Result<Response<Resp>, HrpcError>> + Send,
HandlerFn: FnOnce(Request<Req>) -> HandlerFut + Clone + Send + 'static,
{
let service = service_fn(move |req: BoxRequest| {
(handler.clone())(req.map::<Req>())
.map(|res| Ok(res.map_or_else(HrpcError::into, |resp| resp.map::<()>())))
});
HrpcService::new(service)
}
#[doc(hidden)]
pub fn ws_handler<Req, Resp, HandlerFn, HandlerFut>(handler: HandlerFn) -> HrpcService
where
Req: prost::Message + Default + 'static,
Resp: prost::Message + 'static,
HandlerFut: Future<Output = Result<(), HrpcError>> + Send,
HandlerFn: FnOnce(Request<()>, Socket<Resp, Req>) -> HandlerFut + Clone + Send + Sync + 'static,
{
let service = service_fn(move |req: BoxRequest| {
let handler = handler.clone();
let socket_handler = SocketHandler {
inner: Box::new(move |rx, tx| {
Box::pin(async move {
let socket =
Socket::new(rx, tx, socket::encode_message, socket::decode_message);
let res = handler(req, socket).await;
if let Err(err) = res {
tracing::error!("{}", err);
}
})
}),
};
let mut response = Response::empty();
response.extensions_mut().insert(socket_handler);
futures_util::future::ready(Ok(response))
});
HrpcService::new(service)
}