settings

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 23, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package settings 见 spec.go 顶部注释。

Package settings 定义 master 与 agent 共享的全局同步配置 schema。

单一职责:AgentSettings struct 是所有"会从 master 同步到 agent 内存"配置项的 source of truth。加新同步配置 = 给 struct 加一个字段配 tag,master 端的 default seed / 校验、agent 端的 apply / snapshot 都通过反射自动生效。

这个包不依赖 models / dao / events,任何模块都能 import,无循环风险。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(s *AgentSettings, key, value string) error

Apply 用 key/value 字符串更新 struct 字段,带 min/max 校验。

  • 未知 key: 返回 nil(forward-compat)
  • 解析失败: 返回 *ParseError
  • 越界: 返回 *RangeError

func Defaults

func Defaults() map[string]string

Defaults 返回 AgentSettings 所有 setting tag 的默认值(key -> string)。 master 启动 seed DB 时调用;若 DB 内 key 已存在则不覆盖。

func Keys

func Keys() []string

Keys 返回所有声明的 setting key,顺序按 struct 字段声明顺序。

func Validate

func Validate(key, value string) error

Validate 试运行一次 Apply 但不修改任何状态,供 master UpdateSettings 在写 DB 前 校验入参用。未知 key 返回 nil。

Types

type AgentSettings

type AgentSettings struct {
	TraceMaxBodySize int `setting:"trace_max_body_size,65536,4096,16777216"`
	FallbackSleepMs  int `setting:"fallback_sleep_ms,1000,0,60000"`
}

AgentSettings 是 agent 从 master 同步过来的全局设置快照。

字段添加规则:

  • 字段类型: int(其他类型需扩展 reflect.go 的 parse/assign 分支)
  • tag 格式: setting:"<key>,<default>,<min>,<max>"
  • key 在整个 struct 内唯一

该 struct 通过 atomic.Pointer 在 agent cache.Store 内保存当前快照, 调用方走 cache.Settings() 拿到 value copy(immutable,无需锁)。

type ParseError

type ParseError struct {
	Key   string
	Value string
	Cause error
}

ParseError 表示无法把 string value 转成 struct field 类型。

func (*ParseError) Error

func (e *ParseError) Error() string

func (*ParseError) Unwrap

func (e *ParseError) Unwrap() error

type RangeError

type RangeError struct {
	Key   string
	Value int64
	Min   int64
	Max   int64
}

RangeError 表示 value 越过了 tag 上声明的 min/max 区间。

func (*RangeError) Error

func (e *RangeError) Error() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL