3x-ui/internal/web/service/client_node_push_test.go

100 lines
3.9 KiB
Go
Raw Normal View History

perf(node): bound the per-client node push and fan out the traffic reset An operator with several nodes reported that editing a client or resetting its traffic takes more than ten seconds on the master. Measured against real Remote HTTP (fake node servers, one client per node), the healthy case is already fast — 3 nodes: update 51ms, delete 51ms; 5 nodes: 102ms / 103ms — but two things were not: - ResetTrafficByEmail still walked its inbounds one node round-trip after another: 152ms at 3 nodes, 253ms at 5, linear in node count. - Every per-client op blocked on the SLOWEST node's push. With one node answering in 3s, update/delete/reset all took 3003ms regardless of node count. A node that answers the 4s heartbeat probe but hangs on the push stays "online", so every edit waited on it up to remoteHTTPTimeout — the ten seconds in the report. More nodes only raise the odds one is sick. The push is an immediacy optimisation, not the source of truth: every one of these ops calls MarkNodeDirtyTx inside the transaction that commits the change, before it pushes, and the node reconcile job converges a dirty node on its next 5s tick by re-sending the inbound whose fingerprint was not advanced. So bound the synchronous push with nodeClientPushTimeout = 4s — the budget the heartbeat and traffic-sync jobs already treat as "responsive" — at the eight node-branch push sites. A node that does not answer in time is left dirty and converged a few seconds later instead of stalling the request; the tag-cache list fetch inside resolveRemoteID shares the same budget. Once one push in a batch has timed out, the rest of that inbound's batch now stops pushing too, as AddInboundClient already did: the node is dirty and one reconcile converges the whole inbound. Deleting three clients on one hung node went from 30.08s (three remote timeouts) to 4.06s; at the 32-client push threshold that is 128s of deadlines saved per inbound. Fan the reset out through fanoutInboundApplies like the other client ops. Its node propagation is still attempted whatever the node's status flag says, as before, because nothing replays a traffic reset — the reconcile pushes inbound config, not counters — so a node still serving after being marked offline must receive it now or never. Trade-offs stated plainly: a node that would have answered in 4–10s now falls to the reconcile's full-inbound push, which on the node is a delete+add of the inbound and drops its sessions there — the same fallback a failed 10s push already used, now reached sooner. The reset stays best-effort with no retry path, which predates this change. The response still reports success while a timed-out node catches up; the pending-node badge is keyed off node status by design, so only the warning log records it. Tests: a barrier test that a sequential reset cannot satisfy; two tests against a real runtime.Remote and an httptest node that hangs on the push, pinning that an edit returns at the deadline (exactly one push reached the node, the node is left dirty) and that a bulk delete stops after its first timed-out push. All red without the change; the two hung-node tests pay their 4s deadline on every run.
2026-09-07 05:24:14 -07:00
package service
import (
"testing"
"time"
"github.com/mhsanaei/3x-ui/v3/internal/database"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
)
// TestResetTrafficAcrossNodesPushesConcurrently covers the panel's per-client
// traffic reset, which propagated to nodes one round-trip after another.
func TestResetTrafficAcrossNodesPushesConcurrently(t *testing.T) {
setupBulkDB(t)
startSerializedWriter(t)
const nodes = inboundFanoutConcurrency + 1
const email = "reset@x"
bar := newApplyBarrier(inboundFanoutConcurrency)
seedClientAcrossNodes(t, bar, nodes, 46801, email, "88888888-1111-2222-3333-444444444444")
bar.arm()
if _, err := (&ClientService{}).ResetTrafficByEmail(&InboundService{}, email); err != nil {
t.Fatalf("ResetTrafficByEmail across %d node inbounds: %v", nodes, err)
}
if got := bar.maxPar.Load(); got != inboundFanoutConcurrency {
t.Fatalf("peak node pushes in flight = %d, want overlap at the %d cap (barrier timed out: %v)",
got, inboundFanoutConcurrency, bar.expired.Load())
}
}
// TestNodePushGivesUpBeforeTheRemoteTimeout pins that an edit gives up on a node
// that hangs on the push at the deadline, leaving it dirty for the reconcile.
func TestNodePushGivesUpBeforeTheRemoteTimeout(t *testing.T) {
setupBulkDB(t)
startSerializedWriter(t)
useTestRuntimeManager(t)
db := database.GetDB()
const uuid = "77777777-1111-2222-3333-444444444444"
f := newFakeNodeHTTP(t)
ib := realNodeInbound(t, f, 53100, []model.Client{{Email: "hung@x", ID: uuid, SubID: "sub-hung", Enable: true}})
rec := lookupClientRecord(t, "hung@x")
f.setHold(true)
start := time.Now()
if _, err := (&ClientService{}).Update(&InboundService{}, rec.Id, model.Client{
Email: "hung@x", ID: uuid, SubID: "sub-hung", Enable: true, Comment: "edited",
}, 0); err != nil {
t.Fatalf("Update against a hung node: %v", err)
}
elapsed := time.Since(start)
// Both bounds: no push at all would also finish fast and leave the node dirty.
if got := f.hitCount("/clients/update/"); got != 1 {
t.Fatalf("clients/update requests reaching the hung node = %d, want exactly 1", got)
}
if elapsed < nodeClientPushTimeout-200*time.Millisecond {
t.Fatalf("edit returned after %v, before the %v push deadline: the push was never awaited", elapsed, nodeClientPushTimeout)
}
if elapsed >= 2*nodeClientPushTimeout {
t.Fatalf("edit took %v against a hung node: it waited out the remote timeout instead of the %v push deadline", elapsed, nodeClientPushTimeout)
}
var node model.Node
if err := db.Where("id = ?", *ib.NodeID).First(&node).Error; err != nil {
t.Fatalf("read node: %v", err)
}
if !node.ConfigDirty {
t.Fatal("the node whose push timed out must stay dirty, or nothing ever converges it")
}
}
// TestBulkDeleteStopsPushingToAHungNode pins the batch circuit-break: once one
// push times out, the rest of the batch defers to the reconcile as well.
func TestBulkDeleteStopsPushingToAHungNode(t *testing.T) {
setupBulkDB(t)
startSerializedWriter(t)
useTestRuntimeManager(t)
f := newFakeNodeHTTP(t)
realNodeInbound(t, f, 53200, []model.Client{
{Email: "b1@x", ID: "66666666-1111-2222-3333-444444444441", SubID: "sub-b1", Enable: true},
{Email: "b2@x", ID: "66666666-1111-2222-3333-444444444442", SubID: "sub-b2", Enable: true},
{Email: "b3@x", ID: "66666666-1111-2222-3333-444444444443", SubID: "sub-b3", Enable: true},
})
f.setHold(true)
start := time.Now()
if _, _, err := (&ClientService{}).BulkDelete(&InboundService{}, []string{"b1@x", "b2@x", "b3@x"}, false); err != nil {
t.Fatalf("BulkDelete against a hung node: %v", err)
}
elapsed := time.Since(start)
if got := f.hitCount("/clients/del/"); got != 1 {
t.Fatalf("clients/del requests sent to the hung node = %d, want 1: the batch kept paying a deadline per client", got)
}
if elapsed >= 2*nodeClientPushTimeout {
t.Fatalf("deleting 3 clients took %v against a hung node, want a single %v deadline, not one per client", elapsed, nodeClientPushTimeout)
}
}