fix(traffic): stop adding capped counters in SQL

Per-client counters are stored up to TrafficMax, which a node can reach
by reporting huge usage, and the group totals, group baselines and the
per-client reset lookup added two such counters inside SQL, overflowing
it: every group listing and reset failed, and no client on that reset
cycle was reset. The counters are now read separately and added in Go,
saturating at TrafficMax.
This commit is contained in:
MHSanaei
2026-10-31 14:41:12 +01:00
parent d69b924baf
commit dcdc56c64b
4 changed files with 115 additions and 55 deletions
+4 -3
View File
@@ -123,18 +123,19 @@ type ClientResetCycle struct {
TrafficResetDay int
Enable bool
Total int64
Used int64
Up int64
Down int64
}
// Depleted reports a client the quota switched off. A reset restores that one;
// a client disabled below its quota was switched off by hand and stays off.
func (c ClientResetCycle) Depleted() bool {
return c.Total > 0 && c.Used >= c.Total
return c.Total > 0 && clampedTrafficAdd(c.Up, c.Down) >= c.Total
}
func clientResetCycles(tx *gorm.DB) *gorm.DB {
return tx.Table("clients c").
Select("c.email, c.traffic_reset_day, c.enable, COALESCE(ct.total, 0) AS total, COALESCE(ct.up, 0) + COALESCE(ct.down, 0) AS used").
Select("c.email, c.traffic_reset_day, c.enable, COALESCE(ct.total, 0) AS total, COALESCE(ct.up, 0) AS up, COALESCE(ct.down, 0) AS down").
Joins("LEFT JOIN client_traffics ct ON ct.email = c.email")
}