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
c4182f8c
Commit
c4182f8c
authored
Feb 06, 2026
by
yangjianbo
Browse files
perf(service): 移除 jitter 随机数全局锁
parent
028f8aaa
Changes
1
Show whitespace changes
Inline
Side-by-side
backend/internal/service/api_key_auth_cache_impl.go
View file @
c4182f8c
...
@@ -6,8 +6,7 @@ import (
...
@@ -6,8 +6,7 @@ import (
"encoding/hex"
"encoding/hex"
"errors"
"errors"
"fmt"
"fmt"
"math/rand"
"math/rand/v2"
"sync"
"time"
"time"
"github.com/Wei-Shaw/sub2api/internal/config"
"github.com/Wei-Shaw/sub2api/internal/config"
...
@@ -24,9 +23,8 @@ type apiKeyAuthCacheConfig struct {
...
@@ -24,9 +23,8 @@ type apiKeyAuthCacheConfig struct {
}
}
var
(
var
(
jitterRandMu
sync
.
Mutex
// 认证缓存抖动直接使用 rand/v2 的顶层函数。
// 认证缓存抖动使用独立随机源,避免全局 Seed
// rand/v2 顶层函数并发安全,避免全局互斥锁成为热点。
jitterRand
=
rand
.
New
(
rand
.
NewSource
(
time
.
Now
()
.
UnixNano
()))
)
)
func
newAPIKeyAuthCacheConfig
(
cfg
*
config
.
Config
)
apiKeyAuthCacheConfig
{
func
newAPIKeyAuthCacheConfig
(
cfg
*
config
.
Config
)
apiKeyAuthCacheConfig
{
...
@@ -68,9 +66,7 @@ func (c apiKeyAuthCacheConfig) jitterTTL(ttl time.Duration) time.Duration {
...
@@ -68,9 +66,7 @@ func (c apiKeyAuthCacheConfig) jitterTTL(ttl time.Duration) time.Duration {
percent
=
100
percent
=
100
}
}
delta
:=
float64
(
percent
)
/
100
delta
:=
float64
(
percent
)
/
100
jitterRandMu
.
Lock
()
randVal
:=
rand
.
Float64
()
randVal
:=
jitterRand
.
Float64
()
jitterRandMu
.
Unlock
()
factor
:=
1
-
delta
+
randVal
*
(
2
*
delta
)
factor
:=
1
-
delta
+
randVal
*
(
2
*
delta
)
if
factor
<=
0
{
if
factor
<=
0
{
return
ttl
return
ttl
...
...
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