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]
|
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)
|
r.ReplaceParticipantRequestSource(p.Identity(), requestSource)
|
||||||
// close previous sink, and link to new one
|
// close previous sink, and link to new one
|
||||||
p.CloseSignalConnection(types.SignallingCloseReasonResume)
|
p.CloseSignalConnection(types.SignallingCloseReasonResume)
|
||||||
@@ -521,7 +528,7 @@ func (r *Room) ResumeParticipant(p types.LocalParticipant, requestSource routing
|
|||||||
}
|
}
|
||||||
|
|
||||||
_ = p.SendRoomUpdate(r.ToProto())
|
_ = p.SendRoomUpdate(r.ToProto())
|
||||||
p.ICERestart(nil)
|
p.ICERestart(iceConfig)
|
||||||
|
|
||||||
// check for simulated signal disconnect on resume
|
// check for simulated signal disconnect on resume
|
||||||
r.simulationLock.Lock()
|
r.simulationLock.Lock()
|
||||||
|
|||||||
@@ -431,9 +431,7 @@ func (t *TransportManager) HandleClientReconnect(reason livekit.ReconnectReason)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *TransportManager) ICERestart(iceConfig *livekit.ICEConfig) error {
|
func (t *TransportManager) ICERestart(iceConfig *livekit.ICEConfig) error {
|
||||||
if iceConfig != nil {
|
|
||||||
t.SetICEConfig(iceConfig)
|
t.SetICEConfig(iceConfig)
|
||||||
}
|
|
||||||
|
|
||||||
return t.subscriber.ICERestart()
|
return t.subscriber.ICERestart()
|
||||||
}
|
}
|
||||||
@@ -445,7 +443,9 @@ func (t *TransportManager) OnICEConfigChanged(f func(iceConfig *livekit.ICEConfi
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *TransportManager) SetICEConfig(iceConfig *livekit.ICEConfig) {
|
func (t *TransportManager) SetICEConfig(iceConfig *livekit.ICEConfig) {
|
||||||
|
if iceConfig != nil {
|
||||||
t.configureICE(iceConfig, true)
|
t.configureICE(iceConfig, true)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TransportManager) resetTransportConfigureLocked(reconfigured bool) {
|
func (t *TransportManager) resetTransportConfigureLocked(reconfigured bool) {
|
||||||
|
|||||||
+16
-31
@@ -25,6 +25,7 @@ import (
|
|||||||
"golang.org/x/exp/maps"
|
"golang.org/x/exp/maps"
|
||||||
|
|
||||||
"github.com/livekit/livekit-server/pkg/agent"
|
"github.com/livekit/livekit-server/pkg/agent"
|
||||||
|
sutils "github.com/livekit/livekit-server/pkg/utils"
|
||||||
"github.com/livekit/mediatransportutil/pkg/rtcconfig"
|
"github.com/livekit/mediatransportutil/pkg/rtcconfig"
|
||||||
"github.com/livekit/protocol/auth"
|
"github.com/livekit/protocol/auth"
|
||||||
"github.com/livekit/protocol/livekit"
|
"github.com/livekit/protocol/livekit"
|
||||||
@@ -48,14 +49,13 @@ const (
|
|||||||
roomPurgeSeconds = 24 * 60 * 60
|
roomPurgeSeconds = 24 * 60 * 60
|
||||||
tokenRefreshInterval = 5 * time.Minute
|
tokenRefreshInterval = 5 * time.Minute
|
||||||
tokenDefaultTTL = 10 * time.Minute
|
tokenDefaultTTL = 10 * time.Minute
|
||||||
iceConfigTTL = 5 * time.Minute
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var affinityEpoch = time.Date(2000, 0, 0, 0, 0, 0, 0, time.UTC)
|
var affinityEpoch = time.Date(2000, 0, 0, 0, 0, 0, 0, time.UTC)
|
||||||
|
|
||||||
type iceConfigCacheEntry struct {
|
type iceConfigCacheKey struct {
|
||||||
iceConfig *livekit.ICEConfig
|
roomName livekit.RoomName
|
||||||
modifiedAt time.Time
|
participantIdentity livekit.ParticipantIdentity
|
||||||
}
|
}
|
||||||
|
|
||||||
// RoomManager manages rooms and its interaction with participants.
|
// RoomManager manages rooms and its interaction with participants.
|
||||||
@@ -82,7 +82,7 @@ type RoomManager struct {
|
|||||||
roomServers utils.MultitonService[rpc.RoomTopic]
|
roomServers utils.MultitonService[rpc.RoomTopic]
|
||||||
participantServers utils.MultitonService[rpc.ParticipantTopic]
|
participantServers utils.MultitonService[rpc.ParticipantTopic]
|
||||||
|
|
||||||
iceConfigCache map[livekit.ParticipantIdentity]*iceConfigCacheEntry
|
iceConfigCache *sutils.IceConfigCache[iceConfigCacheKey]
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLocalRoomManager(
|
func NewLocalRoomManager(
|
||||||
@@ -119,7 +119,7 @@ func NewLocalRoomManager(
|
|||||||
|
|
||||||
rooms: make(map[livekit.RoomName]*rtc.Room),
|
rooms: make(map[livekit.RoomName]*rtc.Room),
|
||||||
|
|
||||||
iceConfigCache: make(map[livekit.ParticipantIdentity]*iceConfigCacheEntry),
|
iceConfigCache: sutils.NewIceConfigCache[iceConfigCacheKey](),
|
||||||
|
|
||||||
serverInfo: &livekit.ServerInfo{
|
serverInfo: &livekit.ServerInfo{
|
||||||
Edition: livekit.ServerInfo_Standard,
|
Edition: livekit.ServerInfo_Standard,
|
||||||
@@ -230,6 +230,8 @@ func (r *RoomManager) Stop() {
|
|||||||
_ = r.rtcConfig.TCPMuxListener.Close()
|
_ = r.rtcConfig.TCPMuxListener.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
r.iceConfigCache.Stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
// StartSession starts WebRTC session when a new participant is connected, takes place on RTC node
|
// 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,
|
"reason", pi.ReconnectReason,
|
||||||
"numParticipants", room.GetParticipantCount(),
|
"numParticipants", room.GetParticipantCount(),
|
||||||
)
|
)
|
||||||
iceConfig := r.getIceConfig(participant)
|
iceConfig := r.getIceConfig(roomName, participant)
|
||||||
if iceConfig == nil {
|
|
||||||
iceConfig = &livekit.ICEConfig{}
|
|
||||||
}
|
|
||||||
if err = room.ResumeParticipant(
|
if err = room.ResumeParticipant(
|
||||||
participant,
|
participant,
|
||||||
requestSource,
|
requestSource,
|
||||||
responseSink,
|
responseSink,
|
||||||
|
iceConfig,
|
||||||
r.iceServersForParticipant(
|
r.iceServersForParticipant(
|
||||||
apiKey,
|
apiKey,
|
||||||
participant,
|
participant,
|
||||||
@@ -444,7 +444,7 @@ func (r *RoomManager) StartSession(
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
iceConfig := r.setIceConfig(participant)
|
iceConfig := r.setIceConfig(roomName, participant)
|
||||||
|
|
||||||
// join room
|
// join room
|
||||||
opts := rtc.ParticipantOptions{
|
opts := rtc.ParticipantOptions{
|
||||||
@@ -504,12 +504,7 @@ func (r *RoomManager) StartSession(
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
participant.OnICEConfigChanged(func(participant types.LocalParticipant, iceConfig *livekit.ICEConfig) {
|
participant.OnICEConfigChanged(func(participant types.LocalParticipant, iceConfig *livekit.ICEConfig) {
|
||||||
r.lock.Lock()
|
r.iceConfigCache.Put(iceConfigCacheKey{roomName, participant.Identity()}, iceConfig, time.Now())
|
||||||
r.iceConfigCache[participant.Identity()] = &iceConfigCacheEntry{
|
|
||||||
iceConfig: iceConfig,
|
|
||||||
modifiedAt: time.Now(),
|
|
||||||
}
|
|
||||||
r.lock.Unlock()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
go r.rtcSessionWorker(room, participant, requestSource)
|
go r.rtcSessionWorker(room, participant, requestSource)
|
||||||
@@ -871,24 +866,14 @@ func (r *RoomManager) refreshToken(participant types.LocalParticipant) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RoomManager) setIceConfig(participant types.LocalParticipant) *livekit.ICEConfig {
|
func (r *RoomManager) setIceConfig(roomName livekit.RoomName, participant types.LocalParticipant) *livekit.ICEConfig {
|
||||||
iceConfig := r.getIceConfig(participant)
|
iceConfig := r.getIceConfig(roomName, participant)
|
||||||
if iceConfig == nil {
|
|
||||||
return &livekit.ICEConfig{}
|
|
||||||
}
|
|
||||||
participant.SetICEConfig(iceConfig)
|
participant.SetICEConfig(iceConfig)
|
||||||
return iceConfig
|
return iceConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *RoomManager) getIceConfig(participant types.LocalParticipant) *livekit.ICEConfig {
|
func (r *RoomManager) getIceConfig(roomName livekit.RoomName, participant types.LocalParticipant) *livekit.ICEConfig {
|
||||||
r.lock.Lock()
|
return r.iceConfigCache.Get(iceConfigCacheKey{roomName, participant.Identity()})
|
||||||
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) getFirstKeyPair() (string, string, error) {
|
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