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
963b3b76
Unverified
Commit
963b3b76
authored
Mar 06, 2026
by
Wesley Liddick
Committed by
GitHub
Mar 06, 2026
Browse files
Merge pull request #825 from FizzlyCode/fix/setup-token-real-utilization
fix: Setup Token 账号使用真实 utilization 值替代状态估算
parents
f6709fb5
49d0301d
Changes
2
Show whitespace changes
Inline
Side-by-side
backend/internal/service/account_usage_service.go
View file @
963b3b76
...
...
@@ -903,15 +903,30 @@ func (s *AccountUsageService) estimateSetupTokenUsage(account *Account) *UsageIn
remaining
=
0
}
//
根据状态估算使用率 (百分比形式,100 = 100%)
//
优先使用响应头中存储的真实 utilization 值(0-1 小数,转为 0-100 百分比)
var
utilization
float64
var
found
bool
if
stored
,
ok
:=
account
.
Extra
[
"session_window_utilization"
];
ok
{
switch
v
:=
stored
.
(
type
)
{
case
float64
:
utilization
=
v
*
100
found
=
true
case
json
.
Number
:
if
f
,
err
:=
v
.
Float64
();
err
==
nil
{
utilization
=
f
*
100
found
=
true
}
}
}
// 如果没有存储的 utilization,回退到状态估算
if
!
found
{
switch
account
.
SessionWindowStatus
{
case
"rejected"
:
utilization
=
100.0
case
"allowed_warning"
:
utilization
=
80.0
default
:
utilization
=
0.0
}
}
info
.
FiveHour
=
&
UsageProgress
{
...
...
backend/internal/service/ratelimit_service.go
View file @
963b3b76
...
...
@@ -970,12 +970,27 @@ func (s *RateLimitService) UpdateSessionWindow(ctx context.Context, account *Acc
windowStart
=
&
start
windowEnd
=
&
end
slog
.
Info
(
"account_session_window_initialized"
,
"account_id"
,
account
.
ID
,
"window_start"
,
start
,
"window_end"
,
end
,
"status"
,
status
)
// 窗口重置时清除旧的 utilization,避免残留上个窗口的数据
_
=
s
.
accountRepo
.
UpdateExtra
(
ctx
,
account
.
ID
,
map
[
string
]
any
{
"session_window_utilization"
:
nil
,
})
}
if
err
:=
s
.
accountRepo
.
UpdateSessionWindow
(
ctx
,
account
.
ID
,
windowStart
,
windowEnd
,
status
);
err
!=
nil
{
slog
.
Warn
(
"session_window_update_failed"
,
"account_id"
,
account
.
ID
,
"error"
,
err
)
}
// 存储真实的 utilization 值(0-1 小数),供 estimateSetupTokenUsage 使用
if
utilStr
:=
headers
.
Get
(
"anthropic-ratelimit-unified-5h-utilization"
);
utilStr
!=
""
{
if
util
,
err
:=
strconv
.
ParseFloat
(
utilStr
,
64
);
err
==
nil
{
if
err
:=
s
.
accountRepo
.
UpdateExtra
(
ctx
,
account
.
ID
,
map
[
string
]
any
{
"session_window_utilization"
:
util
,
});
err
!=
nil
{
slog
.
Warn
(
"session_window_utilization_update_failed"
,
"account_id"
,
account
.
ID
,
"error"
,
err
)
}
}
}
// 如果状态为allowed且之前有限流,说明窗口已重置,清除限流状态
if
status
==
"allowed"
&&
account
.
IsRateLimited
()
{
if
err
:=
s
.
ClearRateLimit
(
ctx
,
account
.
ID
);
err
!=
nil
{
...
...
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