修改事务提交调用问题

This commit is contained in:
2025-04-02 18:02:23 +08:00
parent 68e96793fe
commit 116fa8ec18
5 changed files with 57 additions and 24 deletions

2
.gitignore vendored
View File

@@ -5,6 +5,6 @@
.env.*
!.env.example
build/
bin/
*.http

36
build.ps1 Normal file
View File

@@ -0,0 +1,36 @@
Remove-Job *
$tasks = 0
$start = Get-Date
$tasks++
Write-Output "building platform for linux amd64..."
Start-Job -ScriptBlock {
$env:GOOS = "linux"; $env:GOARCH = "amd64"; go build -ldflags '-w -s' -o bin/platform_linux_amd64 cmd/main/main.go
} | Out-Null
$tasks++
Write-Output "building tasks for linux amd64..."
Start-Job -ScriptBlock {
$env:GOOS = "linux"; $env:GOARCH = "amd64"; go build -ldflags '-w -s' -o bin/tasks_linux_amd64 cmd/tasks/main.go
} | Out-Null
# Wait for all jobs to complete
while ($tasks -gt 0)
{
foreach ($job in Get-Job)
{
if ($job.State -eq "Completed")
{
$tasks--
$job | Receive-Job
$job | Remove-Job
}
}
}
$end = Get-Date
Write-Output "build completed"
Write-Output "time taken: $( ($end - $start).TotalSeconds ) seconds"
Write-Output "output files are in ./bin/"

View File

@@ -13,7 +13,7 @@ import (
// SetupDBTest 创建一个基于 SQLite 内存数据库的 GORM 连接
func SetupDBTest(t *testing.T) *gorm.DB {
// 使用 SQLite 内存数据库
gormDB, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
gormDB, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
if err != nil {
t.Fatalf("gorm 打开 SQLite 内存数据库失败: %v", err)
}

View File

@@ -251,7 +251,7 @@ func (s *channelService) CreateChannel(
}
var addr []*PortInfo
err := q.Q.Transaction(func(tx *q.Query) error {
err := q.Q.Transaction(func(q *q.Query) error {
// 查找套餐
step = time.Now()
@@ -285,7 +285,7 @@ func (s *channelService) CreateChannel(
// 申请节点
step = time.Now()
edgeAssigns, err := assignEdge(count, filter)
edgeAssigns, err := assignEdge(q, count, filter)
if err != nil {
return err
}
@@ -297,7 +297,7 @@ func (s *channelService) CreateChannel(
now := time.Now()
expiration := now.Add(time.Duration(resource.Live) * time.Second)
_addr, channels, err := assignPort(edgeAssigns, auth.Payload.Id, protocol, authType, expiration, filter)
_addr, channels, err := assignPort(q, edgeAssigns, auth.Payload.Id, protocol, authType, expiration, filter)
if err != nil {
return err
}
@@ -386,7 +386,7 @@ func checkUser(auth *AuthContext, resource *ResourceInfo, count int) error {
}
// assignEdge 分配边缘节点数量
func assignEdge(count int, filter NodeFilterConfig) (*AssignEdgeResult, error) {
func assignEdge(q *q.Query, count int, filter NodeFilterConfig) (*AssignEdgeResult, error) {
// 查询可以使用的网关
var step = time.Now()
@@ -539,6 +539,7 @@ type ProxyConfig struct {
// assignPort 分配指定数量的端口
func assignPort(
q *q.Query,
proxies *AssignEdgeResult,
userId int32,
protocol ChannelProtocol,
@@ -718,8 +719,7 @@ func genPassPair() (string, string) {
password[i] = letters[rand.N(len(letters))]
}
// return string(username), string(password)
return "123123", "123123"
return string(username), string(password)
}
func chKey(channel *models.Channel) string {

View File

@@ -8,6 +8,7 @@ import (
"platform/pkg/remote"
"platform/pkg/testutil"
"platform/web/models"
"reflect"
"strings"
"testing"
"time"
@@ -337,7 +338,7 @@ func Test_channelService_CreateChannel(t *testing.T) {
name string
args args
setup func()
want []string
want []*PortInfo
wantErr bool
wantErrContains string
checkCache func(channels []models.Channel) error
@@ -353,10 +354,12 @@ func Test_channelService_CreateChannel(t *testing.T) {
count: 3,
nodeFilter: []NodeFilterConfig{{Prov: "河南", City: "郑州", Isp: "电信"}},
},
want: []string{
"http://111.111.111.111:10000",
"http://111.111.111.111:10001",
"http://111.111.111.111:10002",
want: []*PortInfo{
{
Proto: "http",
Host: proxy.Host,
Port: 10000,
},
},
},
{
@@ -369,10 +372,7 @@ func Test_channelService_CreateChannel(t *testing.T) {
authType: ChannelAuthTypeIp,
count: 2,
},
want: []string{
"http://111.111.111.111:10000",
"http://111.111.111.111:10001",
},
want: []*PortInfo{},
},
{
name: "管理员创建SOCKS5密码通道",
@@ -384,10 +384,7 @@ func Test_channelService_CreateChannel(t *testing.T) {
authType: ChannelAuthTypePass,
count: 2,
},
want: []string{
"socks5://111.111.111.111:10000",
"socks5://111.111.111.111:10001",
},
want: []*PortInfo{},
},
{
name: "套餐不存在",
@@ -507,9 +504,9 @@ func Test_channelService_CreateChannel(t *testing.T) {
return
}
if len(got) != len(tt.want) {
t.Errorf("CreateChannel() 返回长度 = %v, want %v", len(got), len(tt.want))
return
// 检查返回值
if reflect.DeepEqual(got, tt.want) {
t.Errorf("CreateChannel() 返回值 = %v, 期望 %v", got, tt.want)
}
// 查询创建的通道