mirror of
https://github.com/MHSanaei/3x-ui.git
synced 2026-09-10 20:27:15 +00:00
8b9cf260b6
BulkResetTraffic resolved every address with its own GetRecordByEmail call, one SELECT per email, purely to find the disabled clients it has to re-enable. Resetting 30 clients issued 30 queries before the batched transaction even started, while BulkAdjust, BulkDelete and BulkSetEnable next to it already loaded their rows with a single chunked IN query. Those three carried a verbatim copy each of both the trim/dedupe loop and the chunked record load, so the reuse is the fix: trimmedUniqueEmails now delegates to the existing uniqueNonEmptyStrings, and clientRecordsByEmail holds the one chunked lookup all four call sites share. A DB failure during the lookup now aborts the reset instead of being swallowed per email; a missing row is still skipped, as before. The new test drives BulkResetTraffic with 3 and with 30 emails and fails unless both issue the same number of SELECTs against clients.
214 lines
5.5 KiB
Go
214 lines
5.5 KiB
Go
package service
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/database/model"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/logger"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/util/common"
|
|
"github.com/mhsanaei/3x-ui/v3/internal/xray"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func (s *ClientService) ResetTrafficByEmail(inboundSvc *InboundService, email string) (bool, error) {
|
|
if email == "" {
|
|
return false, common.NewError("client email is required")
|
|
}
|
|
rec, err := s.GetRecordByEmail(nil, email)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
inboundIds, err := s.GetInboundIdsForRecord(rec.Id)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
needRestart := false
|
|
if !rec.Enable {
|
|
updated := rec.ToClient()
|
|
updated.Enable = true
|
|
nr, uErr := s.Update(inboundSvc, rec.Id, *updated, rec.LimitHwid)
|
|
if uErr != nil {
|
|
logger.Warning("Failed to auto-enable client during traffic reset:", uErr)
|
|
}
|
|
if nr {
|
|
needRestart = true
|
|
}
|
|
}
|
|
|
|
if len(inboundIds) == 0 {
|
|
if rErr := inboundSvc.ResetClientTrafficByEmail(email); rErr != nil {
|
|
return false, rErr
|
|
}
|
|
return needRestart, nil
|
|
}
|
|
|
|
applies := make([]inboundApply, 0, len(inboundIds))
|
|
for _, ibId := range inboundIds {
|
|
applies = append(applies, inboundApply{id: ibId, run: func() (bool, error) {
|
|
return inboundSvc.ResetClientTraffic(ibId, email)
|
|
}})
|
|
}
|
|
nr, applyErr := fanoutInboundApplies(applies)
|
|
return needRestart || nr, applyErr
|
|
}
|
|
|
|
func (s *ClientService) BulkResetTraffic(inboundSvc *InboundService, emails []string) (int, error) {
|
|
if len(emails) == 0 {
|
|
return 0, nil
|
|
}
|
|
cleanEmails := trimmedUniqueEmails(emails)
|
|
if len(cleanEmails) == 0 {
|
|
return 0, nil
|
|
}
|
|
|
|
recordsByEmail, err := clientRecordsByEmail(nil, cleanEmails)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
for _, e := range cleanEmails {
|
|
rec := recordsByEmail[e]
|
|
if rec == nil || rec.Enable {
|
|
continue
|
|
}
|
|
updated := rec.ToClient()
|
|
updated.Enable = true
|
|
if _, uErr := s.Update(inboundSvc, rec.Id, *updated, rec.LimitHwid); uErr != nil {
|
|
logger.Warning("Failed to auto-enable client during bulk traffic reset:", uErr)
|
|
}
|
|
}
|
|
|
|
affected := 0
|
|
err = submitTrafficWrite(func() error {
|
|
db := database.GetDB()
|
|
return db.Transaction(func(tx *gorm.DB) error {
|
|
if err := adjustGroupBaselinesForRemovedTraffic(tx, cleanEmails); err != nil {
|
|
return err
|
|
}
|
|
for _, batch := range chunkStrings(cleanEmails, sqlInChunk) {
|
|
res := tx.Model(xray.ClientTraffic{}).
|
|
Where("email IN ?", batch).
|
|
Updates(map[string]any{"enable": true, "up": 0, "down": 0})
|
|
if res.Error != nil {
|
|
return res.Error
|
|
}
|
|
affected += int(res.RowsAffected)
|
|
}
|
|
if err := clearGlobalTraffic(tx, cleanEmails...); err != nil {
|
|
return err
|
|
}
|
|
for _, batch := range chunkStrings(cleanEmails, sqlInChunk) {
|
|
if err := tx.Where("email IN ?", batch).Delete(&model.NodeClientTraffic{}).Error; err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
})
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return affected, nil
|
|
}
|
|
|
|
func (s *ClientService) ResetAllClientTraffics(inboundSvc *InboundService, id int) error {
|
|
err := submitTrafficWrite(func() error {
|
|
return s.resetAllClientTrafficsLocked(id)
|
|
})
|
|
if err == nil {
|
|
inboundSvc.resetAllMtprotoQuotas()
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (s *ClientService) resetAllClientTrafficsLocked(id int) error {
|
|
db := database.GetDB()
|
|
now := time.Now().Unix() * 1000
|
|
|
|
if err := db.Transaction(func(tx *gorm.DB) error {
|
|
// client_traffics.inbound_id is stale: it reflects the inbound the row was
|
|
// first inserted under and is never refreshed. Use the client_inbounds join
|
|
// as the authoritative source for which emails belong to a given inbound.
|
|
var resetEmails []string
|
|
if id == -1 {
|
|
if err := tx.Model(xray.ClientTraffic{}).Pluck("email", &resetEmails).Error; err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
if err := tx.Table("client_inbounds ci").
|
|
Select("c.email").
|
|
Joins("JOIN clients c ON c.id = ci.client_id").
|
|
Where("ci.inbound_id = ?", id).
|
|
Pluck("c.email", &resetEmails).Error; err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if len(resetEmails) == 0 {
|
|
return nil
|
|
}
|
|
|
|
if err := adjustGroupBaselinesForRemovedTraffic(tx, resetEmails); err != nil {
|
|
return err
|
|
}
|
|
|
|
result := tx.Model(xray.ClientTraffic{}).
|
|
Where("email IN ?", resetEmails).
|
|
Updates(map[string]any{"enable": true, "up": 0, "down": 0})
|
|
|
|
if result.Error != nil {
|
|
return result.Error
|
|
}
|
|
|
|
if err := clearGlobalTraffic(tx, resetEmails...); err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, batch := range chunkStrings(resetEmails, sqlInChunk) {
|
|
if err := tx.Where("email IN ?", batch).Delete(&model.NodeClientTraffic{}).Error; err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
inboundWhereText := "id "
|
|
if id == -1 {
|
|
inboundWhereText += " > ?"
|
|
} else {
|
|
inboundWhereText += " = ?"
|
|
}
|
|
|
|
result = tx.Model(model.Inbound{}).
|
|
Where(inboundWhereText, id).
|
|
Update("last_traffic_reset_time", now)
|
|
|
|
return result.Error
|
|
}); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *ClientService) ResetAllTraffics() (bool, error) {
|
|
var affected int64
|
|
err := submitTrafficWrite(func() error {
|
|
return database.GetDB().Transaction(func(tx *gorm.DB) error {
|
|
res := tx.Model(&xray.ClientTraffic{}).
|
|
Where("1 = 1").
|
|
Updates(map[string]any{"enable": true, "up": 0, "down": 0})
|
|
if res.Error != nil {
|
|
return res.Error
|
|
}
|
|
affected = res.RowsAffected
|
|
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 != nil {
|
|
return false, err
|
|
}
|
|
return affected > 0, nil
|
|
}
|