fixed panic when client connected abruptly

This commit is contained in:
David Zhao
2021-01-26 17:38:47 -08:00
parent b96028b4d6
commit a065a01592
10 changed files with 54 additions and 10 deletions
+1
View File
@@ -8,4 +8,5 @@ var (
ErrNoAvailableNodes = errors.New("could not find any available nodes")
ErrIncorrectNodeForRoom = errors.New("incorrect node for the current room")
errInvalidRouterMessage = errors.New("invalid router message")
ErrChannelClosed = errors.New("channel closed")
)
+9 -3
View File
@@ -5,13 +5,15 @@ import (
)
type MessageChannel struct {
msgChan chan proto.Message
onClose func()
msgChan chan proto.Message
isClosed bool
onClose func()
}
func NewMessageChannel() *MessageChannel {
return &MessageChannel{
msgChan: make(chan proto.Message, 1),
// allow some buffer to avoid blocked writes
msgChan: make(chan proto.Message, 2),
}
}
@@ -20,6 +22,9 @@ func (m *MessageChannel) OnClose(f func()) {
}
func (m *MessageChannel) WriteMessage(msg proto.Message) error {
if m.isClosed {
return ErrChannelClosed
}
m.msgChan <- msg
return nil
}
@@ -29,6 +34,7 @@ func (m *MessageChannel) ReadChan() <-chan proto.Message {
}
func (m *MessageChannel) Close() {
m.isClosed = true
close(m.msgChan)
if m.onClose != nil {
m.onClose()
+5
View File
@@ -69,6 +69,7 @@ type RedisSink struct {
rc *redis.Client
nodeId string
participantId string
isClosed bool
once sync.Once
onClose func()
}
@@ -83,12 +84,16 @@ func NewRedisSink(rc *redis.Client, nodeId, participantId string) *RedisSink {
}
func (s *RedisSink) WriteMessage(msg proto.Message) error {
if s.isClosed {
return ErrChannelClosed
}
return publishRouterMessage(s.rc, s.nodeId, s.participantId, msg)
}
func (s *RedisSink) Close() {
s.once.Do(func() {
publishRouterMessage(s.rc, s.nodeId, s.participantId, &livekit.EndSession{})
s.isClosed = true
if s.onClose != nil {
s.onClose()
}