fix: 修复写队列 busy 恢复、关闭安全、备份与配置构建问题

This commit is contained in:
Nixevol
2026-09-30 16:21:05 +08:00
parent 4059a1576b
commit 57c2f70678
23 changed files with 825 additions and 101 deletions
+8 -33
View File
@@ -12,6 +12,9 @@ import (
// MaxBodyBytesCap 是 max_body_bytes 的硬上限(DEVELOPMENT 11.1)。
const MaxBodyBytesCap = 262144
// MaxFrameBytesCap 是 max_frame_bytes 的硬上限,与 broker MaximumPacketSize 一致。
const MaxFrameBytesCap = 786432
// Config 对应 DEVELOPMENT 第 11.1 节的配置文件。
type Config struct {
Listen string `yaml:"listen"`
@@ -124,39 +127,8 @@ func applyEmptyDefaults(cfg *Config) {
if cfg.Log.Level == "" {
cfg.Log.Level = def.Log.Level
}
if cfg.Limits.MaxBodyBytes == 0 {
cfg.Limits.MaxBodyBytes = def.Limits.MaxBodyBytes
}
if cfg.Limits.MaxMetaBytes == 0 {
cfg.Limits.MaxMetaBytes = def.Limits.MaxMetaBytes
}
if cfg.Limits.MaxFrameBytes == 0 {
cfg.Limits.MaxFrameBytes = def.Limits.MaxFrameBytes
}
if cfg.Limits.MaxTTLSeconds == 0 {
cfg.Limits.MaxTTLSeconds = def.Limits.MaxTTLSeconds
}
if cfg.Limits.MaxScheduleSeconds == 0 {
cfg.Limits.MaxScheduleSeconds = def.Limits.MaxScheduleSeconds
}
if cfg.Limits.MaxGroupMembers == 0 {
cfg.Limits.MaxGroupMembers = def.Limits.MaxGroupMembers
}
if cfg.Limits.GraceSeconds == 0 {
cfg.Limits.GraceSeconds = def.Limits.GraceSeconds
}
if cfg.Limits.AckTimeoutSeconds == 0 {
cfg.Limits.AckTimeoutSeconds = def.Limits.AckTimeoutSeconds
}
if cfg.Limits.DeliveryWindow == 0 {
cfg.Limits.DeliveryWindow = def.Limits.DeliveryWindow
}
if cfg.Limits.ReceiptWindow == 0 {
cfg.Limits.ReceiptWindow = def.Limits.ReceiptWindow
}
if cfg.Limits.RequestsPerSecond == 0 {
cfg.Limits.RequestsPerSecond = def.Limits.RequestsPerSecond
}
// 数值字段不在这里把 0 改回默认:Load 已先填 Default 再解析 YAML,
// 未写的字段保留默认值;显式写 0 对 grace 等字段有意义,其余由 Validate 拒绝。
}
// Validate 校验 DEVELOPMENT 11.1 全部字段;拒绝 max_body_bytes > 262144。
@@ -189,6 +161,9 @@ func (c Config) Validate() error {
if c.Limits.MaxFrameBytes <= 0 {
errs = append(errs, "limits.max_frame_bytes must be > 0")
}
if c.Limits.MaxFrameBytes > MaxFrameBytesCap {
errs = append(errs, fmt.Sprintf("limits.max_frame_bytes must be <= %d", MaxFrameBytesCap))
}
if c.Limits.MaxFrameBytes < c.Limits.MaxBodyBytes {
errs = append(errs, "limits.max_frame_bytes must be >= limits.max_body_bytes")
}
+56
View File
@@ -48,6 +48,62 @@ func TestLoadAndValidate(t *testing.T) {
}
}
func TestLoadExplicitZeroGraceSeconds(t *testing.T) {
t.Parallel()
dir := t.TempDir()
path := filepath.Join(dir, "config.yaml")
body := "listen: \"127.0.0.1:0\"\ndata_dir: \"" + filepath.ToSlash(dir) + "\"\nlimits:\n grace_seconds: 0\n"
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
cfg, err := Load(path)
if err != nil {
t.Fatal(err)
}
if err := cfg.Validate(); err != nil {
t.Fatal(err)
}
if cfg.Limits.GraceSeconds != 0 {
t.Fatalf("grace_seconds=%d, want 0", cfg.Limits.GraceSeconds)
}
if cfg.Limits.MaxBodyBytes != MaxBodyBytesCap {
t.Fatalf("omitted max_body_bytes=%d", cfg.Limits.MaxBodyBytes)
}
}
func TestValidateRejectsMaxFrameBytesTooLarge(t *testing.T) {
t.Parallel()
cfg := Default()
cfg.Limits.MaxFrameBytes = MaxFrameBytesCap + 1
err := cfg.Validate()
if err == nil {
t.Fatal("expected error")
}
if !strings.Contains(err.Error(), "max_frame_bytes") {
t.Fatalf("unexpected error: %v", err)
}
}
func TestLoadExplicitZeroMaxBodyBytesRejected(t *testing.T) {
t.Parallel()
dir := t.TempDir()
path := filepath.Join(dir, "config.yaml")
body := "listen: \"127.0.0.1:0\"\ndata_dir: \"" + filepath.ToSlash(dir) + "\"\nlimits:\n max_body_bytes: 0\n"
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
t.Fatal(err)
}
cfg, err := Load(path)
if err != nil {
t.Fatal(err)
}
if cfg.Limits.MaxBodyBytes != 0 {
t.Fatalf("max_body_bytes=%d, want explicit 0", cfg.Limits.MaxBodyBytes)
}
if err := cfg.Validate(); err == nil || !strings.Contains(err.Error(), "max_body_bytes") {
t.Fatalf("want max_body_bytes error, got %v", err)
}
}
func TestValidateTrustedProxies(t *testing.T) {
t.Parallel()
cfg := Default()