修复余额与套餐用量并发更新可能导致数据错误的问题

This commit is contained in:
2025-04-30 16:17:18 +08:00
parent c9258aa8ae
commit fa356431ee
4 changed files with 10 additions and 10 deletions

View File

@@ -2,6 +2,7 @@ package services
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
@@ -244,6 +245,8 @@ func (s *channelService) CreateChannel(
count int,
nodeFilter ...NodeFilterConfig,
) ([]*PortInfo, error) {
var now = time.Now()
var step = time.Now()
var rid = ctx.Value(requestid.ConfigDefault.ContextKey).(string)
@@ -298,7 +301,6 @@ func (s *channelService) CreateChannel(
// 分配端口
step = time.Now()
now := time.Now()
expiration := common.LocalDateTime(now.Add(time.Duration(resource.Live) * time.Second))
_addr, channels, err := assignPort(q, edgeAssigns, auth.Payload.Id, protocol, authType, expiration, filter)
if err != nil {
@@ -315,7 +317,7 @@ func (s *channelService) CreateChannel(
Used: resource.Used + int32(count),
DailyLast: common.LocalDateTime(now),
}
last := time.Time(resource.DailyLast)
var last = time.Time(resource.DailyLast)
if now.Year() != last.Year() || now.Month() != last.Month() || now.Day() != last.Day() {
toUpdate.DailyUsed = int32(count)
} else {
@@ -346,7 +348,7 @@ func (s *channelService) CreateChannel(
slog.Debug("缓存通道数据", "rid", rid, "step", time.Since(step))
return nil
})
}, &sql.TxOptions{Isolation: sql.LevelRepeatableRead})
if err != nil {
return nil, err
}
@@ -674,7 +676,7 @@ func assignPort(
q.Channel.NodeHost,
q.Channel.DeletedAt,
).
Save(channels...)
Create(channels...)
if err != nil {
return nil, nil, err
}