fix(nodes): sum client traffic across nodes instead of overwriting

A client shared across multiple nodes has a single email-keyed client_traffics row, but each node reports its cumulative up/down. setRemoteTrafficLocked overwrote the row with one node's cumulative, so non-owning nodes hit the create branch and OnConflict-DoNothing, silently dropping their traffic and under-counting the client.

Make the shared row a pure accumulator (like the local path): a new node_client_traffics(node_id, email) baseline table stores each node's last cumulative; the node path converts cumulative to a per-node delta (clamped to the post-reset value on a negative delta) and does up = up + delta. First observation seeds the baseline and adds 0 so upgrades and newly-shared clients are not double-counted. Create-vs-accumulate now keys off global email existence. Baselines are cleaned in DelClientStat, the node sweeps, and NodeService.Delete.
This commit is contained in:
MHSanaei
2026-06-01 22:54:56 +02:00
parent 588ea86298
commit 5b9ed34009
6 changed files with 306 additions and 26 deletions
+1
View File
@@ -72,6 +72,7 @@ func initModels() error {
&model.ClientInbound{},
&model.ClientGroup{},
&model.InboundFallback{},
&model.NodeClientTraffic{},
}
for _, mdl := range models {
if err := db.AutoMigrate(mdl); err != nil {
+1
View File
@@ -36,6 +36,7 @@ func migrationModels() []any {
&model.ClientRecord{},
&model.ClientInbound{},
&model.InboundFallback{},
&model.NodeClientTraffic{},
}
}
+9
View File
@@ -0,0 +1,9 @@
package model
type NodeClientTraffic struct {
Id int `json:"id" gorm:"primaryKey;autoIncrement"`
NodeId int `json:"nodeId" gorm:"uniqueIndex:idx_node_email,priority:1;not null"`
Email string `json:"email" gorm:"uniqueIndex:idx_node_email,priority:2;not null"`
Up int64 `json:"up"`
Down int64 `json:"down"`
}