fix(traffic): make a client reset reach every counter enforcing its quota

Invariant: a client traffic reset zeroes every counter that enforces the
client's quota - the master's, each hosting node's, and the local MTProto
sidecar's. A node cuts a client on its own local counters, and the master
adopts that verdict when both judged the same limits (#4917).

Node counters: ResetClientTraffic tried the node once and dropped a
failure ("nothing replays a reset"); BulkResetTraffic,
ResetAllClientTraffics and ClientService.ResetAllTraffics never told the
node at all. The node kept its pre-reset usage, switched the client off
again on its next tick, and the master latched that - a client shown at
zero usage stayed disabled.

Every reset path now queues a node_pending_resets row per hosting node in
its own transaction. It is delivered right after commit (per-client
endpoint up to the push threshold, bulkResetTraffic above) and replayed by
the node sync ahead of its snapshot, and dropped only once the node
accepted it. While one is owed, the merge takes only that client's usage
from the node, not its enable or limits. Deliveries to a node are
serialized so a reset is not sent twice.

Sidecar quota: BulkResetTraffic, ClientService.ResetAllTraffics and
auto-renew zeroed the panel counters but not mtg's own quota counter, so
the sidecar kept refusing a renewed or reset MTProto client. They now
reset it too, scoped to the affected clients.
This commit is contained in:
MHSanaei
2026-09-28 02:56:53 +02:00
parent 7c84ca9689
commit 4210a50cb4
18 changed files with 850 additions and 61 deletions
+29 -6
View File
@@ -74,6 +74,7 @@ func (s *ClientService) BulkResetTraffic(inboundSvc *InboundService, emails []st
return 0, err
}
affected := 0
var resetNodes []int
err = submitTrafficWrite(func() error {
db := database.GetDB()
return db.Transaction(func(tx *gorm.DB) error {
@@ -97,12 +98,16 @@ func (s *ClientService) BulkResetTraffic(inboundSvc *InboundService, emails []st
return err
}
}
return nil
var qErr error
resetNodes, qErr = queueNodeResets(tx, cleanEmails)
return qErr
})
})
if err != nil {
return 0, err
}
inboundSvc.resetMtprotoClientQuotas(cleanEmails)
inboundSvc.deliverNodeResetsNow(resetNodes)
// After the zeroing, as in ResetTrafficByEmail: enabling a still-depleted
// client first lets the next traffic tick switch it off again.
for _, e := range cleanEmails {
@@ -120,18 +125,23 @@ func (s *ClientService) BulkResetTraffic(inboundSvc *InboundService, emails []st
}
func (s *ClientService) ResetAllClientTraffics(inboundSvc *InboundService, id int) error {
var resetNodes []int
err := submitTrafficWrite(func() error {
return s.resetAllClientTrafficsLocked(id)
var inner error
resetNodes, inner = s.resetAllClientTrafficsLocked(id)
return inner
})
if err == nil {
inboundSvc.resetAllMtprotoQuotas()
inboundSvc.deliverNodeResetsNow(resetNodes)
}
return err
}
func (s *ClientService) resetAllClientTrafficsLocked(id int) error {
func (s *ClientService) resetAllClientTrafficsLocked(id int) ([]int, error) {
db := database.GetDB()
now := time.Now().Unix() * 1000
var resetNodes []int
if err := db.Transaction(func(tx *gorm.DB) error {
// client_traffics.inbound_id is stale: it reflects the inbound the row was
@@ -176,6 +186,10 @@ func (s *ClientService) resetAllClientTrafficsLocked(id int) error {
return err
}
}
var qErr error
if resetNodes, qErr = queueNodeResets(tx, resetEmails); qErr != nil {
return qErr
}
inboundWhereText := "id "
if id == -1 {
@@ -190,13 +204,14 @@ func (s *ClientService) resetAllClientTrafficsLocked(id int) error {
return result.Error
}); err != nil {
return err
return nil, err
}
return nil
return resetNodes, nil
}
func (s *ClientService) ResetAllTraffics() (bool, error) {
var affected int64
var resetNodes []int
err := submitTrafficWrite(func() error {
return database.GetDB().Transaction(func(tx *gorm.DB) error {
res := tx.Model(&xray.ClientTraffic{}).
@@ -209,11 +224,19 @@ func (s *ClientService) ResetAllTraffics() (bool, error) {
if err := tx.Where("1 = 1").Delete(&model.ClientGlobalTraffic{}).Error; err != nil {
return err
}
return tx.Where("1 = 1").Delete(&model.NodeClientTraffic{}).Error
if err := tx.Where("1 = 1").Delete(&model.NodeClientTraffic{}).Error; err != nil {
return err
}
var qErr error
resetNodes, qErr = queueNodeResets(tx, nil)
return qErr
})
})
if err != nil {
return false, err
}
inbounds := &InboundService{}
inbounds.resetAllMtprotoQuotas()
inbounds.deliverNodeResetsNow(resetNodes)
return affected > 0, nil
}