mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-12-02 07:24:48 +03:00
1ec55aed30
Every five seconds the master fetched each node's complete inbound list, settings JSON and enrichment included, parsed it and reconciled every client, whether anything had changed or not. A node that advertises traffic-snapshot-v1 now answers a revisioned endpoint: counters and lifecycle state on every poll, inbound configuration and attachments only when its revision changed. The master skips structural parsing and client reconciliation while the node's revision, its own rows for that node, the lifecycle state and the full remote-to-central inbound mapping are all unchanged; anything else falls back to the full merge. Old and new panels keep talking through the previous endpoints. Revisions come from a hidden per-row token on inbounds and clients, maintained by database triggers on SQLite and PostgreSQL so that direct and background writes count too. Counter and timestamp updates leave it alone. The triggers are dropped before migrations and recreated after them, and the previous build runs unchanged on a database that carries them. Measured here at 20,000 clients on one node, per idle poll: master SQL statements 551 -> 44, node statements 55 -> 4, transfer 23.0 MB -> 4.8 MB. When every client is shared between nodes the ownership checks keep the full master merge, so only the node and the transfer get cheaper. The node still reads every membership and counter on each poll. Closes #7007
58 lines
2.1 KiB
Go
58 lines
2.1 KiB
Go
package database
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
)
|
|
|
|
func TestMigrateInboundPreserveClientTrafficColumn(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 := dropSnapshotRevisionTriggers(tx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := tx.Exec("ALTER TABLE inbounds DROP COLUMN preserve_client_traffic").Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
const tag = "legacy-preserve-client-traffic"
|
|
if err := tx.Exec("INSERT INTO inbounds (tag, up, down, traffic_reset, traffic_reset_day) VALUES (?, 30, 50, 'monthly', 20)", tag).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := migrateInboundPreserveClientTrafficColumn(tx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := migrateSnapshotRevisions(tx); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var stored model.Inbound
|
|
if err := tx.Where("tag = ?", tag).First(&stored).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if stored.PreserveClientTraffic == nil || *stored.PreserveClientTraffic {
|
|
t.Fatal("legacy inbound must have a non-null, false preserve_client_traffic")
|
|
}
|
|
if stored.Up != 30 || stored.Down != 50 || stored.TrafficReset != "monthly" || stored.TrafficResetDay != 20 {
|
|
t.Fatalf("legacy inbound after migration: up=%d down=%d schedule=%s day=%d", stored.Up, stored.Down, stored.TrafficReset, stored.TrafficResetDay)
|
|
}
|
|
revision := stored.SnapshotRevision
|
|
if err := tx.Model(&stored).Update("preserve_client_traffic", true).Error; err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := migrateInboundPreserveClientTrafficColumn(tx); err != nil {
|
|
t.Fatalf("repeat migration: %v", err)
|
|
}
|
|
if err := tx.First(&stored, stored.Id).Error; err != nil || !stored.PreservesClientTraffic() {
|
|
t.Fatalf("repeat migration lost the opt-in: preserve=%t, err=%v", stored.PreservesClientTraffic(), err)
|
|
}
|
|
if revision == "" || stored.SnapshotRevision == "" || stored.SnapshotRevision == revision {
|
|
t.Fatalf("changing preserved traffic must update the snapshot revision: %q -> %q", revision, stored.SnapshotRevision)
|
|
}
|
|
}
|