Fixed reconnection for single node

It does not yet support resuming the session.
This commit is contained in:
David Zhao
2021-09-24 14:19:23 -07:00
parent d83b7b543d
commit cdb04248fb
3 changed files with 40 additions and 11 deletions
+20 -4
View File
@@ -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()
+18 -7
View File
@@ -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()
+2
View File
@@ -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
}