-
Notifications
You must be signed in to change notification settings - Fork 264
/
lib.rs
182 lines (155 loc) · 4.88 KB
/
lib.rs
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
#![deny(warnings, rust_2018_idioms, clippy::disallowed_method)]
#![forbid(unsafe_code)]
use bytes::BytesMut;
use linkerd_error::Error;
use linkerd_io as io;
use linkerd_stack::{layer, ExtractParam, NewService};
use std::{
fmt,
future::Future,
pin::Pin,
task::{Context, Poll},
};
use thiserror::Error;
use tokio::time;
use tower::util::ServiceExt;
use tracing::{debug, info, trace};
#[async_trait::async_trait]
pub trait Detect<I>: Clone + Send + Sync + 'static {
type Protocol: Send;
async fn detect(&self, io: &mut I, buf: &mut BytesMut)
-> Result<Option<Self::Protocol>, Error>;
}
pub type DetectResult<P> = Result<Option<P>, DetectTimeoutError<P>>;
#[derive(Error)]
#[error("{} protocol detection timed out after {0:?}", std::any::type_name::<P>())]
pub struct DetectTimeoutError<P>(time::Duration, std::marker::PhantomData<P>);
#[derive(Copy, Clone, Debug)]
pub struct Config<D> {
pub detect: D,
pub capacity: usize,
pub timeout: time::Duration,
}
#[derive(Copy, Clone, Debug)]
pub struct NewDetectService<P, D, N> {
inner: N,
params: P,
_detect: std::marker::PhantomData<fn() -> D>,
}
#[derive(Copy, Clone, Debug)]
pub struct DetectService<T, D, N> {
target: T,
config: Config<D>,
inner: N,
}
pub fn allow_timeout<P, T>((p, t): (DetectResult<P>, T)) -> (Option<P>, T) {
match p {
Ok(p) => (p, t),
Err(e) => {
info!("Continuing after timeout: {}", e);
(None, t)
}
}
}
// === impl Config ===
impl<D: Default> Config<D> {
const DEFAULT_CAPACITY: usize = 1024;
pub fn from_timeout(timeout: time::Duration) -> Self {
Self {
detect: D::default(),
capacity: Self::DEFAULT_CAPACITY,
timeout,
}
}
}
// === impl NewDetectService ===
impl<P, D, N> NewDetectService<P, D, N> {
pub fn new(params: P, inner: N) -> Self {
Self {
inner,
params,
_detect: std::marker::PhantomData,
}
}
pub fn layer(params: P) -> impl layer::Layer<N, Service = Self> + Clone
where
P: Clone,
{
layer::mk(move |inner| Self::new(params.clone(), inner))
}
}
impl<T, P, D, N: Clone> NewService<T> for NewDetectService<P, D, N>
where
P: ExtractParam<Config<D>, T>,
{
type Service = DetectService<T, D, N>;
fn new_service(&self, target: T) -> DetectService<T, D, N> {
let config = self.params.extract_param(&target);
DetectService {
target,
config,
inner: self.inner.clone(),
}
}
}
// === impl DetectService ===
impl<I, T, D, N, NSvc> tower::Service<I> for DetectService<T, D, N>
where
T: Clone + Send + 'static,
I: Send + 'static,
D: Detect<I>,
D::Protocol: std::fmt::Debug,
N: NewService<(DetectResult<D::Protocol>, T), Service = NSvc> + Clone + Send + 'static,
NSvc: tower::Service<io::PrefixedIo<I>, Response = ()> + Send,
NSvc::Error: Into<Error>,
NSvc::Future: Send,
{
type Response = ();
type Error = Error;
type Future = Pin<Box<dyn Future<Output = Result<(), Error>> + Send + 'static>>;
#[inline]
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(()))
}
fn call(&mut self, mut io: I) -> Self::Future {
let Config {
detect,
capacity,
timeout,
} = self.config.clone();
let target = self.target.clone();
let inner = self.inner.clone();
Box::pin(async move {
trace!(%capacity, ?timeout, "Starting protocol detection");
let t0 = time::Instant::now();
let mut buf = BytesMut::with_capacity(capacity);
let detected = match time::timeout(timeout, detect.detect(&mut io, &mut buf)).await {
Ok(Ok(protocol)) => {
debug!(?protocol, elapsed = ?time::Instant::now().saturating_duration_since(t0), "DetectResult");
Ok(protocol)
}
Err(_) => Err(DetectTimeoutError(timeout, std::marker::PhantomData)),
Ok(Err(e)) => return Err(e),
};
trace!("Dispatching connection");
let svc = inner.new_service((detected, target));
let mut svc = svc.ready_oneshot().await.map_err(Into::into)?;
svc.call(io::PrefixedIo::new(buf.freeze(), io))
.await
.map_err(Into::into)?;
trace!("Connection completed");
// Hold the service until it's done being used so that cache
// idleness is reset.
drop(svc);
Ok(())
})
}
}
// === impl DetectTimeoutError ===
impl<P> fmt::Debug for DetectTimeoutError<P> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple(std::any::type_name::<Self>())
.field(&self.0)
.finish()
}
}