fixed message channel deadlock
This commit is contained in:
@@ -9,4 +9,5 @@ var (
|
|||||||
ErrIncorrectRTCNode = errors.New("current node isn't the RTC node for the room")
|
ErrIncorrectRTCNode = errors.New("current node isn't the RTC node for the room")
|
||||||
errInvalidRouterMessage = errors.New("invalid router message")
|
errInvalidRouterMessage = errors.New("invalid router message")
|
||||||
ErrChannelClosed = errors.New("channel closed")
|
ErrChannelClosed = errors.New("channel closed")
|
||||||
|
ErrChannelFull = errors.New("channel is full")
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ type MessageChannel struct {
|
|||||||
func NewMessageChannel() *MessageChannel {
|
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, 2),
|
msgChan: make(chan proto.Message, 10),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,8 +27,15 @@ func (m *MessageChannel) WriteMessage(msg proto.Message) error {
|
|||||||
if m.isClosed.Get() {
|
if m.isClosed.Get() {
|
||||||
return ErrChannelClosed
|
return ErrChannelClosed
|
||||||
}
|
}
|
||||||
m.msgChan <- msg
|
|
||||||
return nil
|
select {
|
||||||
|
case m.msgChan <- msg:
|
||||||
|
// published
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
// channel is full
|
||||||
|
return ErrChannelFull
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MessageChannel) ReadChan() <-chan proto.Message {
|
func (m *MessageChannel) ReadChan() <-chan proto.Message {
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ func publishRTCMessage(rc *redis.Client, nodeId string, participantKey string, m
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
//logger.Debugw("publishing to", "rtcChannel", rtcNodeChannel(nodeId),
|
//logger.Debugw("publishing to rtc", "rtcChannel", rtcNodeChannel(nodeId),
|
||||||
// "message", rm.Message)
|
// "message", rm.Message)
|
||||||
return rc.Publish(redisCtx, rtcNodeChannel(nodeId), data).Err()
|
return rc.Publish(redisCtx, rtcNodeChannel(nodeId), data).Err()
|
||||||
}
|
}
|
||||||
@@ -84,6 +84,9 @@ func publishSignalMessage(rc *redis.Client, nodeId string, connectionId string,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//logger.Debugw("publishing to signal", "signalChannel", signalNodeChannel(nodeId),
|
||||||
|
// "message", rm.Message)
|
||||||
return rc.Publish(redisCtx, signalNodeChannel(nodeId), data).Err()
|
return rc.Publish(redisCtx, signalNodeChannel(nodeId), data).Err()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -351,21 +351,27 @@ func (r *RedisRouter) redisWorker() {
|
|||||||
func (r *RedisRouter) handleSignalMessage(sm *livekit.SignalNodeMessage) error {
|
func (r *RedisRouter) handleSignalMessage(sm *livekit.SignalNodeMessage) error {
|
||||||
connectionId := sm.ConnectionId
|
connectionId := sm.ConnectionId
|
||||||
|
|
||||||
|
r.lock.Lock()
|
||||||
|
resSink := r.responseChannels[connectionId]
|
||||||
|
r.lock.Unlock()
|
||||||
|
|
||||||
|
// if a client closed the channel, then sent more messages after that,
|
||||||
|
if resSink == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
switch rmb := sm.Message.(type) {
|
switch rmb := sm.Message.(type) {
|
||||||
case *livekit.SignalNodeMessage_Response:
|
case *livekit.SignalNodeMessage_Response:
|
||||||
//logger.Debugw("forwarding signal message",
|
//logger.Debugw("forwarding signal message",
|
||||||
// "connectionId", connectionId,
|
// "connectionId", connectionId,
|
||||||
// "type", fmt.Sprintf("%T", rmb.Response.Message))
|
// "type", fmt.Sprintf("%T", rmb.Response.Message))
|
||||||
// in the event the current node is an Signal node, push to response channels
|
|
||||||
resSink := r.getOrCreateMessageChannel(r.responseChannels, connectionId)
|
|
||||||
if err := resSink.WriteMessage(rmb.Response); err != nil {
|
if err := resSink.WriteMessage(rmb.Response); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
case *livekit.SignalNodeMessage_EndSession:
|
case *livekit.SignalNodeMessage_EndSession:
|
||||||
logger.Debugw("received EndSession, closing signal connection",
|
//logger.Debugw("received EndSession, closing signal connection",
|
||||||
"connectionId", connectionId)
|
// "connectionId", connectionId)
|
||||||
resSink := r.getOrCreateMessageChannel(r.responseChannels, connectionId)
|
|
||||||
resSink.Close()
|
resSink.Close()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -382,7 +388,9 @@ func (r *RedisRouter) handleRTCMessage(rm *livekit.RTCNodeMessage) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case *livekit.RTCNodeMessage_Request:
|
case *livekit.RTCNodeMessage_Request:
|
||||||
requestChan := r.getOrCreateMessageChannel(r.requestChannels, pKey)
|
r.lock.Lock()
|
||||||
|
requestChan := r.requestChannels[pKey]
|
||||||
|
r.lock.Unlock()
|
||||||
if err := requestChan.WriteMessage(rmb.Request); err != nil {
|
if err := requestChan.WriteMessage(rmb.Request); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user