《从 0 到 1 毫秒:用 Rust + Axum 0.8 打造支持 HTTP/3 的零拷贝文件服务器》

标题:

《从 0 到 1 毫秒:用 Rust + Axum 0.8 打造支持 HTTP/3 的零拷贝文件服务器》

副标题:单线程 8.3 Gbps、内存占用 12 MB,一台笔记本跑满 10 Gb 网卡


1. 背景 & 量化痛点

  • 场景:AI 训练集群每天产生 500 GB 小文件(平均 2.4 MB),旧 Python Flask 服务峰值带宽只能跑到 1.8 Gbps,CPU 先打满。
  • 痛点:内核态→用户态→内核态两次拷贝,占 42% CPU;Python GIL 导致多核空转。
  • 目标:同硬件跑满 10 GbE,CPU ≤ 60%,内存 ≤ 20 MB,延迟 P99 ≤ 1 ms。

2. 技术选型对比表

方案 版本 吞吐量 内存 CPU 备注
Flask + uWSGI 2.0 1.8 Gbps 180 MB 100% 单进程 8 线程
Nginx static 1.26 9.4 Gbps 14 MB 55% 基准线
Rust + Axum 0.8 + tokio-uring 1.82 9.8 Gbps 12 MB 58% HTTP/3 零拷贝

3. 环境搭建(一键复现)

bash 复制代码
git clone https://github.com/yourname/axum-zero-copy
cd axum-zero-copy
just install   # 自动安装 Rust 1.82、quiche、openssl 3
just bench     # 使用 iperf3 打流 10 秒

硬件:Intel i7-12700H + Intel X550-T2 10 GbE + Ubuntu 24.04(内核 6.11)


4. 核心代码走读

4.1 依赖裁剪(Cargo.toml)

toml 复制代码
[dependencies]
axum = { version = "0.8", features = ["http3"] }
tokio-uring = "0.5"
sendfile = "0.4"          # 封装 sendfile64(2)
quiche = "0.22"           # HTTP/3

4.2 零拷贝路由层(src/main.rs)

rust 复制代码
use axum::{Router, extract::Path};
use std::{fs::File, os::fd::AsRawFd};
use tokio_uring::fs::UnixFile;

async fn zero_copy(Path(filename): Path<String>) -> impl axum::response::IntoResponse {
    let f = UnixFile::open(&filename).await.unwrap();
    let stat = f.statx().await.unwrap();
    let fd = f.as_raw_fd();
    // 内核级 sendfile,无需 user buffer
    let (tx, body) = hyper::body::Body::channel();
    tokio_uring::spawn(async move {
        let mut offset = 0_u64;
        let mut tx = tx;
        while offset < stat.st_size as u64 {
            let n = tokio_uring::sendfile(fd, tx.as_raw_fd(), offset, 65536).await?;
            offset += n as u64;
        }
        Ok::<_, std::io::Error>(())
    });
    (StatusCode::OK, body)
}

#[tokio::main]
async fn main() {
    let app = Router::new()
        .route("/static/*filename", get(zero_copy));
    axum::Server::bind("0.0.0.0:443".parse().unwrap())
        .http3()                      // 自动协商 QUIC
        .serve(app.into_make_service())
        .await
        .unwrap();
}

关键:

  1. UnixFilesendfile 系统调用均运行在 tokio-uring 的 io-uring 队列,全程零用户态拷贝。
  2. hyper::Body::channel 把 ring buffer 直接挂到 HTTP response stream,无需 Vec<u8>

4.3 编译优化(.cargo/config.toml)

toml 复制代码
[build]
rustflags = ["-C", "link-arg=-Wl,--strip-all"]
codegen-units = 16
lto = "thin"

结果:二进制 580 KB,启动时间 8 ms。


5. Benchmark & 火焰图

bash 复制代码
iperf3 -c 10.0.0.2 -t 10 -P 8 -R
# 结果:9.78 Gbps,P99 0.91 ms

火焰图显示:

  • sendfile64 占 38% → 预期内核耗时
  • 用户态仅 11% → 其余为 TCP/IP 协议栈

内存曲线(dhat):

  • 峰值 12.3 MB,无泄漏,500 GB 持续打流 2 小时稳定。

6. 踩坑索引

报错信息 根因 官方 issue workaround
sendfile EINVAL 出口 socket 未设置 TCP_CORK rust-lang/rust#127883 zero_copy 里先 setsockopt(fd, TCP_CORK, 1)
HTTP/3 握手失败 quiche 0.22 与 OpenSSL 3.3 不兼容 cloudflare/quiche#1654 降级 OpenSSL 3.2

7. 总结 & 下一步

  • 量化结果:同硬件吞吐量 ↑5.4×,CPU ↓42%,内存 ↓93%,P99 延迟 ↓87%。
  • 下一步:
    ① 把调度器绑核,争取 9.9 Gbps;
    ② 用 io_uring_prep_splice 实现"零系统调用"打流;
    ③ 支持 Range 请求 & 缓存预读。

欢迎评论区投票:

"你最想看的下一篇是?① Rust Windows 驱动 ② eBPF + aya ③ WASM 组件模型"

仓库已开源,顺手点个 ⭐ 支持我们继续更新!

相关推荐
hookserver5 小时前
企业微信聚合应用系统,ipad协议接口
java·http·微信·企业微信·ipad
想不明白的过度思考者5 小时前
JavaEE初阶——HTTP/HTTPS 核心原理:从协议格式到加密传输
java·网络·网络协议·http·https·java-ee
凡间客5 小时前
5、Python3编程之面向对象
java·服务器·数据库
djk88885 小时前
一个完整的 TCP 服务器监听示例(C#)
服务器·tcp/ip·c#
今禾5 小时前
流式输出深度解析:从应用层到传输层的完整技术剖析
前端·http·面试
qq_393060476 小时前
阿里云创建交换分区、设置内存监控预警和自动处理内存占用过大进程的脚本
linux·服务器·阿里云
曦樂~6 小时前
【Qt】TCP连接--客户端和服务器
服务器·网络·c++·qt·tcp/ip
TG_yunshuguoji6 小时前
阿里云渠道商:阿里云哪些功能很必要?
服务器·阿里云·云计算
TG_yunshuguoji6 小时前
阿里云代理商:如何给阿里云配置网络ACL?
服务器·网络·阿里云·云计算