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 // index channels by roomName | identity
key := participantKey(roomName, pi.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( r.onNewParticipant(
ctx, 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 { 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 // create a new one
r.rtcMessageChan = NewMessageChannel() r.rtcMessageChan = NewMessageChannel()
} }
@@ -164,7 +174,7 @@ func (r *LocalRouter) rtcMessageWorker() {
go r.rtcMessageWorker() go r.rtcMessageWorker()
}() }()
if r.rtcMessageChan.isClosed.Get() { if r.rtcMessageChan.IsClosed() {
// sleep and retry // sleep and retry
time.Sleep(time.Second) 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 { func (r *LocalRouter) getOrCreateMessageChannel(target map[string]*MessageChannel, key string) *MessageChannel {
r.lock.Lock() r.lock.Lock()
defer r.lock.Unlock() defer r.lock.Unlock()
+16 -5
View File
@@ -2,13 +2,11 @@ package routing
import ( import (
"google.golang.org/protobuf/proto" "google.golang.org/protobuf/proto"
"github.com/livekit/protocol/utils"
) )
type MessageChannel struct { type MessageChannel struct {
msgChan chan proto.Message msgChan chan proto.Message
isClosed utils.AtomicFlag closed chan struct{}
onClose func() onClose func()
} }
@@ -16,6 +14,7 @@ func NewMessageChannel() *MessageChannel {
return &MessageChannel{ return &MessageChannel{
// allow some buffer to avoid blocked writes // allow some buffer to avoid blocked writes
msgChan: make(chan proto.Message, 200), msgChan: make(chan proto.Message, 200),
closed: make(chan struct{}),
} }
} }
@@ -23,12 +22,23 @@ func (m *MessageChannel) OnClose(f func()) {
m.onClose = f 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 { func (m *MessageChannel) WriteMessage(msg proto.Message) error {
if m.isClosed.Get() { if m.IsClosed() {
return ErrChannelClosed return ErrChannelClosed
} }
select { select {
case <-m.closed:
return ErrChannelClosed
case m.msgChan <- msg: case m.msgChan <- msg:
// published // published
return nil return nil
@@ -43,9 +53,10 @@ func (m *MessageChannel) ReadChan() <-chan proto.Message {
} }
func (m *MessageChannel) Close() { func (m *MessageChannel) Close() {
if !m.isClosed.TrySet(true) { if m.IsClosed() {
return return
} }
close(m.closed)
close(m.msgChan) close(m.msgChan)
if m.onClose != nil { if m.onClose != nil {
m.onClose() m.onClose()
+2
View File
@@ -359,6 +359,8 @@ func (r *LocalRoomManager) rtcSessionWorker(room *rtc.Room, participant types.Pa
return return
} }
case obj := <-requestSource.ReadChan(): 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 { if obj == nil {
return return
} }