性能优化 #

Worker 配置 #

rust
use actix_web::{web, App, HttpServer};

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| App::new())
        .workers(4)
        .bind("127.0.0.1:8080")?
        .run()
        .await
}

Keep-Alive 配置 #

rust
use std::time::Duration;

HttpServer::new(|| App::new())
    .keep_alive(Duration::from_secs(75))
    .bind("127.0.0.1:8080")?
    .run()
    .await

连接池优化 #

rust
use sqlx::postgres::PgPoolOptions;

let pool = PgPoolOptions::new()
    .max_connections(20)
    .min_connections(5)
    .acquire_timeout(Duration::from_secs(30))
    .connect(&database_url)
    .await?;

响应压缩 #

rust
use actix_web::middleware::Compress;

App::new()
    .wrap(Compress::default())

缓存策略 #

rust
use actix_web::http::header;

HttpResponse::Ok()
    .insert_header(("Cache-Control", "public, max-age=3600"))
    .json(data)

下一步 #

继续学习 RESTful API 实战,了解完整项目开发!

最后更新:2026-03-29