fix(vkauth): группировка кэша по 1-based streamID
CacheID делил streamID/streamsPerCache, но streamID 1-based: поток с номером, кратным streamsPerCache, переваливал в соседний bucket и тянул лишнюю VK-авторизацию (заметно при -n == -streams-per-cred). Теперь (streamID-1)/streamsPerCache - блок 1..N делит один кэш.
This commit is contained in:
1 parent
4084cce091
commit
745539dc11
2 files changed
+25
-5
No files matched your search
@@ -15,7 +15,7 @@ type StreamCredentialsCache struct {
|
|||||||
lastErrorTime atomic.Int64
|
lastErrorTime atomic.Int64
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store отображает cache-id (streamID / streamsPerCache) → StreamCredentialsCache.
|
// Store отображает cache-id ((streamID-1) / streamsPerCache) → StreamCredentialsCache.
|
||||||
type Store struct {
|
type Store struct {
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
caches map[int]*StreamCredentialsCache
|
caches map[int]*StreamCredentialsCache
|
||||||
@@ -32,8 +32,15 @@ func NewStore(streamsPerCache int) *Store {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CacheID группирует потоки в блоки по streamsPerCache: потоки 1..streamsPerCache
|
||||||
|
// делят один кэш реквизитов, streamsPerCache+1.. - следующий. streamID 1-based
|
||||||
|
// (единый базис udprelay и tcpfwd); первый поток блока инициирует fetch к VK,
|
||||||
|
// остальные переиспользуют тёплый кэш.
|
||||||
func (s *Store) CacheID(streamID int) int {
|
func (s *Store) CacheID(streamID int) int {
|
||||||
return streamID / s.streamsPerCache
|
if streamID < 1 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return (streamID - 1) / s.streamsPerCache
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) Get(streamID int) *StreamCredentialsCache {
|
func (s *Store) Get(streamID int) *StreamCredentialsCache {
|
||||||
@@ -68,7 +75,7 @@ func (c *StreamCredentialsCache) Invalidate() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// IsAuthError проверяет ошибку по эвристике TURN-клиента:
|
// IsAuthError проверяет ошибку по эвристике TURN-клиента:
|
||||||
// auth/401/stale-nonce — признак инвалидации кэша.
|
// auth/401/stale-nonce - признак инвалидации кэша.
|
||||||
func IsAuthError(err error) bool {
|
func IsAuthError(err error) bool {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return false
|
return false
|
||||||
|
|||||||
@@ -53,9 +53,10 @@ func TestIsAuthError(t *testing.T) {
|
|||||||
func TestStoreCacheGrouping(t *testing.T) {
|
func TestStoreCacheGrouping(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
// streamID 1-based (как в pipeline): потоки 1..10 → cache 0, 11..20 → cache 1.
|
||||||
s := NewStore(10)
|
s := NewStore(10)
|
||||||
if s.CacheID(0) != 0 || s.CacheID(9) != 0 || s.CacheID(10) != 1 {
|
if s.CacheID(1) != 0 || s.CacheID(10) != 0 || s.CacheID(11) != 1 {
|
||||||
t.Fatalf("unexpected cache grouping: 0→%d 9→%d 10→%d", s.CacheID(0), s.CacheID(9), s.CacheID(10))
|
t.Fatalf("unexpected cache grouping: 1→%d 10→%d 11→%d", s.CacheID(1), s.CacheID(10), s.CacheID(11))
|
||||||
}
|
}
|
||||||
a := s.Get(3)
|
a := s.Get(3)
|
||||||
b := s.Get(7)
|
b := s.Get(7)
|
||||||
@@ -66,6 +67,18 @@ func TestStoreCacheGrouping(t *testing.T) {
|
|||||||
if a == c {
|
if a == c {
|
||||||
t.Error("streams 3 and 13 should not share cache (groups 0 vs 1)")
|
t.Error("streams 3 and 13 should not share cache (groups 0 vs 1)")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// streams-per-cred == n: все потоки 1..n делят один кэш — поток n не должен
|
||||||
|
// переваливать в отдельный bucket (регрессия на -n 7 -streams-per-cred 7).
|
||||||
|
s7 := NewStore(7)
|
||||||
|
for id := 1; id <= 7; id++ {
|
||||||
|
if s7.CacheID(id) != 0 {
|
||||||
|
t.Errorf("streams-per-cred=7: stream %d → cache %d, want 0", id, s7.CacheID(id))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if s7.CacheID(8) != 1 {
|
||||||
|
t.Errorf("streams-per-cred=7: stream 8 → cache %d, want 1", s7.CacheID(8))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandleAuthErrorInvalidatesAtThreshold(t *testing.T) {
|
func TestHandleAuthErrorInvalidatesAtThreshold(t *testing.T) {
|
||||||
|
|||||||
Reference in new issue
Block a user