cocoon/throttle.h
xfy911 522685e55b
Some checks failed
CI / build (push) Failing after 1m0s
feat: 实现带宽限速 / 流量整形(Token Bucket 算法)
- 新增 throttle.c/throttle.h:Token Bucket 双层限速实现
  - 支持 per-connection(单连接限速)和 global(服务器总带宽限速)
  - 使用 fd 映射表自动注册/清理,侵入性最小
- cocoon.h: 配置结构体新增 throttle_conn_rate、throttle_global_rate
- server.c: 连接创建时自动初始化 per-connection throttle;server_destroy 释放 global throttle
- static.c: send_all() 自动查表限速
- platform.c: cocoon_file_send() 限速启用时回退到 read+write 循环,逐 chunk 限速
- proxy.c: send_all_fd() 加入限速
- config.c: 解析 JSON 配置中的 throttle 对象(conn_rate / global_rate)
- config.h: 更新 config_merge 签名
- Makefile: 将 throttle.c 加入 SRCS 和所有单元测试编译规则

编译零警告,109 项集成测试通过(6 个失败为已知环境反向代理 404 问题)
2026-06-16 15:41:24 +08:00

78 lines
2.2 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* throttle.h - 带宽限速模块
*
* Token Bucket 算法实现,支持 per-connection 和全局总限速。
* 通过 fd 注册/查询机制,在 send_all 和 cocoon_file_send 中自动应用限速。
*
* @author xfy
*/
#ifndef COCOON_THROTTLE_H
#define COCOON_THROTTLE_H
#include <stdint.h>
#include <stddef.h>
#include <time.h>
/**
* cocoon_throttle_t - Token Bucket 限速器
*
* 基于令牌桶算法,控制每秒发送的字节数。
* rate: 每秒补充的令牌数bytes/sec
* burst: 桶的最大容量bytes允许突发传输
*/
typedef struct {
uint64_t rate_bytes_per_sec; /**< 每秒速率bytes/sec */
uint64_t burst_bytes; /**< 突发容量bytes */
uint64_t tokens; /**< 当前桶中令牌数 */
struct timespec last_update; /**< 上次更新时间 */
} cocoon_throttle_t;
/**
* throttle_init - 初始化限速器
*
* @param t 限速器指针
* @param rate_bytes_per_sec 每秒速率0 表示不限制)
* @param burst_bytes 突发容量0 表示使用 rate 作为 burst
*/
void throttle_init(cocoon_throttle_t *t, uint64_t rate_bytes_per_sec, uint64_t burst_bytes);
/**
* throttle_consume - 从桶中消费 N 字节
*
* 根据当前时间和上次更新时间计算应补充的令牌数,
* 然后尝试消费指定字节数。如果令牌不足,返回需要等待的微秒数。
*
* @param t 限速器指针
* @param bytes 需要消费的字节数
* @return 需要等待的微秒数0 表示无需等待,可直接发送)
*/
uint64_t throttle_consume(cocoon_throttle_t *t, size_t bytes);
/**
* throttle_set_fd - 为 fd 注册限速器
*
* 在 send_all / cocoon_file_send 中会自动查找并应用限速。
*
* @param fd socket 文件描述符
* @param t 限速器指针NULL 表示清除)
*/
void throttle_set_fd(int fd, cocoon_throttle_t *t);
/**
* throttle_clear_fd - 清除 fd 的限速器
*
* @param fd socket 文件描述符
*/
void throttle_clear_fd(int fd);
/**
* throttle_lookup - 查询 fd 的限速器
*
* @param fd socket 文件描述符
* @return 限速器指针NULL 表示无限制
*/
cocoon_throttle_t *throttle_lookup(int fd);
#endif /* COCOON_THROTTLE_H */