Rust / 系统课时

完成高并发服务演练

完成高并发服务演练

这一节把协议分帧、并发控制、请求超时、结构化取消、优雅关闭、tracing 和测试合并成一个可运行的 Tokio TCP 服务。完成后,你不仅能跑通正常请求,还能用自动化测试验证超时、并发上限与关闭路径。

学习目标

  • 用长度受限的行协议完成 TCP 分帧,并用 Semaphore 限制并发连接数。
  • 用 CancellationToken、timeout 和 JoinSet 组织请求取消与优雅关闭。
  • 用 tracing 记录关键事件,并通过集成测试验证正常与故障路径。

明确服务边界

服务采用“每行一个请求”的文本协议:普通文本返回大写结果,SLOW <毫秒> 用来模拟慢请求。单帧最大 8 KiB;并发上限约束的是同时存活的连接,而不是每秒请求数;单个请求超过时限时返回 ERR timeout。

关闭时先让监听循环停止接收新连接,再由父 CancellationToken 取消所有连接任务,最后等待 JoinSet 回收任务。若任务在宽限期内仍未退出,再执行强制中止。这样可以把“停止入口、通知子任务、等待清理、兜底终止”放在同一条结构化关闭链路中。

Tokio TCP 服务的并发与关闭链路

实现完整服务

新建 Cargo 项目后,将依赖配置为下面内容。tokio-util 同时启用 codec 和 rt,分别提供协议编解码与 CancellationToken;tracing-subscriber 启用环境过滤和 JSON 输出,便于后续接日志平台。

toml
[package]
name = "tokio-tcp-drill"
version = "0.1.0"
edition = "2021"

[dependencies]
futures-util = "0.3"
tokio = { version = "1", features = ["full"] }
tokio-util = { version = "0.7", features = ["codec", "rt"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter", "json"] }

把核心服务放到 src/lib.rs。监听循环先取得信号量许可再 accept,因此达到上限后不会继续把连接交给应用任务;每个连接都持有一个许可,任务退出时自动释放。下面是项目文件片段示例,依赖本节的 Cargo.toml 与 src/main.rs,不单独运行。

rust
use std::{io, sync::Arc, time::Duration};

use futures_util::{SinkExt, StreamExt};
use tokio::{
    net::{TcpListener, TcpStream},
    sync::Semaphore,
    task::JoinSet,
    time::{sleep, timeout},
};
use tokio_util::{
    codec::{Framed, LinesCodec, LinesCodecError},
    sync::CancellationToken,
};
use tracing::{info, warn};

#[derive(Clone, Copy)]
pub struct Config {
    pub max_connections: usize,
    pub request_timeout: Duration,
    pub shutdown_grace: Duration,
}

pub async fn run(
    listener: TcpListener,
    shutdown: CancellationToken,
    cfg: Config,
) -> io::Result<()> {
    if cfg.max_connections == 0 {
        return Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "max_connections must be greater than zero",
        ));
    }

    let limiter = Arc::new(Semaphore::new(cfg.max_connections));
    let mut tasks = JoinSet::new();

    loop {
        // 先拿许可再接收连接,达到上限时 accept 循环自然背压。
        let permit = tokio::select! {
            _ = shutdown.cancelled() => break,
            result = limiter.clone().acquire_owned() => {
                result.map_err(|_| io::Error::new(
                    io::ErrorKind::Other,
                    "connection limiter closed",
                ))?
            }
        };

        let (stream, peer) = tokio::select! {
            _ = shutdown.cancelled() => break,
            result = listener.accept() => result?,
        };

        let child = shutdown.child_token();
        let request_timeout = cfg.request_timeout;

        tasks.spawn(async move {
            let _permit = permit;
            info!(%peer, "connection accepted");

            if let Err(error) =
                handle_connection(stream, child, request_timeout).await
            {
                warn!(%peer, %error, "connection failed");
            }
        });
    }

    info!("shutdown started");

    // 先协作式取消;宽限期耗尽后再强制中止残留任务。
    if timeout(cfg.shutdown_grace, async {
        while let Some(result) = tasks.join_next().await {
            if let Err(error) = result {
                warn!(%error, "connection task join failed");
            }
        }
    })
    .await
    .is_err()
    {
        warn!("shutdown grace expired; aborting remaining tasks");
        tasks.abort_all();
        while tasks.join_next().await.is_some() {}
    }

    info!("shutdown complete");
    Ok(())
}

