2026-04-25 01:18:30 +08:00

77 lines
2.8 KiB
Go
Raw 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.

// Package config 定义 room-service 的启动配置和 YAML 加载入口。
package config
import (
"time"
"hyapp/pkg/configx"
)
// Config 描述 room-service 进程启动所需的最小配置。
type Config struct {
// NodeID 是当前 room-service 实例标识Redis lease owner 使用它。
NodeID string `yaml:"node_id"`
// GRPCAddr 是内部 gRPC 监听地址gateway-service 和 im-service 都通过它访问 room-service。
GRPCAddr string `yaml:"grpc_addr"`
// LeaseTTL 是 room owner 租约有效期,节点故障后必须等它过期才能接管。
LeaseTTL time.Duration `yaml:"lease_ttl"`
// RankLimit 是房间本地礼物榜 top N。
RankLimit int `yaml:"rank_limit"`
// SnapshotEveryN 控制按房间版本保存快照的频率。
SnapshotEveryN int64 `yaml:"snapshot_every_n"`
// WalletServiceAddr 是 SendGift 同步扣费 gRPC 地址。
WalletServiceAddr string `yaml:"wallet_service_addr"`
// IMServiceAddr 是 PublishRoomEvent 同步广播 gRPC 地址。
IMServiceAddr string `yaml:"im_service_addr"`
// MySQLDSN 是 rooms、snapshot、command log 和 outbox 的持久化连接串。
MySQLDSN string `yaml:"mysql_dsn"`
// MySQLMaxOpenConns 控制 MySQL 最大打开连接数。
MySQLMaxOpenConns int `yaml:"mysql_max_open_conns"`
// MySQLMaxIdleConns 控制 MySQL 空闲连接数。
MySQLMaxIdleConns int `yaml:"mysql_max_idle_conns"`
// MySQLAutoMigrate 控制启动时是否自动建表。
MySQLAutoMigrate bool `yaml:"mysql_auto_migrate"`
// RedisAddr 是 room owner lease 目录地址。
RedisAddr string `yaml:"redis_addr"`
// RedisPassword 是 Redis 鉴权密码,本地默认留空。
RedisPassword string `yaml:"redis_password"`
// RedisDB 是 Redis logical DB。
RedisDB int `yaml:"redis_db"`
}
// Default 返回适合本地开发的默认配置。
func Default() Config {
// 默认端口遵守项目 13xxx 约束MySQL/Redis 指向本地 Docker 暴露端口。
return Config{
NodeID: "room-node-local",
GRPCAddr: ":13001",
LeaseTTL: 10 * time.Second,
RankLimit: 20,
SnapshotEveryN: 1,
WalletServiceAddr: "127.0.0.1:13004",
IMServiceAddr: "127.0.0.1:13003",
MySQLDSN: "hyapp:hyapp@tcp(127.0.0.1:13306)/hyapp_room?parseTime=true&charset=utf8mb4&loc=Local",
MySQLMaxOpenConns: 20,
MySQLMaxIdleConns: 10,
MySQLAutoMigrate: true,
RedisAddr: "127.0.0.1:13379",
RedisDB: 0,
}
}
// Load 从独立 config.yaml 读取 room-service 配置。
func Load(path string) (Config, error) {
cfg := Default()
if path == "" {
// 空路径代表显式使用默认配置,便于单元测试和最小启动。
return cfg, nil
}
if err := configx.LoadYAML(path, &cfg); err != nil {
// YAML 解析失败返回空 Config避免调用方误用部分默认值启动。
return Config{}, err
}
return cfg, nil
}