mirror of
https://github.com/livekit/livekit.git
synced 2026-08-28 11:44:43 +00:00
TTL param for ICE config cache (#2676)
* TTL param for ICE config cache * rename to min
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user