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),
|
rooms: make(map[livekit.RoomName]*rtc.Room),
|
||||||
|
|
||||||
iceConfigCache: sutils.NewIceConfigCache[iceConfigCacheKey](),
|
iceConfigCache: sutils.NewIceConfigCache[iceConfigCacheKey](0),
|
||||||
|
|
||||||
serverInfo: &livekit.ServerInfo{
|
serverInfo: &livekit.ServerInfo{
|
||||||
Edition: livekit.ServerInfo_Standard,
|
Edition: livekit.ServerInfo_Standard,
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
iceConfigTTL = 5 * time.Minute
|
iceConfigTTLMin = 5 * time.Minute
|
||||||
)
|
)
|
||||||
|
|
||||||
type iceConfigCacheEntry struct {
|
type iceConfigCacheEntry struct {
|
||||||
@@ -19,16 +19,23 @@ type iceConfigCacheEntry struct {
|
|||||||
|
|
||||||
type IceConfigCache[T comparable] struct {
|
type IceConfigCache[T comparable] struct {
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
|
ttl time.Duration
|
||||||
entries map[T]*iceConfigCacheEntry
|
entries map[T]*iceConfigCacheEntry
|
||||||
|
|
||||||
stopped atomic.Bool
|
stopped atomic.Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewIceConfigCache[T comparable]() *IceConfigCache[T] {
|
func NewIceConfigCache[T comparable](ttl time.Duration) *IceConfigCache[T] {
|
||||||
icc := &IceConfigCache[T]{
|
icc := &IceConfigCache[T]{
|
||||||
entries: make(map[T]*iceConfigCacheEntry),
|
entries: make(map[T]*iceConfigCacheEntry),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ttl < iceConfigTTLMin {
|
||||||
|
icc.ttl = iceConfigTTLMin
|
||||||
|
} else {
|
||||||
|
icc.ttl = ttl
|
||||||
|
}
|
||||||
|
|
||||||
go icc.pruneWorker()
|
go icc.pruneWorker()
|
||||||
return icc
|
return icc
|
||||||
}
|
}
|
||||||
@@ -52,7 +59,7 @@ func (icc *IceConfigCache[T]) Get(key T) *livekit.ICEConfig {
|
|||||||
defer icc.lock.Unlock()
|
defer icc.lock.Unlock()
|
||||||
|
|
||||||
entry, ok := icc.entries[key]
|
entry, ok := icc.entries[key]
|
||||||
if !ok || time.Since(entry.modifiedAt) > iceConfigTTL {
|
if !ok || time.Since(entry.modifiedAt) > icc.ttl {
|
||||||
delete(icc.entries, key)
|
delete(icc.entries, key)
|
||||||
return &livekit.ICEConfig{}
|
return &livekit.ICEConfig{}
|
||||||
}
|
}
|
||||||
@@ -61,7 +68,7 @@ func (icc *IceConfigCache[T]) Get(key T) *livekit.ICEConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (icc *IceConfigCache[T]) pruneWorker() {
|
func (icc *IceConfigCache[T]) pruneWorker() {
|
||||||
ticker := time.NewTicker(iceConfigTTL / 2)
|
ticker := time.NewTicker(icc.ttl / 2)
|
||||||
defer ticker.Stop()
|
defer ticker.Stop()
|
||||||
|
|
||||||
for !icc.stopped.Load() {
|
for !icc.stopped.Load() {
|
||||||
@@ -69,7 +76,7 @@ func (icc *IceConfigCache[T]) pruneWorker() {
|
|||||||
|
|
||||||
icc.lock.Lock()
|
icc.lock.Lock()
|
||||||
for key, entry := range icc.entries {
|
for key, entry := range icc.entries {
|
||||||
if time.Since(entry.modifiedAt) > iceConfigTTL {
|
if time.Since(entry.modifiedAt) > icc.ttl {
|
||||||
delete(icc.entries, key)
|
delete(icc.entries, key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user