248 lines
9.8 KiB
Go
248 lines
9.8 KiB
Go
// Package config 提供 go-sms 平台统一配置模型与加载能力(Viper + 环境变量覆盖)。
|
|
// 对应文件:configs/config.dev.yaml;环境变量规则见 loader.go。
|
|
package config
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// Config 平台总配置根结构,与 configs/config.*.yaml 一一对应。
|
|
type Config struct {
|
|
// Source 配置文件来源路径(运行时自动填充,yaml 中无需定义)。
|
|
Source string `mapstructure:"-"`
|
|
Server Server `mapstructure:"server"`
|
|
Log Log `mapstructure:"log"`
|
|
MySQL MySQL `mapstructure:"mysql"`
|
|
Redis Redis `mapstructure:"redis"`
|
|
Kafka Kafka `mapstructure:"kafka"`
|
|
ETCD ETCD `mapstructure:"etcd"`
|
|
ClickHouse ClickHouse `mapstructure:"clickhouse"`
|
|
MinIO MinIO `mapstructure:"minio"`
|
|
Tracer Tracer `mapstructure:"tracer"`
|
|
JWT JWT `mapstructure:"jwt"`
|
|
RateLimit RateLimit `mapstructure:"rate_limit"`
|
|
HotReload HotReload `mapstructure:"hot_reload"`
|
|
Balance Balance `mapstructure:"balance"`
|
|
Settle Settle `mapstructure:"settle"`
|
|
}
|
|
|
|
// Settle 结算域配置(S2):消耗日汇总滚动重刷模型——周期重刷近 N 天
|
|
// (默认 4 天:72h 回执窗内明细状态会迁移 UNKNOWN→成功/失败,昨日行非终值,
|
|
// 须滚动收敛;月结建议 T+4 后生成)。零值兜底见 service/settle/job.go。
|
|
type Settle struct {
|
|
RefreshEvery time.Duration `mapstructure:"refresh_every"` // 重刷周期(默认 1h)
|
|
RefreshDays int `mapstructure:"refresh_days"` // 每轮重刷天数窗(默认 4=today-3..today)
|
|
BackfillDays int `mapstructure:"backfill_days"` // 启动回补天数(默认 7,明细热窗 5 天内余量)
|
|
}
|
|
|
|
// Balance 资金预扣模式(Day54 R1,评审 docs/review/r1-balance.md):
|
|
// mysql=行锁强一致(默认,回退路径);redis=Lua 原子扣+异步流水+对账覆写。
|
|
type Balance struct {
|
|
Mode string `mapstructure:"mode"` // mysql | redis
|
|
ReconcileInterval time.Duration `mapstructure:"reconcile_interval"` // 对账周期(评审 ≤5min)
|
|
}
|
|
|
|
// HotReload 配置热更新(Day 12:ETCD watch)。
|
|
type HotReload struct {
|
|
Enabled bool `mapstructure:"enabled"`
|
|
Prefix string `mapstructure:"prefix"` // 如 /config/go-sms/
|
|
}
|
|
|
|
// JWT 鉴权令牌配置(Day 22)。
|
|
type JWT struct {
|
|
Secret string `mapstructure:"secret"`
|
|
TTL time.Duration `mapstructure:"ttl"`
|
|
}
|
|
|
|
// Tracer 链路追踪配置(OTLP → Jaeger,Day 9)。
|
|
type Tracer struct {
|
|
Enabled bool `mapstructure:"enabled"`
|
|
OTLPEndpoint string `mapstructure:"otlp_endpoint"` // 形如 127.0.0.1:4318(OTLP HTTP)
|
|
ServiceName string `mapstructure:"service_name"`
|
|
}
|
|
|
|
// RateLimit 分布式限流配置(Redis 令牌桶,Day 9)。
|
|
type RateLimit struct {
|
|
Enabled bool `mapstructure:"enabled"`
|
|
Capacity int64 `mapstructure:"capacity"` // 桶容量(突发)
|
|
RatePerSec int64 `mapstructure:"rate_per_sec"` // 每秒填充
|
|
Key string `mapstructure:"key"` // ip | global
|
|
}
|
|
|
|
// Server 服务进程监听配置(HTTP API / CMPP+SMPP 客户接入)。
|
|
type Server struct {
|
|
Name string `mapstructure:"name"`
|
|
HTTP HTTPServer `mapstructure:"http"`
|
|
CMPP CMPPServer `mapstructure:"cmpp"`
|
|
SMPP SMPPServer `mapstructure:"smpp"`
|
|
}
|
|
|
|
// HTTPServer HTTP API 监听配置。
|
|
type HTTPServer struct {
|
|
Addr string `mapstructure:"addr"`
|
|
ReadTimeout time.Duration `mapstructure:"read_timeout"`
|
|
WriteTimeout time.Duration `mapstructure:"write_timeout"`
|
|
}
|
|
|
|
// CMPPServer CMPP 服务端监听配置(Day 47 客户接入)。
|
|
type CMPPServer struct {
|
|
Addr string `mapstructure:"addr"`
|
|
MaxConnsPerAccount int `mapstructure:"max_connections_per_account"`
|
|
IdleTimeout time.Duration `mapstructure:"idle_timeout"`
|
|
WindowSize int `mapstructure:"window_size"`
|
|
SubmitRatePerConn int `mapstructure:"submit_rate_per_conn"` // Day 62:单链限速 条/s(账号级 0 时回退值)
|
|
LongSmsWindow time.Duration `mapstructure:"longsms_window"` // Day 71:入向长短信挂起窗(默认 72h,到期不齐失败终态)
|
|
}
|
|
|
|
// SMPPServer SMPP 服务端监听配置(Day 47 客户接入;addr 空=不启用)。
|
|
type SMPPServer struct {
|
|
Addr string `mapstructure:"addr"`
|
|
IdleTimeout time.Duration `mapstructure:"idle_timeout"`
|
|
}
|
|
|
|
// Log 结构化日志配置(zap + lumberjack,Day 5 实现)。
|
|
type Log struct {
|
|
Level string `mapstructure:"level"` // debug | info | warn | error
|
|
Format string `mapstructure:"format"` // json | console
|
|
Output []string `mapstructure:"output"` // stdout | file
|
|
File LogFile `mapstructure:"file"`
|
|
}
|
|
|
|
// LogFile 日志文件滚动切割配置。
|
|
type LogFile struct {
|
|
Path string `mapstructure:"path"`
|
|
MaxSizeMB int `mapstructure:"max_size_mb"`
|
|
MaxAgeDays int `mapstructure:"max_age_days"`
|
|
MaxBackups int `mapstructure:"max_backups"`
|
|
Compress bool `mapstructure:"compress"`
|
|
}
|
|
|
|
// MySQL 业务主库配置(用户/通道/余额/结算等事务数据)。
|
|
type MySQL struct {
|
|
DSN string `mapstructure:"dsn"`
|
|
MaxOpenConns int `mapstructure:"max_open_conns"`
|
|
MaxIdleConns int `mapstructure:"max_idle_conns"`
|
|
ConnMaxLifetime time.Duration `mapstructure:"conn_max_lifetime"`
|
|
SlowThresholdMs int `mapstructure:"slow_threshold_ms"`
|
|
}
|
|
|
|
// Redis 缓存/分布式锁/限流配置。
|
|
type Redis struct {
|
|
Mode string `mapstructure:"mode"` // single | cluster | sentinel
|
|
Addrs []string `mapstructure:"addrs"`
|
|
Password string `mapstructure:"password"`
|
|
DB int `mapstructure:"db"`
|
|
PoolSize int `mapstructure:"pool_size"`
|
|
MasterName string `mapstructure:"master_name"` // sentinel 模式使用
|
|
}
|
|
|
|
// Kafka 消息队列配置(发送/回执/上行/统计四类主题)。
|
|
type Kafka struct {
|
|
Brokers []string `mapstructure:"brokers"`
|
|
Topics map[string]string `mapstructure:"topics"`
|
|
GroupID string `mapstructure:"group_id"`
|
|
MaxRetry int `mapstructure:"max_retry"`
|
|
Consumer Consumer `mapstructure:"consumer"` // Day 48:submit 消费域
|
|
}
|
|
|
|
// Consumer submit 消费者配置(Day 48;零值全部缺省)。
|
|
type Consumer struct {
|
|
GroupID string `mapstructure:"group_id"` // 消费组(空=go-sms-consumer)
|
|
ReclaimEvery time.Duration `mapstructure:"reclaim_every"` // 补偿扫描周期(默认 5s)
|
|
ReclaimBatch int `mapstructure:"reclaim_batch"` // 单轮认领上限(默认 100)
|
|
BPSampleEvery time.Duration `mapstructure:"bp_sample_every"` // 背压采样周期(默认 10s)
|
|
Enable bool `mapstructure:"enable"` // 背压/限速总开关
|
|
OffsetMode string `mapstructure:"offset_mode"` // Day58:起步 offset oldest(默认)|latest;仅新组/offset 重置后生效(env GOSMS_KAFKA_CONSUMER_OFFSET_MODE)
|
|
PaceEvery int `mapstructure:"pace_every"` // Day58:每成功消费 N 条歇一次(0=不限速)
|
|
PaceSleep time.Duration `mapstructure:"pace_sleep"` // Day58:每次歇时长(配合 pace_every;dev 存量回放防落库风暴)
|
|
Workers int `mapstructure:"workers"` // Day65:每分区并行 worker 数(默认 0/1=串行;submit 域慢事务链提吞吐)
|
|
}
|
|
|
|
// ETCD 配置中心 / 服务发现。
|
|
type ETCD struct {
|
|
Endpoints []string `mapstructure:"endpoints"`
|
|
DialTimeout time.Duration `mapstructure:"dial_timeout"`
|
|
Username string `mapstructure:"username"`
|
|
Password string `mapstructure:"password"`
|
|
}
|
|
|
|
// ClickHouse 历史明细与统计库。
|
|
type ClickHouse struct {
|
|
Addrs []string `mapstructure:"addrs"`
|
|
Database string `mapstructure:"database"`
|
|
Username string `mapstructure:"username"`
|
|
Password string `mapstructure:"password"`
|
|
DialTimeout time.Duration `mapstructure:"dial_timeout"`
|
|
}
|
|
|
|
// MinIO 对象存储(号码文件/报表/检测结果)。
|
|
type MinIO struct {
|
|
Endpoint string `mapstructure:"endpoint"`
|
|
AccessKey string `mapstructure:"access_key"`
|
|
SecretKey string `mapstructure:"secret_key"`
|
|
Bucket string `mapstructure:"bucket"`
|
|
UseSSL bool `mapstructure:"use_ssl"`
|
|
}
|
|
|
|
// Validate 对关键配置项做基础校验,避免进程带病启动。
|
|
func (c *Config) Validate() error {
|
|
if c.Server.HTTP.Addr == "" {
|
|
return errors.New("server.http.addr 不能为空")
|
|
}
|
|
if c.MySQL.DSN == "" {
|
|
return errors.New("mysql.dsn 不能为空")
|
|
}
|
|
switch c.Redis.Mode {
|
|
case "single", "cluster", "sentinel":
|
|
default:
|
|
return fmt.Errorf("redis.mode 非法: %q(可选 single/cluster/sentinel)", c.Redis.Mode)
|
|
}
|
|
if len(c.Redis.Addrs) == 0 {
|
|
return errors.New("redis.addrs 不能为空")
|
|
}
|
|
if len(c.Kafka.Brokers) == 0 {
|
|
return errors.New("kafka.brokers 不能为空")
|
|
}
|
|
for _, key := range []string{"submit", "report", "up", "stats"} {
|
|
if c.Kafka.Topics[key] == "" {
|
|
return fmt.Errorf("kafka.topics.%s 不能为空", key)
|
|
}
|
|
}
|
|
if len(c.ETCD.Endpoints) == 0 {
|
|
return errors.New("etcd.endpoints 不能为空")
|
|
}
|
|
if len(c.ClickHouse.Addrs) == 0 {
|
|
return errors.New("clickhouse.addrs 不能为空")
|
|
}
|
|
if c.MinIO.Endpoint == "" {
|
|
return errors.New("minio.endpoint 不能为空")
|
|
}
|
|
if c.Tracer.Enabled && c.Tracer.OTLPEndpoint == "" {
|
|
return errors.New("tracer.enabled 时 otlp_endpoint 不能为空")
|
|
}
|
|
if c.RateLimit.Enabled {
|
|
if c.RateLimit.Capacity <= 0 || c.RateLimit.RatePerSec <= 0 {
|
|
return errors.New("rate_limit.capacity / rate_per_sec 必须大于 0")
|
|
}
|
|
switch c.RateLimit.Key {
|
|
case "ip", "global":
|
|
default:
|
|
return fmt.Errorf("rate_limit.key 非法: %q(可选 ip/global)", c.RateLimit.Key)
|
|
}
|
|
}
|
|
if c.JWT.Secret == "" {
|
|
return errors.New("jwt.secret 不能为空")
|
|
}
|
|
if c.HotReload.Enabled && c.HotReload.Prefix == "" {
|
|
return errors.New("hot_reload.enabled 时 prefix 不能为空")
|
|
}
|
|
switch c.Log.Level {
|
|
case "debug", "info", "warn", "error":
|
|
default:
|
|
return fmt.Errorf("log.level 非法: %q(可选 debug/info/warn/error)", c.Log.Level)
|
|
}
|
|
return nil
|
|
}
|