rat_congestion

Crates.iorat_congestion
lib.rsrat_congestion
version0.1.0
created_at2025-09-26 06:38:45.42574+00
updated_at2025-09-26 06:38:45.42574+00
descriptionHigh-performance congestion control algorithms for network protocols
homepagehttps://github.com/0ldm0s/rat_congestion
repositoryhttps://github.com/0ldm0s/rat_congestion
max_upload_size
id1855483
size171,356
0ldm0s (0ldm0s)

documentation

https://docs.rs/rat_congestion

README

rat_congestion

Crates.io Crates.io License

English | 日本語 | 中文

一个高性能、平台优化的拥塞控制库,实现了 BBR 和 CUBIC 算法,具备智能切换能力。

概述

rat_congestion 是一个专为网络协议设计的纯计算拥塞控制引擎。该库不收集任何系统数据,所有网络数据均由调用方提供,确保了最大的灵活性和性能。

核心特性

  • 纯计算引擎: 不收集系统数据,所有数据由调用方提供
  • BBR 算法: 实现 Google 的 Bottleneck Bandwidth and Round-trip propagation time 算法
  • CUBIC 算法: 高性能 CUBIC 拥塞控制算法及其优化版本
  • 智能切换: 基于网络条件的动态算法选择
  • 平台优化: 自动平台检测和 SIMD 优化
  • 零拷贝设计: 最小化内存分配,高效批处理
  • 统一接口: 单一 API 适配所有平台,自动配置

算法支持

BBR (Bottleneck Bandwidth and Round-trip propagation time)

  • 基于带宽和延迟的拥塞控制
  • 适用于高带宽、低延迟网络
  • 优秀的吞吐量表现

CUBIC

  • 基于丢包的拥塞控制算法
  • 在拥塞和可变网络条件下表现稳定
  • 广泛部署和验证的算法

自适应切换

  • 根据网络条件自动选择最优算法
  • 结合 BBR 和 CUBIC 的优势
  • 智能的算法切换策略

快速开始

添加依赖

Cargo.toml 中添加:

[dependencies]
rat_congestion = "0.1.0"

基本使用

use rat_congestion::{CongestionController, NetworkMetrics, MetricsWindow};
use std::time::Duration;

// 创建带有自动平台优化的控制器
// 注意:实际使用中需要提供 NetworkMetrics 和 MetricsWindow 的实现
let mut controller = CongestionController::new(metrics, metrics_window);

// 处理数据包确认
controller.on_packet_acked(1500, Duration::from_millis(50));

// 处理数据包丢失
controller.on_packet_lost(1500);

// 获取当前窗口大小
let window_size = controller.window_size();

// 获取当前发送速率
let pacing_rate = controller.pacing_rate();

高级配置

use rat_congestion::{CongestionController, ControllerConfig};

// 创建自定义配置
let config = ControllerConfig::builder()
    .initial_algorithm("bbr")
    .auto_switching(true)
    .platform_optimized(true)
    .build();

// 使用自定义配置创建控制器
let mut controller = CongestionController::with_config(config, metrics, metrics_window);

配置选项

预设配置

// 平台优化配置(推荐)
let config = ControllerConfig::platform_optimized();

// 高性能配置
let config = ControllerConfig::high_performance();

// 稳定网络配置
let config = ControllerConfig::stable_network();

// 自适应配置
let config = ControllerConfig::adaptive();

自定义配置

let config = ControllerConfig::builder()
    .initial_algorithm("cubic")        // 初始算法: "bbr", "cubic", "auto"
    .auto_switching(true)              // 启用自动切换
    .platform_optimized(true)          // 启用平台优化
    .metrics_window_size(100)          // 指标窗口大小
    .switch_cooldown(Duration::from_secs(5))  // 切换冷却时间
    .build();

特性标志

Cargo.toml 中可以选择性启用特性:

[dependencies]
rat_congestion = { version = "0.1.0", features = ["bbr", "cubic", "platform-optimized", "simd"] }
  • bbr: 启用 BBR 算法(默认启用)
  • cubic: 启用 CUBIC 算法(默认启用)
  • platform-optimized: 启用平台特定优化
  • simd: 启用 SIMD 指令优化

示例

项目包含多个示例,展示不同的使用场景:

运行基础示例

cargo run --example basic_usage

展示基本的拥塞控制功能,包括:

  • 基本的数据包处理
  • 算法性能比较
  • 批量处理演示
  • 手动算法切换

运行高级场景示例

cargo run --example advanced_scenarios

演示高级功能,包括:

  • 多种网络场景模拟
  • 算法性能基准测试
  • 多连接并发处理
  • 真实流量模式模拟

运行性能对比示例

cargo run --example performance_comparison

比较不同算法在各种网络条件下的性能:

  • 高速光纤网络
  • 移动网络
  • 拥塞网络
  • 卫星连接

架构设计

核心组件

  • CongestionController: 主要的控制器接口
  • CongestionManager: 算法管理和切换逻辑
  • BBR/CUBIC: 具体的拥塞控制算法实现
  • Platform: 平台检测和优化
  • NetworkMetrics: 网络指标接口(由用户实现)
  • MetricsWindow: 指标窗口接口(由用户实现)

设计原则

  1. 纯计算: 不依赖系统调用,所有数据由调用方提供
  2. 零拷贝: 最小化内存分配和数据拷贝
  3. 平台无关: 统一接口,自动平台优化
  4. 可扩展: 易于添加新的拥塞控制算法
  5. 高性能: SIMD 优化和批处理支持

性能特点

BBR 算法优势

  • 在高带宽低延迟场景下表现优异
  • 能够充分利用可用带宽
  • 对网络变化响应迅速

CUBIC 算法优势

  • 在拥塞条件下表现稳定
  • 对丢包处理更加鲁棒
  • 在可变网络条件下性能可靠

自适应切换优势

  • 结合两种算法的优点
  • 根据网络条件自动选择最优策略
  • 提供最佳的整体性能

平台支持

  • macOS: 完全支持,包括 Apple Silicon 优化
  • Linux: 完全支持,包括 x86_64 和 ARM64
  • Windows: 完全支持,包括 SIMD 优化
  • 其他平台: 基础功能支持

注意事项

  1. 接口实现: 使用前需要实现 NetworkMetricsMetricsWindow 接口
  2. 数据提供: 所有网络数据需要由调用方提供
  3. 线程安全: 控制器不是线程安全的,需要外部同步
  4. 内存管理: 库采用零拷贝设计,注意数据生命周期
  5. 依赖: 仅依赖 fastrand 库用于模拟网络条件

版本信息

当前版本: 0.1.0

技术支持

如有问题或建议,请通过GitHub Issues联系开发团队。

许可证

本项目采用LGPL v3许可证。详见LICENSE文件。


RAT Congestion Control - 高性能网络拥塞控制库

Commit count: 0

cargo fmt