diff --git a/pkg/routing/localrouter.go b/pkg/routing/localrouter.go index 718aa925..b4d6e2de 100644 --- a/pkg/routing/localrouter.go +++ b/pkg/routing/localrouter.go @@ -85,8 +85,18 @@ func (r *LocalRouter) StartParticipantSignal(ctx context.Context, roomName strin // index channels by roomName | identity key := participantKey(roomName, pi.Identity) - reqChan := r.getOrCreateMessageChannel(r.requestChannels, key) - resChan := r.getOrCreateMessageChannel(r.responseChannels, key) + + // close older channels if one already exists + reqChan := r.getMessageChannel(r.requestChannels, key) + if reqChan != nil { + reqChan.Close() + } + resChan := r.getMessageChannel(r.responseChannels, key) + if resChan != nil { + resChan.Close() + } + reqChan = r.getOrCreateMessageChannel(r.requestChannels, key) + resChan = r.getOrCreateMessageChannel(r.responseChannels, key) r.onNewParticipant( ctx, @@ -101,7 +111,7 @@ func (r *LocalRouter) StartParticipantSignal(ctx context.Context, roomName strin } func (r *LocalRouter) WriteRTCMessage(ctx context.Context, roomName, identity string, msg *livekit.RTCNodeMessage) error { - if r.rtcMessageChan.isClosed.Get() { + if r.rtcMessageChan.IsClosed() { // create a new one r.rtcMessageChan = NewMessageChannel() } @@ -164,7 +174,7 @@ func (r *LocalRouter) rtcMessageWorker() { go r.rtcMessageWorker() }() - if r.rtcMessageChan.isClosed.Get() { + if r.rtcMessageChan.IsClosed() { // sleep and retry time.Sleep(time.Second) } @@ -184,6 +194,12 @@ func (r *LocalRouter) rtcMessageWorker() { } } +func (r *LocalRouter) getMessageChannel(target map[string]*MessageChannel, key string) *MessageChannel { + r.lock.RLock() + defer r.lock.RUnlock() + return target[key] +} + func (r *LocalRouter) getOrCreateMessageChannel(target map[string]*MessageChannel, key string) *MessageChannel { r.lock.Lock() defer r.lock.Unlock() diff --git a/pkg/routing/messagechannel.go b/pkg/routing/messagechannel.go index c53fc0dc..074d18ec 100644 --- a/pkg/routing/messagechannel.go +++ b/pkg/routing/messagechannel.go @@ -2,20 +2,19 @@ package routing import ( "google.golang.org/protobuf/proto" - - "github.com/livekit/protocol/utils" ) type MessageChannel struct { - msgChan chan proto.Message - isClosed utils.AtomicFlag - onClose func() + msgChan chan proto.Message + closed chan struct{} + onClose func() } func NewMessageChannel() *MessageChannel { return &MessageChannel{ // allow some buffer to avoid blocked writes msgChan: make(chan proto.Message, 200), + closed: make(chan struct{}), } } @@ -23,12 +22,23 @@ func (m *MessageChannel) OnClose(f func()) { m.onClose = f } +func (m *MessageChannel) IsClosed() bool { + select { + case <-m.closed: + return true + default: + return false + } +} + func (m *MessageChannel) WriteMessage(msg proto.Message) error { - if m.isClosed.Get() { + if m.IsClosed() { return ErrChannelClosed } select { + case <-m.closed: + return ErrChannelClosed case m.msgChan <- msg: // published return nil @@ -43,9 +53,10 @@ func (m *MessageChannel) ReadChan() <-chan proto.Message { } func (m *MessageChannel) Close() { - if !m.isClosed.TrySet(true) { + if m.IsClosed() { return } + close(m.closed) close(m.msgChan) if m.onClose != nil { m.onClose() diff --git a/pkg/service/roommanager.go b/pkg/service/roommanager.go index 72c75852..1148433b 100644 --- a/pkg/service/roommanager.go +++ b/pkg/service/roommanager.go @@ -359,6 +359,8 @@ func (r *LocalRoomManager) rtcSessionWorker(room *rtc.Room, participant types.Pa return } case obj := <-requestSource.ReadChan(): + // In single node mode, the request source is directly tied to the signal message channel + // this means ICE restart isn't possible in single node mode if obj == nil { return }