async fn handle_connection(
    stream: TcpStream,
    cancel: CancellationToken,
    request_timeout: Duration,
) -> io::Result<()> {
    // 限制单帧长度,避免没有换行符的输入无限占用缓冲区。
    let codec = LinesCodec::new_with_max_length(8 * 1024);
    let mut framed = Framed::new(stream, codec);

    loop {
        let frame = tokio::select! {
            _ = cancel.cancelled() => break,
            item = framed.next() => item,
        };

        let Some(frame) = frame else {
            break;
        };
        let request = frame.map_err(codec_error)?;

        let response = tokio::select! {
            _ = cancel.cancelled() => break,
            result = timeout(request_timeout, process(&request)) => {
                match result {
                    Ok(response) => response,
                    Err(_) => {
                        warn!(request = %request, "request timed out");
                        "ERR timeout".to_owned()
                    }
                }
            }
        };

        tokio::select! {
            _ = cancel.cancelled() => break,
            result = framed.send(response) => {
                result.map_err(codec_error)?;
            }
        }
    }

    Ok(())
}

async fn process(request: &str) -> String {
    if let Some(ms) = request
        .strip_prefix("SLOW ")
        .and_then(|value| value.parse::<u64>().ok())
    {
        sleep(Duration::from_millis(ms)).await;
        return format!("OK SLOW {ms}");
    }

    format!("OK {}", request.to_ascii_uppercase())
}

fn codec_error(error: LinesCodecError) -> io::Error {
    io::Error::new(io::ErrorKind::InvalidData, error.to_string())
}

入口放在 src/main.rs。这里把 Ctrl+C 转换成根取消令牌,所有连接任务都通过 child_token() 继承这次取消。下面同样是项目文件片段示例,依赖同一项目中的 src/lib.rs。

rust
use std::{io, time::Duration};

use tokio::net::TcpListener;
use tokio_util::sync::CancellationToken;
use tracing::info;
use tracing_subscriber::EnvFilter;

use tokio_tcp_drill::{run, Config};

#[tokio::main]
async fn main() -> io::Result<()> {
    tracing_subscriber::fmt()
        .with_env_filter(EnvFilter::from_default_env())
        .json()
        .init();

    let listener = TcpListener::bind("127.0.0.1:7000").await?;
    let address = listener.local_addr()?;
    info!(%address, "listening");

    let shutdown = CancellationToken::new();
    let signal_token = shutdown.clone();

    tokio::spawn(async move {
        if tokio::signal::ctrl_c().await.is_ok() {
            signal_token.cancel();
        }
    });

    run(
        listener,
        shutdown,
        Config {
            max_connections: 128,
            request_timeout: Duration::from_millis(500),
            shutdown_grace: Duration::from_secs(3),
        },
    )
    .await
}

用测试覆盖正常与故障路径

集成测试不依赖固定端口,而是绑定 127.0.0.1:0 让操作系统分配空闲端口。第一项测试验证分帧、正常响应、请求超时和取消关闭;第二项把并发上限设为 1,保持第一个连接不关闭,并确认第二个连接暂时拿不到响应,直到第一个连接释放许可。下面是 tests/service.rs 的项目文件片段示例,依赖前面的库实现。

rust
use std::{error::Error, time::Duration};

use futures_util::{SinkExt, StreamExt};
use tokio::{
    net::{TcpListener, TcpStream},
    time::timeout,
};
use tokio_util::{
    codec::{Framed, LinesCodec},
    sync::CancellationToken,
};

use tokio_tcp_drill::{run, Config};

type TestResult = Result<(), Box<dyn Error + Send + Sync>>;

