Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
陈曦
sub2api
Commits
b59c79c4
Commit
b59c79c4
authored
Mar 07, 2026
by
神乐
Browse files
fix: 简易模式仅提升管理员默认并发到30
parent
bcb6444f
Changes
4
Show whitespace changes
Inline
Side-by-side
backend/internal/repository/ent.go
View file @
b59c79c4
...
@@ -89,6 +89,10 @@ func InitEnt(cfg *config.Config) (*ent.Client, *sql.DB, error) {
...
@@ -89,6 +89,10 @@ func InitEnt(cfg *config.Config) (*ent.Client, *sql.DB, error) {
_
=
client
.
Close
()
_
=
client
.
Close
()
return
nil
,
nil
,
err
return
nil
,
nil
,
err
}
}
if
err
:=
ensureSimpleModeAdminConcurrency
(
seedCtx
,
client
);
err
!=
nil
{
_
=
client
.
Close
()
return
nil
,
nil
,
err
}
}
}
return
client
,
drv
.
DB
(),
nil
return
client
,
drv
.
DB
(),
nil
...
...
backend/internal/repository/simple_mode_admin_concurrency.go
0 → 100644
View file @
b59c79c4
package
repository
import
(
"context"
"fmt"
"time"
dbent
"github.com/Wei-Shaw/sub2api/ent"
"github.com/Wei-Shaw/sub2api/ent/setting"
dbuser
"github.com/Wei-Shaw/sub2api/ent/user"
"github.com/Wei-Shaw/sub2api/internal/service"
)
const
(
simpleModeAdminConcurrencyUpgradeKey
=
"simple_mode_admin_concurrency_upgraded_30"
simpleModeLegacyAdminConcurrency
=
5
simpleModeTargetAdminConcurrency
=
30
)
func
ensureSimpleModeAdminConcurrency
(
ctx
context
.
Context
,
client
*
dbent
.
Client
)
error
{
if
client
==
nil
{
return
fmt
.
Errorf
(
"nil ent client"
)
}
upgraded
,
err
:=
client
.
Setting
.
Query
()
.
Where
(
setting
.
KeyEQ
(
simpleModeAdminConcurrencyUpgradeKey
))
.
Exist
(
ctx
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"check admin concurrency upgrade marker: %w"
,
err
)
}
if
upgraded
{
return
nil
}
if
_
,
err
:=
client
.
User
.
Update
()
.
Where
(
dbuser
.
RoleEQ
(
service
.
RoleAdmin
),
dbuser
.
ConcurrencyEQ
(
simpleModeLegacyAdminConcurrency
),
)
.
SetConcurrency
(
simpleModeTargetAdminConcurrency
)
.
Save
(
ctx
);
err
!=
nil
{
return
fmt
.
Errorf
(
"upgrade simple mode admin concurrency: %w"
,
err
)
}
now
:=
time
.
Now
()
if
err
:=
client
.
Setting
.
Create
()
.
SetKey
(
simpleModeAdminConcurrencyUpgradeKey
)
.
SetValue
(
now
.
Format
(
time
.
RFC3339
))
.
SetUpdatedAt
(
now
)
.
OnConflictColumns
(
setting
.
FieldKey
)
.
UpdateNewValues
()
.
Exec
(
ctx
);
err
!=
nil
{
return
fmt
.
Errorf
(
"persist admin concurrency upgrade marker: %w"
,
err
)
}
return
nil
}
backend/internal/setup/setup.go
View file @
b59c79c4
...
@@ -12,6 +12,7 @@ import (
...
@@ -12,6 +12,7 @@ import (
"strings"
"strings"
"time"
"time"
"github.com/Wei-Shaw/sub2api/internal/config"
"github.com/Wei-Shaw/sub2api/internal/pkg/logger"
"github.com/Wei-Shaw/sub2api/internal/pkg/logger"
"github.com/Wei-Shaw/sub2api/internal/repository"
"github.com/Wei-Shaw/sub2api/internal/repository"
"github.com/Wei-Shaw/sub2api/internal/service"
"github.com/Wei-Shaw/sub2api/internal/service"
...
@@ -25,8 +26,17 @@ import (
...
@@ -25,8 +26,17 @@ import (
const
(
const
(
ConfigFileName
=
"config.yaml"
ConfigFileName
=
"config.yaml"
InstallLockFile
=
".installed"
InstallLockFile
=
".installed"
defaultUserConcurrency
=
5
simpleModeAdminConcurrency
=
30
)
)
func
setupDefaultAdminConcurrency
()
int
{
if
strings
.
EqualFold
(
strings
.
TrimSpace
(
os
.
Getenv
(
"RUN_MODE"
)),
config
.
RunModeSimple
)
{
return
simpleModeAdminConcurrency
}
return
defaultUserConcurrency
}
// GetDataDir returns the data directory for storing config and lock files.
// GetDataDir returns the data directory for storing config and lock files.
// Priority: DATA_DIR env > /app/data (if exists and writable) > current directory
// Priority: DATA_DIR env > /app/data (if exists and writable) > current directory
func
GetDataDir
()
string
{
func
GetDataDir
()
string
{
...
@@ -390,7 +400,7 @@ func createAdminUser(cfg *SetupConfig) (bool, string, error) {
...
@@ -390,7 +400,7 @@ func createAdminUser(cfg *SetupConfig) (bool, string, error) {
Role
:
service
.
RoleAdmin
,
Role
:
service
.
RoleAdmin
,
Status
:
service
.
StatusActive
,
Status
:
service
.
StatusActive
,
Balance
:
0
,
Balance
:
0
,
Concurrency
:
5
,
Concurrency
:
setupDefaultAdminConcurrency
()
,
CreatedAt
:
time
.
Now
(),
CreatedAt
:
time
.
Now
(),
UpdatedAt
:
time
.
Now
(),
UpdatedAt
:
time
.
Now
(),
}
}
...
@@ -462,7 +472,7 @@ func writeConfigFile(cfg *SetupConfig) error {
...
@@ -462,7 +472,7 @@ func writeConfigFile(cfg *SetupConfig) error {
APIKeyPrefix
string
`yaml:"api_key_prefix"`
APIKeyPrefix
string
`yaml:"api_key_prefix"`
RateMultiplier
float64
`yaml:"rate_multiplier"`
RateMultiplier
float64
`yaml:"rate_multiplier"`
}{
}{
UserConcurrency
:
5
,
UserConcurrency
:
defaultUserConcurrency
,
UserBalance
:
0
,
UserBalance
:
0
,
APIKeyPrefix
:
"sk-"
,
APIKeyPrefix
:
"sk-"
,
RateMultiplier
:
1.0
,
RateMultiplier
:
1.0
,
...
...
backend/internal/setup/setup_test.go
View file @
b59c79c4
package
setup
package
setup
import
"testing"
import
(
"os"
"strings"
"testing"
)
func
TestDecideAdminBootstrap
(
t
*
testing
.
T
)
{
func
TestDecideAdminBootstrap
(
t
*
testing
.
T
)
{
t
.
Parallel
()
t
.
Parallel
()
...
@@ -49,3 +53,37 @@ func TestDecideAdminBootstrap(t *testing.T) {
...
@@ -49,3 +53,37 @@ func TestDecideAdminBootstrap(t *testing.T) {
})
})
}
}
}
}
func
TestSetupDefaultAdminConcurrency
(
t
*
testing
.
T
)
{
t
.
Run
(
"simple mode admin uses higher concurrency"
,
func
(
t
*
testing
.
T
)
{
t
.
Setenv
(
"RUN_MODE"
,
"simple"
)
if
got
:=
setupDefaultAdminConcurrency
();
got
!=
simpleModeAdminConcurrency
{
t
.
Fatalf
(
"setupDefaultAdminConcurrency()=%d, want %d"
,
got
,
simpleModeAdminConcurrency
)
}
})
t
.
Run
(
"standard mode keeps existing default"
,
func
(
t
*
testing
.
T
)
{
t
.
Setenv
(
"RUN_MODE"
,
"standard"
)
if
got
:=
setupDefaultAdminConcurrency
();
got
!=
defaultUserConcurrency
{
t
.
Fatalf
(
"setupDefaultAdminConcurrency()=%d, want %d"
,
got
,
defaultUserConcurrency
)
}
})
}
func
TestWriteConfigFileKeepsDefaultUserConcurrency
(
t
*
testing
.
T
)
{
t
.
Setenv
(
"RUN_MODE"
,
"simple"
)
t
.
Setenv
(
"DATA_DIR"
,
t
.
TempDir
())
if
err
:=
writeConfigFile
(
&
SetupConfig
{});
err
!=
nil
{
t
.
Fatalf
(
"writeConfigFile() error = %v"
,
err
)
}
data
,
err
:=
os
.
ReadFile
(
GetConfigFilePath
())
if
err
!=
nil
{
t
.
Fatalf
(
"ReadFile() error = %v"
,
err
)
}
if
!
strings
.
Contains
(
string
(
data
),
"user_concurrency: 5"
)
{
t
.
Fatalf
(
"config missing default user concurrency, got:
\n
%s"
,
string
(
data
))
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment