feat: add optional sequential port allocation with atomic reservations

Adapt the port allocation option from #583 by xigua761 without its commercial licensing changes. Serialize forward, chain, peer runtime, cleanup and import reservations across SQLite/PostgreSQL instances.

Co-authored-by: xigua761 <xigua1166@outlook.com>
This commit is contained in:
sagit
2026-11-15 12:00:48 +08:00
parent 8339f52280
commit 48955fe57d
14 changed files with 773 additions and 201 deletions
@@ -0,0 +1,34 @@
package handler
import "testing"
func TestNormalizeSequentialPortConfigValue(t *testing.T) {
for in, want := range map[string]string{"true": "true", " FALSE ": "false"} {
got, err := normalizeAndValidateConfigValue(sequentialPortConfigKey, in)
if err != nil || got != want {
t.Fatalf("value %q: got %q err=%v, want %q", in, got, err, want)
}
}
if _, err := normalizeAndValidateConfigValue(sequentialPortConfigKey, "maybe"); err == nil {
t.Fatal("expected invalid value to be rejected")
}
}
func TestSequentialPortConfigReadsRepositoryAndDefaultsOff(t *testing.T) {
_, r := setupConfigAccessTestRouter(t)
h := &Handler{repo: r}
if h.isSequentialPortEnabled() {
t.Fatal("missing config must preserve random allocation")
}
for _, tc := range []struct {
value string
want bool
}{
{"true", true}, {" TRUE ", true}, {"false", false}, {"invalid", false},
} {
seedConfigValue(t, r, sequentialPortConfigKey, tc.value)
if got := h.isSequentialPortEnabled(); got != tc.want {
t.Fatalf("config %q: got %v, want %v", tc.value, got, tc.want)
}
}
}