-
-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathlayer.rs
More file actions
151 lines (131 loc) · 4.42 KB
/
Copy pathlayer.rs
File metadata and controls
151 lines (131 loc) · 4.42 KB
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
use std::{
task::{Context, Poll},
time::Duration,
};
use http::{Request, Response};
use tower::{Layer, Service};
use super::future::{ResponseBodyTimeoutFuture, ResponseFuture};
use crate::{
client::layer::{
config::RequestTimeoutOptions,
timeout::{TimeoutBody, TimeoutOptions},
},
core::ext::RequestConfig,
error::BoxError,
};
/// [`Layer`] that applies a [`Timeout`] middleware to a service.
// This layer allows you to set a total timeout and a read timeout for requests.
#[derive(Clone)]
pub struct TimeoutLayer {
timeout: RequestConfig<RequestTimeoutOptions>,
}
impl TimeoutLayer {
/// Create a new [`TimeoutLayer`].
pub const fn new(options: TimeoutOptions) -> Self {
TimeoutLayer {
timeout: RequestConfig::new(Some(options)),
}
}
}
impl<S> Layer<S> for TimeoutLayer {
type Service = Timeout<S>;
fn layer(&self, service: S) -> Self::Service {
Timeout {
inner: service,
timeout: self.timeout,
}
}
}
/// Middleware that applies both a total timeout and a per-read timeout to the response body of a
/// request with a [`Service`].
#[derive(Clone)]
pub struct Timeout<T> {
inner: T,
timeout: RequestConfig<RequestTimeoutOptions>,
}
impl<ReqBody, ResBody, S> Service<Request<ReqBody>> for Timeout<S>
where
S: Service<Request<ReqBody>, Response = Response<ResBody>, Error = BoxError>,
{
type Response = S::Response;
type Error = BoxError;
type Future = ResponseFuture<S::Future>;
#[inline(always)]
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}
fn call(&mut self, req: Request<ReqBody>) -> Self::Future {
let (total_timeout, read_timeout) = resolve_timeout_config(&self.timeout, req.extensions());
ResponseFuture {
response: self.inner.call(req),
total_timeout: total_timeout.map(tokio::time::sleep),
read_timeout: read_timeout.map(tokio::time::sleep),
}
}
}
/// [`Layer`] that applies a [`ResponseBodyTimeout`] middleware to a service.
// This layer allows you to set a total timeout and a read timeout for the response body.
#[derive(Clone)]
pub struct ResponseBodyTimeoutLayer {
timeout: RequestConfig<RequestTimeoutOptions>,
}
impl ResponseBodyTimeoutLayer {
/// Creates a new [`ResponseBodyTimeoutLayer`].
pub const fn new(options: TimeoutOptions) -> Self {
Self {
timeout: RequestConfig::new(Some(options)),
}
}
}
impl<S> Layer<S> for ResponseBodyTimeoutLayer {
type Service = ResponseBodyTimeout<S>;
fn layer(&self, inner: S) -> Self::Service {
ResponseBodyTimeout {
inner,
timeout: self.timeout,
}
}
}
/// Middleware that timeouts the response body of a request with a [`Service`] to a total timeout
/// and a read timeout.
#[derive(Clone)]
pub struct ResponseBodyTimeout<S> {
inner: S,
timeout: RequestConfig<RequestTimeoutOptions>,
}
impl<S, ReqBody, ResBody> Service<Request<ReqBody>> for ResponseBodyTimeout<S>
where
S: Service<Request<ReqBody>, Response = Response<ResBody>>,
{
type Response = Response<TimeoutBody<ResBody>>;
type Error = S::Error;
type Future = ResponseBodyTimeoutFuture<S::Future>;
#[inline(always)]
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx)
}
fn call(&mut self, req: Request<ReqBody>) -> Self::Future {
let (total_timeout, read_timeout) = resolve_timeout_config(&self.timeout, req.extensions());
ResponseBodyTimeoutFuture {
inner: self.inner.call(req),
total_timeout,
read_timeout,
}
}
}
#[inline]
fn resolve_timeout_config(
layer_opts: &RequestConfig<RequestTimeoutOptions>,
extensions: &http::Extensions,
) -> (Option<Duration>, Option<Duration>) {
let request_opts = layer_opts.fetch(extensions);
match (layer_opts.as_ref(), request_opts) {
(Some(layer_opts), Some(request_opts)) => (
request_opts.total_timeout.or(layer_opts.total_timeout),
request_opts.read_timeout.or(layer_opts.read_timeout),
),
(Some(layer_opts), None) => (layer_opts.total_timeout, layer_opts.read_timeout),
(None, Some(request_opts)) => (request_opts.total_timeout, request_opts.read_timeout),
(None, None) => (None, None),
}
}