Files
mirror-3x-ui/internal/database/node_client_traffic_dip_migration_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

53 lines
1.8 KiB
Go

package database
import (
"testing"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
)
func TestMigrateNodeClientTrafficDippedWhileFrozenColumn(t *testing.T) {
initMigrateDB(t)
tx := GetDB().Begin()
if tx.Error != nil {
t.Fatal(tx.Error)
}
t.Cleanup(func() { tx.Rollback() })
// Recreate the previous schema in this transaction on SQLite or PostgreSQL.
if err := tx.Exec("ALTER TABLE node_client_traffics DROP COLUMN dipped_while_frozen").Error; err != nil {
t.Fatal(err)
}
const email = "legacy-baseline@x"
if err := tx.Exec("INSERT INTO node_client_traffics (node_id, email, up, down) VALUES (7, ?, 30, 50)", email).Error; err != nil {
t.Fatal(err)
}
if err := migrateNodeClientTrafficDippedWhileFrozenColumn(tx); err != nil {
t.Fatal(err)
}
var stored model.NodeClientTraffic
if err := tx.Where("email = ?", email).First(&stored).Error; err != nil {
t.Fatal(err)
}
if stored.NodeId != 7 || stored.Up != 30 || stored.Down != 50 || stored.DippedWhileFrozen {
t.Fatalf("legacy baseline after migration: node=%d up=%d down=%d dipped=%t, want 7/30/50/false",
stored.NodeId, stored.Up, stored.Down, stored.DippedWhileFrozen)
}
var nulls int64
if err := tx.Raw("SELECT COUNT(*) FROM node_client_traffics WHERE dipped_while_frozen IS NULL").Scan(&nulls).Error; err != nil {
t.Fatal(err)
}
if nulls != 0 {
t.Fatalf("migration left %d NULL dipped_while_frozen cell(s), want 0", nulls)
}
if err := tx.Model(&stored).Update("dipped_while_frozen", true).Error; err != nil {
t.Fatal(err)
}
if err := migrateNodeClientTrafficDippedWhileFrozenColumn(tx); err != nil {
t.Fatalf("repeat migration: %v", err)
}
if err := tx.First(&stored, stored.Id).Error; err != nil || !stored.DippedWhileFrozen {
t.Fatalf("repeat migration lost the pending dip: dipped=%t, err=%v", stored.DippedWhileFrozen, err)
}
}