TTL param for ICE config cache (#2676)

* TTL param for ICE config cache

* rename to min
This commit is contained in:
Raja Subramanian
2024-04-23 10:49:55 +05:30
committed by GitHub
parent c4354575ec
commit 8f05385126
2 changed files with 13 additions and 6 deletions
+1 -1
View File
@@ -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,
+12 -5
View File
@@ -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)
}
}