@@ -1,85 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"go.uber.org/atomic"
|
||||
)
|
||||
|
||||
const (
|
||||
iceConfigTTLMin = 5 * time.Minute
|
||||
)
|
||||
|
||||
type iceConfigCacheEntry struct {
|
||||
iceConfig *livekit.ICEConfig
|
||||
modifiedAt time.Time
|
||||
}
|
||||
|
||||
type IceConfigCache[T comparable] struct {
|
||||
lock sync.Mutex
|
||||
ttl time.Duration
|
||||
entries map[T]*iceConfigCacheEntry
|
||||
|
||||
stopped atomic.Bool
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (icc *IceConfigCache[T]) Stop() {
|
||||
icc.stopped.Store(true)
|
||||
}
|
||||
|
||||
func (icc *IceConfigCache[T]) Put(key T, iceConfig *livekit.ICEConfig, at time.Time) {
|
||||
icc.lock.Lock()
|
||||
defer icc.lock.Unlock()
|
||||
|
||||
icc.entries[key] = &iceConfigCacheEntry{
|
||||
iceConfig: iceConfig,
|
||||
modifiedAt: at,
|
||||
}
|
||||
}
|
||||
|
||||
func (icc *IceConfigCache[T]) Get(key T) *livekit.ICEConfig {
|
||||
icc.lock.Lock()
|
||||
defer icc.lock.Unlock()
|
||||
|
||||
entry, ok := icc.entries[key]
|
||||
if !ok || time.Since(entry.modifiedAt) > icc.ttl {
|
||||
delete(icc.entries, key)
|
||||
return &livekit.ICEConfig{}
|
||||
}
|
||||
|
||||
return entry.iceConfig
|
||||
}
|
||||
|
||||
func (icc *IceConfigCache[T]) pruneWorker() {
|
||||
ticker := time.NewTicker(icc.ttl / 2)
|
||||
defer ticker.Stop()
|
||||
|
||||
for !icc.stopped.Load() {
|
||||
<-ticker.C
|
||||
|
||||
icc.lock.Lock()
|
||||
for key, entry := range icc.entries {
|
||||
if time.Since(entry.modifiedAt) > icc.ttl {
|
||||
delete(icc.entries, key)
|
||||
}
|
||||
}
|
||||
icc.lock.Unlock()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/jellydator/ttlcache/v3"
|
||||
|
||||
"github.com/livekit/protocol/livekit"
|
||||
)
|
||||
|
||||
const (
|
||||
iceConfigTTLMin = 5 * time.Minute
|
||||
)
|
||||
|
||||
type IceConfigCache[T comparable] struct {
|
||||
c *ttlcache.Cache[T, *livekit.ICEConfig]
|
||||
}
|
||||
|
||||
func NewIceConfigCache[T comparable](ttl time.Duration) *IceConfigCache[T] {
|
||||
cache := ttlcache.New(
|
||||
ttlcache.WithTTL[T, *livekit.ICEConfig](max(ttl, iceConfigTTLMin)),
|
||||
ttlcache.WithDisableTouchOnHit[T, *livekit.ICEConfig](),
|
||||
)
|
||||
go cache.Start()
|
||||
|
||||
return &IceConfigCache[T]{cache}
|
||||
}
|
||||
|
||||
func (icc *IceConfigCache[T]) Stop() {
|
||||
icc.c.Stop()
|
||||
}
|
||||
|
||||
func (icc *IceConfigCache[T]) Put(key T, iceConfig *livekit.ICEConfig) {
|
||||
icc.c.Set(key, iceConfig, ttlcache.DefaultTTL)
|
||||
}
|
||||
|
||||
func (icc *IceConfigCache[T]) Get(key T) *livekit.ICEConfig {
|
||||
if it := icc.c.Get(key); it != nil {
|
||||
return it.Value()
|
||||
}
|
||||
return &livekit.ICEConfig{}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/livekit/protocol/livekit"
|
||||
)
|
||||
|
||||
func TestIceConfigCache(t *testing.T) {
|
||||
cache := NewIceConfigCache[string](10 * time.Second)
|
||||
t.Cleanup(cache.Stop)
|
||||
|
||||
cache.Put("test", &livekit.ICEConfig{})
|
||||
require.NotNil(t, cache)
|
||||
}
|
||||
Reference in New Issue
Block a user