Files
3x-ui/internal/web/service/server_public_ip_async_test.go
T
Farhan Zare 95f19b192f fix(nodes): stop a restarting panel from reporting itself as down
Adding a node fails right after that node's panel restarts. nodes/add
probes the node's /panel/api/server/status first, and that endpoint
returns whatever the @2s ticker last sampled - nil until the first tick
lands, so the master reads a healthy panel as unreachable and rejects it
with "Add node (remote returned success=false: )", an error whose
message is empty because the node answered success with a null obj.

The window is far wider than one tick: GetStatus resolved the public
IPv4/IPv6 addresses inline and held s.mu across every lookup, so a box
with no IPv6 route spent 3s per service - about 15s of nil status after
each restart, and the same stall on a fresh panel's first sample.

- status now answers from CurrentStatus, which samples on demand when
  the ticker has not run yet instead of returning a null obj
- the public-IP lookups run in the background and outside s.mu, so a
  status sample never waits on them
- probe tells "no status yet" apart from a genuine success=false, so the
  master's error says something when it meets an older node
2026-09-18 13:25:24 +03:00

60 lines
1.6 KiB
Go

package service
import (
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
"time"
"github.com/mhsanaei/3x-ui/v3/internal/database"
)
// A box with no IPv6 route spends 3s per lookup service, and a status sample
// that waits for that is a panel reporting nothing for the first ~15s.
func TestStatusSampleDoesNotWaitOnPublicIPLookup(t *testing.T) {
dbDir := t.TempDir()
t.Setenv("XUI_DB_FOLDER", dbDir)
if err := database.InitDB(filepath.Join(dbDir, "x-ui.db")); err != nil {
t.Fatalf("InitDB: %v", err)
}
t.Cleanup(func() { _ = database.CloseDB() })
release := make(chan struct{})
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
select {
case <-release:
case <-r.Context().Done():
return
}
_, _ = w.Write([]byte("203.0.113.7"))
}))
t.Cleanup(srv.Close)
savedV4, savedV6 := publicIPv4Services, publicIPv6Services
publicIPv4Services = []string{srv.URL}
publicIPv6Services = []string{srv.URL}
t.Cleanup(func() { publicIPv4Services, publicIPv6Services = savedV4, savedV6 })
svc := &ServerService{}
status := svc.CurrentStatus()
if status == nil {
t.Fatal("CurrentStatus returned nil while the IP lookup was in flight")
}
if status.PublicIP.IPv4 != "" {
t.Fatalf("the sample waited for the lookup: PublicIP.IPv4 = %q, want it still unresolved", status.PublicIP.IPv4)
}
close(release)
deadline := time.Now().Add(10 * time.Second)
for {
if ipv4, _ := svc.publicIPs(); ipv4 == "203.0.113.7" {
return
}
if time.Now().After(deadline) {
t.Fatal("the background lookup never cached the public IP")
}
time.Sleep(20 * time.Millisecond)
}
}