#[tokio::test]
async fn handles_frames_timeout_and_shutdown() -> TestResult {
    let listener = TcpListener::bind("127.0.0.1:0").await?;
    let address = listener.local_addr()?;
    let shutdown = CancellationToken::new();

    let server = tokio::spawn(run(
        listener,
        shutdown.clone(),
        Config {
            max_connections: 4,
            request_timeout: Duration::from_millis(40),
            shutdown_grace: Duration::from_secs(1),
        },
    ));

    let stream = TcpStream::connect(address).await?;
    let mut client = Framed::new(stream, LinesCodec::new());

    client.send("ping".to_owned()).await?;
    let response = client
        .next()
        .await
        .transpose()?
        .expect("server should answer");
    assert_eq!(response, "OK PING");

    client.send("SLOW 100".to_owned()).await?;
    let response = client
        .next()
        .await
        .transpose()?
        .expect("server should answer");
    assert_eq!(response, "ERR timeout");

    // 客户端仍保持连接,取消令牌也必须让服务在宽限期内退出。
    shutdown.cancel();
    let joined = timeout(Duration::from_secs(2), server).await?;
    joined??;

    Ok(())
}

#[tokio::test]
async fn caps_concurrent_connections() -> TestResult {
    let listener = TcpListener::bind("127.0.0.1:0").await?;
    let address = listener.local_addr()?;
    let shutdown = CancellationToken::new();

    let server = tokio::spawn(run(
        listener,
        shutdown.clone(),
        Config {
            max_connections: 1,
            request_timeout: Duration::from_secs(1),
            shutdown_grace: Duration::from_secs(1),
        },
    ));

    // 第一个连接先得到服务,并持续持有唯一许可。
    let first = TcpStream::connect(address).await?;
    let mut first = Framed::new(first, LinesCodec::new());
    first.send("first".to_owned()).await?;
    let response = first
        .next()
        .await
        .transpose()?
        .expect("first response");
    assert_eq!(response, "OK FIRST");

    let second = TcpStream::connect(address).await?;
    let mut second = Framed::new(second, LinesCodec::new());
    second.send("second".to_owned()).await?;

    // 第二个 TCP 握手可以完成,但服务端尚未为它分配连接任务。
    assert!(timeout(Duration::from_millis(100), second.next())
        .await
        .is_err());

    drop(first);

    let response = timeout(Duration::from_secs(1), second.next())
        .await?
        .transpose()?
        .expect("second response");
    assert_eq!(response, "OK SECOND");

    shutdown.cancel();
    drop(second);
    let joined = timeout(Duration::from_secs(2), server).await?;
    joined??;

    Ok(())
}

在项目根目录执行测试即可同时完成编译和行为验收。

bash
cargo test --test service -- --quiet

预期输出

text
running 2 tests
..
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in <TIME>s

<TIME> 会随机器性能变化。若第一项失败,优先检查协议是否按“每行一帧”处理以及超时是否包住业务 future;若第二项失败,检查信号量许可是否真正由连接任务持有到退出,而不是在 spawn 前提前释放。

观察与定位故障

这个服务把关键状态都放在可观察边界上:接受连接时记录 peer,超时时记录原请求,连接错误记录 codec 或 I/O 错误,关闭阶段分别记录开始、宽限期耗尽和完成。生产环境可以进一步加入 request id、耗时直方图和当前连接数,但不要把高基数字段直接作为指标标签。

面对压测或停机异常时,按“是否还在接新连接 → 是否卡在并发许可 → 是否触发请求超时 → 子任务是否收到取消 → JoinSet 是否在宽限期内清空”的顺序排查。这个顺序与代码里的控制边界一致,能避免只盯某一条错误日志而忽略真正的阻塞点。

按自己的节奏,确认真正理解。

目录与阅读设置

阅读设置

17

记录一个想法

完成高并发服务演练

0 / 10000 · 笔记保存到账号。未提交的修改会在关闭、离开或刷新后丢失。

登录哪都通