ICE config cache module. (#2654)
* ICE config cache module. * generic key type * no ICEConfig in StartSession * clean up
This commit is contained in:
+9
-2
@@ -479,7 +479,14 @@ func (r *Room) GetParticipantRequestSource(identity livekit.ParticipantIdentity)
|
||||
return r.participantRequestSources[identity]
|
||||
}
|
||||
|
||||
func (r *Room) ResumeParticipant(p types.LocalParticipant, requestSource routing.MessageSource, responseSink routing.MessageSink, iceServers []*livekit.ICEServer, reason livekit.ReconnectReason) error {
|
||||
func (r *Room) ResumeParticipant(
|
||||
p types.LocalParticipant,
|
||||
requestSource routing.MessageSource,
|
||||
responseSink routing.MessageSink,
|
||||
iceConfig *livekit.ICEConfig,
|
||||
iceServers []*livekit.ICEServer,
|
||||
reason livekit.ReconnectReason,
|
||||
) error {
|
||||
r.ReplaceParticipantRequestSource(p.Identity(), requestSource)
|
||||
// close previous sink, and link to new one
|
||||
p.CloseSignalConnection(types.SignallingCloseReasonResume)
|
||||
@@ -521,7 +528,7 @@ func (r *Room) ResumeParticipant(p types.LocalParticipant, requestSource routing
|
||||
}
|
||||
|
||||
_ = p.SendRoomUpdate(r.ToProto())
|
||||
p.ICERestart(nil)
|
||||
p.ICERestart(iceConfig)
|
||||
|
||||
// check for simulated signal disconnect on resume
|
||||
r.simulationLock.Lock()
|
||||
|
||||
@@ -431,9 +431,7 @@ func (t *TransportManager) HandleClientReconnect(reason livekit.ReconnectReason)
|
||||
}
|
||||
|
||||
func (t *TransportManager) ICERestart(iceConfig *livekit.ICEConfig) error {
|
||||
if iceConfig != nil {
|
||||
t.SetICEConfig(iceConfig)
|
||||
}
|
||||
t.SetICEConfig(iceConfig)
|
||||
|
||||
return t.subscriber.ICERestart()
|
||||
}
|
||||
@@ -445,7 +443,9 @@ func (t *TransportManager) OnICEConfigChanged(f func(iceConfig *livekit.ICEConfi
|
||||
}
|
||||
|
||||
func (t *TransportManager) SetICEConfig(iceConfig *livekit.ICEConfig) {
|
||||
t.configureICE(iceConfig, true)
|
||||
if iceConfig != nil {
|
||||
t.configureICE(iceConfig, true)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TransportManager) resetTransportConfigureLocked(reconfigured bool) {
|
||||
|
||||
+16
-31
@@ -25,6 +25,7 @@ import (
|
||||
"golang.org/x/exp/maps"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/agent"
|
||||
sutils "github.com/livekit/livekit-server/pkg/utils"
|
||||
"github.com/livekit/mediatransportutil/pkg/rtcconfig"
|
||||
"github.com/livekit/protocol/auth"
|
||||
"github.com/livekit/protocol/livekit"
|
||||
@@ -48,14 +49,13 @@ const (
|
||||
roomPurgeSeconds = 24 * 60 * 60
|
||||
tokenRefreshInterval = 5 * time.Minute
|
||||
tokenDefaultTTL = 10 * time.Minute
|
||||
iceConfigTTL = 5 * time.Minute
|
||||
)
|
||||
|
||||
var affinityEpoch = time.Date(2000, 0, 0, 0, 0, 0, 0, time.UTC)
|
||||
|
||||
type iceConfigCacheEntry struct {
|
||||
iceConfig *livekit.ICEConfig
|
||||
modifiedAt time.Time
|
||||
type iceConfigCacheKey struct {
|
||||
roomName livekit.RoomName
|
||||
participantIdentity livekit.ParticipantIdentity
|
||||
}
|
||||
|
||||
// RoomManager manages rooms and its interaction with participants.
|
||||
@@ -82,7 +82,7 @@ type RoomManager struct {
|
||||
roomServers utils.MultitonService[rpc.RoomTopic]
|
||||
participantServers utils.MultitonService[rpc.ParticipantTopic]
|
||||
|
||||
iceConfigCache map[livekit.ParticipantIdentity]*iceConfigCacheEntry
|
||||
iceConfigCache *sutils.IceConfigCache[iceConfigCacheKey]
|
||||
}
|
||||
|
||||
func NewLocalRoomManager(
|
||||
@@ -119,7 +119,7 @@ func NewLocalRoomManager(
|
||||
|
||||
rooms: make(map[livekit.RoomName]*rtc.Room),
|
||||
|
||||
iceConfigCache: make(map[livekit.ParticipantIdentity]*iceConfigCacheEntry),
|
||||
iceConfigCache: sutils.NewIceConfigCache[iceConfigCacheKey](),
|
||||
|
||||
serverInfo: &livekit.ServerInfo{
|
||||
Edition: livekit.ServerInfo_Standard,
|
||||
@@ -230,6 +230,8 @@ func (r *RoomManager) Stop() {
|
||||
_ = r.rtcConfig.TCPMuxListener.Close()
|
||||
}
|
||||
}
|
||||
|
||||
r.iceConfigCache.Stop()
|
||||
}
|
||||
|
||||
// StartSession starts WebRTC session when a new participant is connected, takes place on RTC node
|
||||
@@ -303,14 +305,12 @@ func (r *RoomManager) StartSession(
|
||||
"reason", pi.ReconnectReason,
|
||||
"numParticipants", room.GetParticipantCount(),
|
||||
)
|
||||
iceConfig := r.getIceConfig(participant)
|
||||
if iceConfig == nil {
|
||||
iceConfig = &livekit.ICEConfig{}
|
||||
}
|
||||
iceConfig := r.getIceConfig(roomName, participant)
|
||||
if err = room.ResumeParticipant(
|
||||
participant,
|
||||
requestSource,
|
||||
responseSink,
|
||||
iceConfig,
|
||||
r.iceServersForParticipant(
|
||||
apiKey,
|
||||
participant,
|
||||
@@ -444,7 +444,7 @@ func (r *RoomManager) StartSession(
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
iceConfig := r.setIceConfig(participant)
|
||||
iceConfig := r.setIceConfig(roomName, participant)
|
||||
|
||||
// join room
|
||||
opts := rtc.ParticipantOptions{
|
||||
@@ -504,12 +504,7 @@ func (r *RoomManager) StartSession(
|
||||
}
|
||||
})
|
||||
participant.OnICEConfigChanged(func(participant types.LocalParticipant, iceConfig *livekit.ICEConfig) {
|
||||
r.lock.Lock()
|
||||
r.iceConfigCache[participant.Identity()] = &iceConfigCacheEntry{
|
||||
iceConfig: iceConfig,
|
||||
modifiedAt: time.Now(),
|
||||
}
|
||||
r.lock.Unlock()
|
||||
r.iceConfigCache.Put(iceConfigCacheKey{roomName, participant.Identity()}, iceConfig, time.Now())
|
||||
})
|
||||
|
||||
go r.rtcSessionWorker(room, participant, requestSource)
|
||||
@@ -871,24 +866,14 @@ func (r *RoomManager) refreshToken(participant types.LocalParticipant) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RoomManager) setIceConfig(participant types.LocalParticipant) *livekit.ICEConfig {
|
||||
iceConfig := r.getIceConfig(participant)
|
||||
if iceConfig == nil {
|
||||
return &livekit.ICEConfig{}
|
||||
}
|
||||
func (r *RoomManager) setIceConfig(roomName livekit.RoomName, participant types.LocalParticipant) *livekit.ICEConfig {
|
||||
iceConfig := r.getIceConfig(roomName, participant)
|
||||
participant.SetICEConfig(iceConfig)
|
||||
return iceConfig
|
||||
}
|
||||
|
||||
func (r *RoomManager) getIceConfig(participant types.LocalParticipant) *livekit.ICEConfig {
|
||||
r.lock.Lock()
|
||||
defer r.lock.Unlock()
|
||||
iceConfigCacheEntry, ok := r.iceConfigCache[participant.Identity()]
|
||||
if !ok || time.Since(iceConfigCacheEntry.modifiedAt) > iceConfigTTL {
|
||||
delete(r.iceConfigCache, participant.Identity())
|
||||
return nil
|
||||
}
|
||||
return iceConfigCacheEntry.iceConfig
|
||||
func (r *RoomManager) getIceConfig(roomName livekit.RoomName, participant types.LocalParticipant) *livekit.ICEConfig {
|
||||
return r.iceConfigCache.Get(iceConfigCacheKey{roomName, participant.Identity()})
|
||||
}
|
||||
|
||||
func (r *RoomManager) getFirstKeyPair() (string, string, error) {
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/livekit/protocol/livekit"
|
||||
"go.uber.org/atomic"
|
||||
)
|
||||
|
||||
const (
|
||||
iceConfigTTL = 5 * time.Minute
|
||||
)
|
||||
|
||||
type iceConfigCacheEntry struct {
|
||||
iceConfig *livekit.ICEConfig
|
||||
modifiedAt time.Time
|
||||
}
|
||||
|
||||
type IceConfigCache[T comparable] struct {
|
||||
lock sync.Mutex
|
||||
entries map[T]*iceConfigCacheEntry
|
||||
|
||||
stopped atomic.Bool
|
||||
}
|
||||
|
||||
func NewIceConfigCache[T comparable]() *IceConfigCache[T] {
|
||||
icc := &IceConfigCache[T]{
|
||||
entries: make(map[T]*iceConfigCacheEntry),
|
||||
}
|
||||
|
||||
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) > iceConfigTTL {
|
||||
delete(icc.entries, key)
|
||||
return &livekit.ICEConfig{}
|
||||
}
|
||||
|
||||
return entry.iceConfig
|
||||
}
|
||||
|
||||
func (icc *IceConfigCache[T]) pruneWorker() {
|
||||
ticker := time.NewTicker(iceConfigTTL / 2)
|
||||
defer ticker.Stop()
|
||||
|
||||
for !icc.stopped.Load() {
|
||||
<-ticker.C
|
||||
|
||||
icc.lock.Lock()
|
||||
for key, entry := range icc.entries {
|
||||
if time.Since(entry.modifiedAt) > iceConfigTTL {
|
||||
delete(icc.entries, key)
|
||||
}
|
||||
}
|
||||
icc.lock.Unlock()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user