cocoon/config.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

71 lines
2.6 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.

/**
* config.h - JSON 配置文件解析模块
*
* 轻量级配置加载,支持从 JSON 文件读取服务器配置。
* 保持零依赖,只解析 cocoon 需要的字段。
*
* @author xfy
*/
#ifndef COCOON_CONFIG_H
#define COCOON_CONFIG_H
#include "cocoon.h"
#include <stdbool.h>
/**
* config_load_from_file - 从 JSON 配置文件加载配置
*
* 解析 JSON 文件,填充 cocoon_config_t 结构体。
* 只解析存在的字段,不存在的保持默认值。
*
* @param path JSON 文件路径
* @param config 输出配置结构体
* @return true 成功false 失败(文件不存在或格式错误)
*/
bool config_load_from_file(const char *path, cocoon_config_t *config);
/**
* config_validate - 校验配置是否合法
*
* 在热重载前调用,确保新配置不会导致服务异常。
* 校验失败时返回 false 并写入错误信息到 err_buf。
*
* @param config 待校验的配置
* @param err_buf 错误信息缓冲区(可为 NULL
* @param err_size 缓冲区大小
* @return true 合法false 不合法
*/
bool config_validate(const cocoon_config_t *config, char *err_buf, size_t err_size);
/**
* config_merge - 用命令行配置覆盖文件配置
*
* 优先级:命令行 > 配置文件 > 硬编码默认值
* 命令行参数中显式指定的值(非默认值)会覆盖配置文件。
*
* @param base 从文件加载的基础配置
* @param cmdline 命令行解析出的配置
* @param has_root_dir 命令行是否指定了 -r
* @param has_port 命令行是否指定了 -p
* @param has_workers 命令行是否指定了 -w
* @param has_max_conn 命令行是否指定了 -m
* @param has_timeout 命令行是否指定了 -o
* @param has_log_level 命令行是否指定了 -l
*/
void config_merge(cocoon_config_t *base, const cocoon_config_t *cmdline,
bool has_root_dir, bool has_port, bool has_workers,
bool has_max_conn, bool has_timeout, bool has_log_level,
bool has_gzip_enabled, bool has_brotli_enabled,
bool has_tls_cert, bool has_tls_key, bool has_tls_enabled,
bool has_access_log,
bool has_cors_enabled, bool has_auth_user, bool has_auth_pass,
bool has_rate_limit,
bool has_throttle_conn_rate, bool has_throttle_global_rate,
bool has_plugins,
bool has_cache_enabled, bool has_cache_max_size,
bool has_cache_ttl_seconds, bool has_cache_max_entry_size,
bool has_acme_enabled);
#endif /* COCOON_CONFIG_H */