Skip to content

Commit 471fc4e

Browse files
committed
Use axum's native with_executor, remove hand-rolled serve loop
Now that axum supports Serve::with_executor(), implement axum::serve::Executor directly on TelemetryHandle behind an "axum" feature flag.
1 parent 956fc47 commit 471fc4e

6 files changed

Lines changed: 42 additions & 190 deletions

File tree

Cargo.lock

Lines changed: 18 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dial9-tokio-telemetry/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ bon = "3"
3434
aws-sdk-s3-transfer-manager = { version = "0.1.3", optional = true, default-features = false }
3535
aws-sdk-s3 = { version = "1", default-features = false, optional = true, features = ["behavior-version-latest", "sigv4a", "rt-tokio", "default-https-client"] }
3636
aws-config = { version = "1", optional = true, default-features = false }
37+
# TODO: update this once this is merged upstream
38+
axum = { git = "https://github.com/yulnr/axum", branch = "feat/custom-serve-executor", default-features = false, features = ["tokio", "http1", "http2"], optional = true }
3739
flate2 = { version = "1" }
3840
time = { version = "0.3", features = ["formatting", "macros"], optional = true }
3941
metrique-writer = "0.1"
@@ -42,6 +44,7 @@ metrique = { version = "0.1.20", features = ["local-format"] }
4244
[features]
4345
cpu-profiling = ["dep:dial9-perf-self-profile"]
4446
task-dump = ["tokio/taskdump"]
47+
axum = ["dep:axum"]
4548
worker-s3 = ["dep:aws-sdk-s3-transfer-manager", "dep:aws-sdk-s3", "dep:aws-config", "dep:time"]
4649

4750
[dev-dependencies]

dial9-tokio-telemetry/src/telemetry/recorder/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,18 @@ impl TelemetryHandle {
216216
}
217217
}
218218

219+
/// Implement `axum::serve::Executor` for `TelemetryHandle` to allow it to be
220+
/// used as an executor for axum's serve.
221+
#[cfg(feature = "axum")]
222+
impl axum::serve::Executor for TelemetryHandle {
223+
fn execute<Fut>(&self, fut: Fut)
224+
where
225+
Fut: std::future::Future<Output = ()> + Send + 'static,
226+
{
227+
self.spawn(fut);
228+
}
229+
}
230+
219231
/// Holds the background worker thread and its stop signal.
220232
struct WorkerHandle {
221233
shutdown: Option<tokio::sync::oneshot::Sender<Duration>>,

examples/metrics-service/Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ publish = false
77
[dependencies]
88
tokio = { version = "1", features = ["rt-multi-thread", "macros", "time", "sync", "process"] }
99
tokio-util = { version = "0.7", features = ["rt"] }
10-
axum = "0.8"
11-
axum-core = "0.5"
10+
# TODO: update this once this is merged upstream
11+
axum = { git = "https://github.com/yulnr/axum", branch = "feat/custom-serve-executor" }
12+
axum-core = { git = "https://github.com/yulnr/axum", branch = "feat/custom-serve-executor" }
1213
hyper = "1"
1314
hyper-util = { version = "0.1", features = ["tokio", "server-auto"] }
1415
tower = "0.5"
@@ -20,12 +21,12 @@ aws-config = "1"
2021
aws-sdk-dynamodb = "1"
2122
serde = { version = "1", features = ["derive"] }
2223
serde_json = "1"
23-
dial9-tokio-telemetry = { path = "../../dial9-tokio-telemetry", features = ["worker-s3"] }
24+
dial9-tokio-telemetry = { path = "../../dial9-tokio-telemetry", features = ["worker-s3", "axum"] }
2425
reqwest = { version = "0.12", default-features = false, features = ["json"] }
2526
clap = { version = "4", features = ["derive"] }
2627
uuid = { version = "1", features = ["v4"] }
2728
hostname = "0.4"
2829
libc = "0.2"
2930

3031
[target.'cfg(target_os = "linux")'.dependencies]
31-
dial9-tokio-telemetry = { path = "../../dial9-tokio-telemetry", features = ["cpu-profiling", "worker-s3"] }
32+
dial9-tokio-telemetry = { path = "../../dial9-tokio-telemetry", features = ["cpu-profiling", "worker-s3", "axum"] }

examples/metrics-service/src/axum_traced.rs

Lines changed: 0 additions & 177 deletions
This file was deleted.

examples/metrics-service/src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
mod axum_traced;
21
mod buffer;
32
mod ddb;
43
mod routes;
@@ -253,10 +252,11 @@ fn main() -> std::io::Result<()> {
253252
}
254253
});
255254

256-
axum_traced::serve(listener, app.into_make_service(), handle.clone())
255+
axum::serve(listener, app.into_make_service())
256+
.with_executor(handle.clone())
257257
.with_graceful_shutdown(async move { shutdown.cancelled().await })
258-
.await
259-
.unwrap();
258+
.await;
259+
260260
let shutdown = guard.graceful_shutdown(Duration::from_secs(5)).await;
261261
tracing::info!("dial9 shutdown: {shutdown:?}");
262262
});

0 commit comments

Comments
 (0)