Skip to content

Commit 38c565e

Browse files
Resolve Axum 0.7 regressions (#9291)
* perf: restore axum [postgresql] performance * fix: axum plaintext regression * feat: upgrade to axum 0.7.6
1 parent 27cade8 commit 38c565e

File tree

7 files changed

+50
-50
lines changed

7 files changed

+50
-50
lines changed

frameworks/Rust/axum/Cargo.lock

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

frameworks/Rust/axum/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ simd-json = [
3939
]
4040

4141
[dependencies]
42-
axum = { version = "0.7.5", default-features = false, features = [
42+
axum = { version = "0.7.6", default-features = false, features = [
4343
"json",
4444
"query",
4545
"http1",

frameworks/Rust/axum/src/main.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ use axum::Json;
1111
use common::simd_json::Json;
1212

1313
/// Return a plaintext static string.
14-
pub async fn plaintext() -> impl IntoResponse {
15-
(StatusCode::OK, "Hello, World!")
14+
#[inline(always)]
15+
pub async fn plaintext() -> &'static str {
16+
"Hello, World!"
1617
}
1718

1819
/// Return a JSON message.
20+
#[inline(always)]
1921
pub async fn json() -> impl IntoResponse {
2022
let message = Message {
2123
message: "Hello, World!",

frameworks/Rust/axum/src/main_mongo.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
mod common;
22
mod mongo;
3+
mod server;
34
//mod mongo_raw;
45

56
use std::time::Duration;
@@ -24,8 +25,6 @@ use mongodb::{
2425
use rand::{rngs::SmallRng, thread_rng, Rng, SeedableRng};
2526
use yarte::Template;
2627

27-
mod server;
28-
2928
use common::{
3029
get_env,
3130
utils::{parse_params, Params, Utf8Html},
@@ -117,25 +116,10 @@ async fn fortunes(DatabaseConnection(db): DatabaseConnection) -> impl IntoRespon
117116

118117
fn main() {
119118
dotenv().ok();
120-
121-
let rt = tokio::runtime::Builder::new_current_thread()
122-
.enable_all()
123-
.build()
124-
.unwrap();
125-
126-
for _ in 1..num_cpus::get() {
127-
std::thread::spawn(move || {
128-
let rt = tokio::runtime::Builder::new_current_thread()
129-
.enable_all()
130-
.build()
131-
.unwrap();
132-
rt.block_on(serve());
133-
});
134-
}
135-
rt.block_on(serve());
119+
server::start_tokio(serve_app)
136120
}
137121

138-
async fn serve() {
122+
async fn serve_app() {
139123
let database_url: String = get_env("MONGODB_URL");
140124
let max_pool_size: u32 = get_env("MONGODB_MAX_POOL_SIZE");
141125
let min_pool_size: u32 = get_env("MONGODB_MIN_POOL_SIZE");

frameworks/Rust/axum/src/main_mongo_raw.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,25 +86,10 @@ async fn updates(
8686

8787
fn main() {
8888
dotenv().ok();
89-
90-
let rt = tokio::runtime::Builder::new_current_thread()
91-
.enable_all()
92-
.build()
93-
.unwrap();
94-
95-
for _ in 1..num_cpus::get() {
96-
std::thread::spawn(move || {
97-
let rt = tokio::runtime::Builder::new_current_thread()
98-
.enable_all()
99-
.build()
100-
.unwrap();
101-
rt.block_on(serve());
102-
});
103-
}
104-
rt.block_on(serve());
89+
server::start_tokio(serve_app)
10590
}
10691

107-
async fn serve() {
92+
async fn serve_app() {
10893
let database_url: String = get_env("MONGODB_URL");
10994
let max_pool_size: u32 = get_env("MONGODB_MAX_POOL_SIZE");
11095
let min_pool_size: u32 = get_env("MONGODB_MIN_POOL_SIZE");

frameworks/Rust/axum/src/main_pg.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ async fn updates(
7878
(StatusCode::OK, Json(worlds))
7979
}
8080

81-
#[tokio::main]
82-
async fn main() {
81+
fn main() {
8382
dotenv().ok();
83+
server::start_tokio(serve_app)
84+
}
8485

86+
async fn serve_app() {
8587
let database_url: String = get_env("POSTGRES_URL");
8688

8789
// Create shared database connection

frameworks/Rust/axum/src/server.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::{
2+
future::Future,
23
io,
34
net::{Ipv4Addr, SocketAddr, TcpListener},
45
};
@@ -104,3 +105,26 @@ pub async fn serve_hyper(app: Router<()>, port: Option<u16>) {
104105
});
105106
}
106107
}
108+
109+
/// Start a single-threaded tokio runtime on multiple threads.
110+
#[allow(dead_code)]
111+
pub fn start_tokio<Fut>(f: fn() -> Fut)
112+
where
113+
Fut: Future<Output = ()> + 'static,
114+
{
115+
let rt = tokio::runtime::Builder::new_current_thread()
116+
.enable_all()
117+
.build()
118+
.unwrap();
119+
120+
for _ in 1..num_cpus::get() {
121+
std::thread::spawn(move || {
122+
let rt = tokio::runtime::Builder::new_current_thread()
123+
.enable_all()
124+
.build()
125+
.unwrap();
126+
rt.block_on(f());
127+
});
128+
}
129+
rt.block_on(f());
130+
}

0 commit comments

Comments
 (0)