From 8f053851260ffd4e6b536713a126b8c547174e1a Mon Sep 17 00:00:00 2001 From: Raja Subramanian Date: Tue, 23 Apr 2024 10:49:55 +0530 Subject: [PATCH] TTL param for ICE config cache (#2676) * TTL param for ICE config cache * rename to min --- pkg/service/roommanager.go | 2 +- pkg/utils/ice_config_cache.go | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/pkg/service/roommanager.go b/pkg/service/roommanager.go index 409a6e34b..3aafad3fd 100644 --- a/pkg/service/roommanager.go +++ b/pkg/service/roommanager.go @@ -119,7 +119,7 @@ func NewLocalRoomManager( rooms: make(map[livekit.RoomName]*rtc.Room), - iceConfigCache: sutils.NewIceConfigCache[iceConfigCacheKey](), + iceConfigCache: sutils.NewIceConfigCache[iceConfigCacheKey](0), serverInfo: &livekit.ServerInfo{ Edition: livekit.ServerInfo_Standard, diff --git a/pkg/utils/ice_config_cache.go b/pkg/utils/ice_config_cache.go index 2fb4f49e7..a3ff2a22a 100644 --- a/pkg/utils/ice_config_cache.go +++ b/pkg/utils/ice_config_cache.go @@ -9,7 +9,7 @@ import ( ) const ( - iceConfigTTL = 5 * time.Minute + iceConfigTTLMin = 5 * time.Minute ) type iceConfigCacheEntry struct { @@ -19,16 +19,23 @@ type iceConfigCacheEntry struct { type IceConfigCache[T comparable] struct { lock sync.Mutex + ttl time.Duration entries map[T]*iceConfigCacheEntry stopped atomic.Bool } -func NewIceConfigCache[T comparable]() *IceConfigCache[T] { +func NewIceConfigCache[T comparable](ttl time.Duration) *IceConfigCache[T] { icc := &IceConfigCache[T]{ entries: make(map[T]*iceConfigCacheEntry), } + if ttl < iceConfigTTLMin { + icc.ttl = iceConfigTTLMin + } else { + icc.ttl = ttl + } + go icc.pruneWorker() return icc } @@ -52,7 +59,7 @@ func (icc *IceConfigCache[T]) Get(key T) *livekit.ICEConfig { defer icc.lock.Unlock() entry, ok := icc.entries[key] - if !ok || time.Since(entry.modifiedAt) > iceConfigTTL { + if !ok || time.Since(entry.modifiedAt) > icc.ttl { delete(icc.entries, key) return &livekit.ICEConfig{} } @@ -61,7 +68,7 @@ func (icc *IceConfigCache[T]) Get(key T) *livekit.ICEConfig { } func (icc *IceConfigCache[T]) pruneWorker() { - ticker := time.NewTicker(iceConfigTTL / 2) + ticker := time.NewTicker(icc.ttl / 2) defer ticker.Stop() for !icc.stopped.Load() { @@ -69,7 +76,7 @@ func (icc *IceConfigCache[T]) pruneWorker() { icc.lock.Lock() for key, entry := range icc.entries { - if time.Since(entry.modifiedAt) > iceConfigTTL { + if time.Since(entry.modifiedAt) > icc.ttl { delete(icc.entries, key) } }