Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

配置模型和结构模式

语言: English

配置入口

配置入口是 rust_supervisor::config::loader::load_config_state. 它只接受 YAML(数据序列化格式)主配置文件, 示例路径是 examples/config/supervisor.yaml.

当前配置形状包含四组数据: supervisor, policy, shutdownobservability. 它们分别进入 SupervisorRootConfig(监督器根配置), PolicyConfig(策略配置), ShutdownConfig(关闭配置) 和 ObservabilityConfig(可观测性配置).

配置状态

rust_supervisor::config::configurable::SupervisorConfig 是公开 root configuration struct(根配置结构体). 它支持 confique::Config(配置派生), schemars::JsonSchema(结构模式生成特征), Serialize(序列化) 和 Deserialize(反序列化). 使用者可以用同一个模型完成 YAML(数据序列化格式) 加载, template generation(模板生成) 和 schema generation(结构模式生成).

ConfigState(配置状态) 是校验后的不可变状态. 运行时不应该在其它模块里保存运行时可调常量.

ConfigState::to_supervisor_spec 会派生 SupervisorSpec(监督器规格). 当前实现用配置值填充 supervision strategy(监督策略),策略默认值,关闭预算,健康检查时间和可观测性容量.

模板边界

官方 template(模板) 是 examples/config/supervisor.template.yaml. 它默认保持单个 YAML(数据序列化格式) 文件, 并覆盖 supervisor, policy, shutdownobservability.

本 crate(包) 不会在公开配置结构体, 官方 schema(结构模式) 或官方 template(模板) 中添加 x-tree-split(树形拆分扩展). 如果使用者项目需要拆分配置文件, 可以在自己的项目中包装或复用 SupervisorConfig(监督器配置), 并自行决定 tree split layout(树形拆分布局).

错误边界

配置加载失败会返回 SupervisorError::FatalConfig. 这些情况会拒绝启动:

  • 配置文件不是 YAML(数据序列化格式).
  • 文件无法读取.
  • YAML(数据序列化格式)无法解析成 SupervisorConfig.
  • supervision strategy(监督策略) 不是 OneForOne, OneForAllRestForOne.
  • 数值为零.
  • 初始退避大于最大退避.
  • jitter(抖动)比例不在零到一之间.

Supervisor::start_from_config_file 会在创建 runtime channel(运行时通道) 或派生 control loop(控制循环) 之前拒绝非法配置.

示例配置

supervisor:
  strategy: OneForAll
policy:
  child_restart_limit: 10
  child_restart_window_ms: 60000
  supervisor_failure_limit: 30
  supervisor_failure_window_ms: 60000
  initial_backoff_ms: 100
  max_backoff_ms: 5000
  jitter_ratio: 0.10
  heartbeat_interval_ms: 1000
  stale_after_ms: 3000
shutdown:
  graceful_timeout_ms: 5000
  abort_wait_ms: 1000
observability:
  event_journal_capacity: 256
  metrics_enabled: true
  audit_enabled: true