-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathaxum_traced.rs
More file actions
177 lines (157 loc) · 5.54 KB
/
axum_traced.rs
File metadata and controls
177 lines (157 loc) · 5.54 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
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
/// Minimal re-implementation of `axum::serve` that wraps both the accept-loop
/// future and every per-connection future in `Traced<F>` so that scheduling
/// delays are captured by the telemetry system.
use std::{convert::Infallible, fmt::Debug, future::Future, io, marker::PhantomData, pin::pin};
use axum::serve::Listener;
use axum_core::{body::Body, extract::Request, response::Response};
use dial9_tokio_telemetry::telemetry::TelemetryHandle;
use futures_util::FutureExt as _;
use hyper::body::Incoming;
use hyper_util::{rt::TokioIo, server::conn::auto::Builder, service::TowerToHyperService};
use tokio::sync::watch;
use tower::ServiceExt as _;
use tower_service::Service;
/// A hyper executor that routes spawns through dial9's TelemetryHandle
/// so HTTP/2 internal tasks get wake event tracking.
#[derive(Clone)]
struct TracedExecutor {
handle: TelemetryHandle,
}
impl<Fut> hyper::rt::Executor<Fut> for TracedExecutor
where
Fut: Future + Send + 'static,
Fut::Output: Send + 'static,
{
fn execute(&self, fut: Fut) {
self.handle.spawn(fut);
}
}
/// Our own `IncomingStream`, mirroring `axum::serve::IncomingStream`.
/// We need this because axum's version has private fields and can only be
/// constructed inside the axum crate.
pub struct IncomingStream<'a, L: Listener> {
#[allow(dead_code)]
io: &'a TokioIo<L::Io>,
#[allow(dead_code)]
remote_addr: L::Addr,
}
pub fn serve<L, M, S>(listener: L, make_service: M, handle: TelemetryHandle) -> Serve<L, M, S>
where
L: Listener,
M: for<'a> Service<IncomingStream<'a, L>, Error = Infallible, Response = S>,
S: Service<Request, Response = Response, Error = Infallible> + Clone + Send + 'static,
S::Future: Send,
{
Serve {
listener,
make_service,
handle,
_marker: PhantomData,
}
}
pub struct Serve<L, M, S> {
listener: L,
make_service: M,
handle: TelemetryHandle,
_marker: PhantomData<fn() -> S>,
}
impl<L, M, S> Serve<L, M, S>
where
L: Listener,
{
pub fn with_graceful_shutdown<F>(self, signal: F) -> WithGracefulShutdown<L, M, S, F>
where
F: Future<Output = ()> + Send + 'static,
{
WithGracefulShutdown {
listener: self.listener,
make_service: self.make_service,
handle: self.handle,
signal,
_marker: PhantomData,
}
}
}
pub struct WithGracefulShutdown<L, M, S, F> {
listener: L,
make_service: M,
handle: TelemetryHandle,
signal: F,
_marker: PhantomData<fn() -> S>,
}
impl<L, M, S, F> std::future::IntoFuture for WithGracefulShutdown<L, M, S, F>
where
L: Listener,
L::Addr: Debug,
M: for<'a> Service<IncomingStream<'a, L>, Error = Infallible, Response = S> + Send + 'static,
for<'a> <M as Service<IncomingStream<'a, L>>>::Future: Send,
S: Service<Request, Response = Response, Error = Infallible> + Clone + Send + 'static,
S::Future: Send,
F: Future<Output = ()> + Send + 'static,
{
type Output = io::Result<()>;
type IntoFuture = futures_util::future::BoxFuture<'static, io::Result<()>>;
fn into_future(self) -> Self::IntoFuture {
let Self {
mut listener,
mut make_service,
handle,
signal,
_marker,
} = self;
Box::pin(async move {
let (signal_tx, signal_rx) = watch::channel(());
handle.spawn(async move {
signal.await;
drop(signal_rx);
});
let (close_tx, close_rx) = watch::channel(());
loop {
let (io, remote_addr) = tokio::select! {
conn = listener.accept() => conn,
_ = signal_tx.closed() => break,
};
let io = TokioIo::new(io);
make_service.ready().await.unwrap_or_else(|e| match e {});
let tower_service = make_service
.call(IncomingStream {
io: &io,
remote_addr,
})
.await
.unwrap_or_else(|e| match e {})
.map_request(|req: Request<Incoming>| req.map(Body::new));
let hyper_service = TowerToHyperService::new(tower_service);
let signal_tx = signal_tx.clone();
let close_rx = close_rx.clone();
let traced_handle = handle.clone();
handle.spawn(async move {
let builder = Builder::new(TracedExecutor {
handle: traced_handle,
});
let conn = builder.serve_connection_with_upgrades(io, hyper_service);
let mut conn = pin!(conn);
let mut signal_closed = pin!(signal_tx.closed().fuse());
loop {
tokio::select! {
result = conn.as_mut() => {
if let Err(_err) = result {
tracing::trace!("failed to serve connection: {_err:#}");
}
break;
}
_ = &mut signal_closed => {
conn.as_mut().graceful_shutdown();
}
}
}
drop(close_rx);
});
}
drop(close_rx);
drop(listener);
close_tx.closed().await;
Ok(())
})
}
}