fix(traffic): commit a traffic tick even when a best-effort maintenance helper fails

addTrafficLocked stages the inbound and client deltas, then runs three helpers
(auto-renew, disable depleted clients, disable depleted inbounds) that are meant
to log and continue. All three reused the function-scope err that the deferred
commit/rollback inspects, so the last helper's error decided the whole tick: a
failure in disableInvalidInbounds rolled back the already-staged traffic while
AddTraffic reported success, and because xray had already advanced its counter
baseline that traffic was lost for good. Give each best-effort helper its own
error variable so only a genuine staging failure rolls the tick back.
This commit is contained in:
MHSanaei
2026-07-15 02:04:48 +02:00
parent f7cae8d1cf
commit b6928f4939
2 changed files with 63 additions and 9 deletions
+9 -9
View File
@@ -51,25 +51,25 @@ func (s *InboundService) addTrafficLocked(inboundTraffics []*xray.Traffic, clien
return false, false, err
}
needRestart0, count, err := s.autoRenewClients(tx)
if err != nil {
logger.Warning("Error in renew clients:", err)
needRestart0, count, renewErr := s.autoRenewClients(tx)
if renewErr != nil {
logger.Warning("Error in renew clients:", renewErr)
} else if count > 0 {
logger.Debugf("%v clients renewed", count)
}
disabledClientsCount := int64(0)
needRestart1, count, err := s.disableInvalidClients(tx)
if err != nil {
logger.Warning("Error in disabling invalid clients:", err)
needRestart1, count, disableClientsErr := s.disableInvalidClients(tx)
if disableClientsErr != nil {
logger.Warning("Error in disabling invalid clients:", disableClientsErr)
} else if count > 0 {
logger.Debugf("%v clients disabled", count)
disabledClientsCount = count
}
needRestart2, count, err := s.disableInvalidInbounds(tx)
if err != nil {
logger.Warning("Error in disabling invalid inbounds:", err)
needRestart2, count, disableInboundsErr := s.disableInvalidInbounds(tx)
if disableInboundsErr != nil {
logger.Warning("Error in disabling invalid inbounds:", disableInboundsErr)
} else if count > 0 {
logger.Debugf("%v inbounds disabled", count)
}
@@ -0,0 +1,54 @@
package service
import (
"errors"
"strings"
"testing"
"gorm.io/gorm"
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
"github.com/mhsanaei/3x-ui/v3/internal/xray"
)
// The traffic tick stages inbound and client deltas, then runs three best-effort
// maintenance helpers (renew, disable-depleted-clients, disable-depleted-inbounds)
// that are meant to log and continue. A failure in one of them must not roll back
// the already-staged traffic — xray has already advanced its baseline, so a
// rolled-back tick loses that traffic permanently.
func TestAddTrafficCommitsDespiteDisableHelperError(t *testing.T) {
db := initTrafficTestDB(t)
svc := &InboundService{}
normal := &model.Inbound{UserId: 1, Tag: "in-normal", Enable: true, Port: 43001, Protocol: model.VLESS, Settings: `{"clients":[]}`}
if err := db.Create(normal).Error; err != nil {
t.Fatalf("seed normal inbound: %v", err)
}
expired := &model.Inbound{UserId: 1, Tag: "in-expired", Enable: true, Port: 43002, Protocol: model.VLESS, ExpiryTime: 1, Settings: `{"clients":[]}`}
if err := db.Create(expired).Error; err != nil {
t.Fatalf("seed expired inbound: %v", err)
}
const cbName = "b2-03:fail-disable"
if err := db.Callback().Update().After("gorm:update").Register(cbName, func(tx *gorm.DB) {
if tx.Statement != nil && tx.Statement.Table == "inbounds" &&
strings.Contains(tx.Statement.SQL.String(), "expiry_time") {
tx.AddError(errors.New("injected disableInvalidInbounds failure"))
}
}); err != nil {
t.Fatalf("register callback: %v", err)
}
t.Cleanup(func() { _ = db.Callback().Update().Remove(cbName) })
if _, _, err := svc.AddTraffic([]*xray.Traffic{{IsInbound: true, Tag: "in-normal", Up: 500, Down: 700}}, nil); err != nil {
t.Fatalf("AddTraffic: %v", err)
}
var reloaded model.Inbound
if err := db.Where("tag = ?", "in-normal").First(&reloaded).Error; err != nil {
t.Fatalf("reload normal inbound: %v", err)
}
if reloaded.Up != 500 || reloaded.Down != 700 {
t.Fatalf("traffic tick was rolled back by a best-effort disable-helper error: up=%d down=%d, want 500/700", reloaded.Up, reloaded.Down)
}
}