83 lines
2.0 KiB
Go
83 lines
2.0 KiB
Go
package vkauth
|
|
|
|
import (
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
)
|
|
|
|
// StreamCredentialsCache хранит TURN-реквизиты для группы потоков
|
|
// и счётчик ошибок для решений об инвалидации.
|
|
type StreamCredentialsCache struct {
|
|
creds TurnCredentials
|
|
mutex sync.RWMutex
|
|
errorCount atomic.Int32
|
|
lastErrorTime atomic.Int64
|
|
}
|
|
|
|
// Store отображает cache-id (streamID / streamsPerCache) → StreamCredentialsCache.
|
|
type Store struct {
|
|
mu sync.RWMutex
|
|
caches map[int]*StreamCredentialsCache
|
|
streamsPerCache int
|
|
}
|
|
|
|
func NewStore(streamsPerCache int) *Store {
|
|
if streamsPerCache <= 0 {
|
|
streamsPerCache = DefaultStreamsPerCache
|
|
}
|
|
return &Store{
|
|
caches: make(map[int]*StreamCredentialsCache),
|
|
streamsPerCache: streamsPerCache,
|
|
}
|
|
}
|
|
|
|
func (s *Store) CacheID(streamID int) int {
|
|
return streamID / s.streamsPerCache
|
|
}
|
|
|
|
func (s *Store) Get(streamID int) *StreamCredentialsCache {
|
|
cacheID := s.CacheID(streamID)
|
|
|
|
s.mu.RLock()
|
|
cache, exists := s.caches[cacheID]
|
|
s.mu.RUnlock()
|
|
if exists {
|
|
return cache
|
|
}
|
|
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
|
|
if cache, exists = s.caches[cacheID]; exists {
|
|
return cache
|
|
}
|
|
cache = &StreamCredentialsCache{}
|
|
s.caches[cacheID] = cache
|
|
return cache
|
|
}
|
|
|
|
// Invalidate сбрасывает реквизиты кэша потока и обнуляет счётчик ошибок.
|
|
func (c *StreamCredentialsCache) Invalidate() {
|
|
c.mutex.Lock()
|
|
c.creds = TurnCredentials{}
|
|
c.mutex.Unlock()
|
|
|
|
c.errorCount.Store(0)
|
|
c.lastErrorTime.Store(0)
|
|
}
|
|
|
|
// IsAuthError проверяет ошибку по эвристике TURN-клиента:
|
|
// auth/401/stale-nonce — признак инвалидации кэша.
|
|
func IsAuthError(err error) bool {
|
|
if err == nil {
|
|
return false
|
|
}
|
|
s := err.Error()
|
|
return strings.Contains(s, "401") ||
|
|
strings.Contains(s, "Unauthorized") ||
|
|
strings.Contains(s, "authentication") ||
|
|
strings.Contains(s, "invalid credential") ||
|
|
strings.Contains(s, "stale nonce")
|
|
}
|