Files
mirror-3x-ui/internal/web/service/node_frozen_dip_test.go
T
Sanaei 408fd682f5 fix(node): count a restarted node counter once while its client is frozen
While a client's lifecycle was frozen (node config_dirty, a push that just landed, a reset the node still owes, or a client the node only reports usage for), a node counter that fell below the stored baseline left that baseline untouched. The other field's growth was still added to the client, and because the baseline never moved the same amount was added again on every sync tick: an idle client gained gigabytes per minute and was cut off. A client whose counters stayed below the old baseline had the opposite problem, and none of its usage was counted.

The baseline was held back on purpose (#6228): the dip is the evidence that the node renewed the client, and it had to survive until an unfrozen tick could judge it. The baseline was doing two jobs. It now always follows the node, so a delta is added once, and the unjudged dip is kept in a new node_client_traffics.dipped_while_frozen column that the first unfrozen tick consumes.

A renewal seen while frozen is now also adopted when the client's usage has since outgrown the pre-renewal baseline; before, that evidence was lost.

Usage already over-counted cannot be recovered and needs a manual reset. A baseline that is stuck at upgrade time adds its delta one last time on the first sync and is correct from then on.
2026-11-29 11:31:20 +03:30

94 lines
3.8 KiB
Go

package service
import (
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
"github.com/mhsanaei/3x-ui/v3/internal/web/runtime"
"github.com/mhsanaei/3x-ui/v3/internal/xray"
)
func syncDirtyNode(t *testing.T, svc *InboundService, nodeID int, tag, settings string, stats ...xray.ClientTraffic) {
t.Helper()
snap := &runtime.TrafficSnapshot{
Inbounds: []*model.Inbound{{Tag: tag, Settings: settings, ClientStats: stats}},
}
if _, err := svc.setRemoteTrafficLocked(nodeID, snap, true, false); err != nil {
t.Fatalf("dirty setRemoteTrafficLocked node %d: %v", nodeID, err)
}
}
// A node counter that restarted below the baseline while the node is dirty must
// add the other field's growth once, not again on every tick.
func TestNodeFrozenDip_IdleClientCountedOnce(t *testing.T) {
db := initTrafficTestDB(t)
svc := &InboundService{}
const email = "idle@x"
seedNodeRow(t, db, &model.Node{Id: 1, Name: "n1", Address: "127.0.0.1", Port: 2096, ApiToken: "tok", Enable: true})
createNodeInboundWithClient(t, db, 1, "n1-in", 41001, email)
settings := `{"clients":[{"email":"idle@x","enable":true}]}`
for range 2 {
syncNodeWithSettings(t, svc, 1, "n1-in", settings, xray.ClientTraffic{Email: email, Up: 2000, Down: 3000, Enable: true})
}
before := readTraffic(t, db, email)
for range 3 {
syncDirtyNode(t, svc, 1, "n1-in", settings, xray.ClientTraffic{Email: email, Up: 100, Down: 18000, Enable: true})
}
assertUpDown(t, readTraffic(t, db, email), before.Up, before.Down+15000, "after three dirty ticks of an idle client")
}
// Usage a client makes below the pre-restart baseline while the node is dirty
// must still be counted.
func TestNodeFrozenDip_UsageBelowOldBaselineCounted(t *testing.T) {
db := initTrafficTestDB(t)
svc := &InboundService{}
const email = "active@x"
seedNodeRow(t, db, &model.Node{Id: 1, Name: "n1", Address: "127.0.0.1", Port: 2096, ApiToken: "tok", Enable: true})
createNodeInboundWithClient(t, db, 1, "n1-in", 41001, email)
settings := `{"clients":[{"email":"active@x","enable":true}]}`
for range 2 {
syncNodeWithSettings(t, svc, 1, "n1-in", settings, xray.ClientTraffic{Email: email, Up: 5000, Down: 5000, Enable: true})
}
before := readTraffic(t, db, email)
for _, used := range []int64{100, 1000, 4000} {
syncDirtyNode(t, svc, 1, "n1-in", settings, xray.ClientTraffic{Email: email, Up: used, Down: used, Enable: true})
}
assertUpDown(t, readTraffic(t, db, email), before.Up+3900, before.Down+3900, "after the restarted counter grew to 4000")
}
// A node-side renewal seen while dirty must still be adopted on the first clean
// tick, even after the client's usage outgrew the pre-renewal baseline.
func TestNodeFrozenDip_RenewalSurvivesUsagePastOldBaseline(t *testing.T) {
db := initTrafficTestDB(t)
createNodeInbound(t, db, 1, "n1-in", 41001)
svc := &InboundService{}
const email = "renew-then-use"
syncNode(t, svc, 1, "n1-in", xray.ClientTraffic{
Email: email, Up: 100, Down: 100, ExpiryTime: earlyAbs, Enable: true, Reset: 30,
})
if err := db.Model(xray.ClientTraffic{}).Where("email = ?", email).
Updates(map[string]any{"expiry_time": earlyAbs, "enable": true, "up": int64(100), "down": int64(100)}).Error; err != nil {
t.Fatalf("seed master: %v", err)
}
renewed := xray.ClientTraffic{Email: email, Up: 10, Down: 10, ExpiryTime: lateAbs, Enable: true, Reset: 30}
syncDirtyNode(t, svc, 1, "n1-in", "", renewed)
renewed.Up, renewed.Down = 500, 500
syncDirtyNode(t, svc, 1, "n1-in", "", renewed)
if got := readTraffic(t, db, email).ExpiryTime; got != earlyAbs {
t.Fatalf("renewal while dirty raised expiry: got %d want %d", got, earlyAbs)
}
syncNode(t, svc, 1, "n1-in", renewed)
if got := readTraffic(t, db, email).ExpiryTime; got != lateAbs {
t.Fatalf("renewal after the dirty flag cleared did not apply: got %d want %d", got, lateAbs)
}
}