From 745539dc114c79a2a46c33fb776c5d16ae961d9b Mon Sep 17 00:00:00 2001 From: samosvalishe Date: Wed, 17 Jun 2026 03:32:10 +0300 Subject: [PATCH] =?UTF-8?q?fix(vkauth):=20=D0=B3=D1=80=D1=83=D0=BF=D0=BF?= =?UTF-8?q?=D0=B8=D1=80=D0=BE=D0=B2=D0=BA=D0=B0=20=D0=BA=D1=8D=D1=88=D0=B0?= =?UTF-8?q?=20=D0=BF=D0=BE=201-based=20streamID?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CacheID делил streamID/streamsPerCache, но streamID 1-based: поток с номером, кратным streamsPerCache, переваливал в соседний bucket и тянул лишнюю VK-авторизацию (заметно при -n == -streams-per-cred). Теперь (streamID-1)/streamsPerCache - блок 1..N делит один кэш. --- internal/provider/vk/internal/vkauth/cache.go | 13 ++++++++++--- .../provider/vk/internal/vkauth/client_test.go | 17 +++++++++++++++-- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/internal/provider/vk/internal/vkauth/cache.go b/internal/provider/vk/internal/vkauth/cache.go index d5d5dd0..664e96d 100644 --- a/internal/provider/vk/internal/vkauth/cache.go +++ b/internal/provider/vk/internal/vkauth/cache.go @@ -15,7 +15,7 @@ type StreamCredentialsCache struct { lastErrorTime atomic.Int64 } -// Store отображает cache-id (streamID / streamsPerCache) → StreamCredentialsCache. +// Store отображает cache-id ((streamID-1) / streamsPerCache) → StreamCredentialsCache. type Store struct { mu sync.RWMutex 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 { - return streamID / s.streamsPerCache + if streamID < 1 { + return 0 + } + return (streamID - 1) / s.streamsPerCache } func (s *Store) Get(streamID int) *StreamCredentialsCache { @@ -68,7 +75,7 @@ func (c *StreamCredentialsCache) Invalidate() { } // IsAuthError проверяет ошибку по эвристике TURN-клиента: -// auth/401/stale-nonce — признак инвалидации кэша. +// auth/401/stale-nonce - признак инвалидации кэша. func IsAuthError(err error) bool { if err == nil { return false diff --git a/internal/provider/vk/internal/vkauth/client_test.go b/internal/provider/vk/internal/vkauth/client_test.go index 7e7fc92..1359b4c 100644 --- a/internal/provider/vk/internal/vkauth/client_test.go +++ b/internal/provider/vk/internal/vkauth/client_test.go @@ -53,9 +53,10 @@ func TestIsAuthError(t *testing.T) { func TestStoreCacheGrouping(t *testing.T) { t.Parallel() + // streamID 1-based (как в pipeline): потоки 1..10 → cache 0, 11..20 → cache 1. s := NewStore(10) - if s.CacheID(0) != 0 || s.CacheID(9) != 0 || s.CacheID(10) != 1 { - t.Fatalf("unexpected cache grouping: 0→%d 9→%d 10→%d", s.CacheID(0), s.CacheID(9), s.CacheID(10)) + if s.CacheID(1) != 0 || s.CacheID(10) != 0 || s.CacheID(11) != 1 { + t.Fatalf("unexpected cache grouping: 1→%d 10→%d 11→%d", s.CacheID(1), s.CacheID(10), s.CacheID(11)) } a := s.Get(3) b := s.Get(7) @@ -66,6 +67,18 @@ func TestStoreCacheGrouping(t *testing.T) { if a == c { 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) {