avoid sending to closed channels
This commit is contained in:
@@ -2,11 +2,13 @@ package routing
|
||||
|
||||
import (
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/utils"
|
||||
)
|
||||
|
||||
type MessageChannel struct {
|
||||
msgChan chan proto.Message
|
||||
isClosed bool
|
||||
isClosed utils.AtomicFlag
|
||||
onClose func()
|
||||
}
|
||||
|
||||
@@ -22,7 +24,7 @@ func (m *MessageChannel) OnClose(f func()) {
|
||||
}
|
||||
|
||||
func (m *MessageChannel) WriteMessage(msg proto.Message) error {
|
||||
if m.isClosed {
|
||||
if m.isClosed.Get() {
|
||||
return ErrChannelClosed
|
||||
}
|
||||
m.msgChan <- msg
|
||||
@@ -34,7 +36,9 @@ func (m *MessageChannel) ReadChan() <-chan proto.Message {
|
||||
}
|
||||
|
||||
func (m *MessageChannel) Close() {
|
||||
m.isClosed = true
|
||||
if !m.isClosed.TrySet(true) {
|
||||
return
|
||||
}
|
||||
close(m.msgChan)
|
||||
if m.onClose != nil {
|
||||
m.onClose()
|
||||
|
||||
+10
-12
@@ -2,11 +2,11 @@ package routing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"google.golang.org/protobuf/proto"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/utils"
|
||||
"github.com/livekit/livekit-server/proto/livekit"
|
||||
)
|
||||
|
||||
@@ -69,8 +69,7 @@ type RedisSink struct {
|
||||
rc *redis.Client
|
||||
nodeId string
|
||||
participantId string
|
||||
isClosed bool
|
||||
once sync.Once
|
||||
isClosed utils.AtomicFlag
|
||||
onClose func()
|
||||
}
|
||||
|
||||
@@ -78,26 +77,25 @@ func NewRedisSink(rc *redis.Client, nodeId, participantId string) *RedisSink {
|
||||
return &RedisSink{
|
||||
rc: rc,
|
||||
nodeId: nodeId,
|
||||
once: sync.Once{},
|
||||
participantId: participantId,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *RedisSink) WriteMessage(msg proto.Message) error {
|
||||
if s.isClosed {
|
||||
if s.isClosed.Get() {
|
||||
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()
|
||||
}
|
||||
})
|
||||
if !s.isClosed.TrySet(true) {
|
||||
return
|
||||
}
|
||||
publishRouterMessage(s.rc, s.nodeId, s.participantId, &livekit.EndSession{})
|
||||
if s.onClose != nil {
|
||||
s.onClose()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *RedisSink) OnClose(f func()) {
|
||||
|
||||
@@ -261,18 +261,28 @@ func (r *RedisRouter) statsWorker() {
|
||||
func (r *RedisRouter) subscribeWorker() {
|
||||
sub := r.rc.Subscribe(redisCtx, nodeChannel(r.currentNode.Id))
|
||||
|
||||
defer func() {
|
||||
logger.Debugw("finishing redis subscribeWorker", "node", r.currentNode.Id)
|
||||
}()
|
||||
logger.Debugw("starting redis subscribeWorker", "node", r.currentNode.Id)
|
||||
for r.ctx.Err() == nil {
|
||||
obj, err := sub.Receive(r.ctx)
|
||||
if err != nil {
|
||||
logger.Warnw("error receiving redis message", "error", err)
|
||||
// TODO: retry? ignore? at a minimum need to sleep here to retry
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
time.Sleep(time.Second)
|
||||
continue
|
||||
}
|
||||
if obj == nil {
|
||||
return
|
||||
}
|
||||
|
||||
msg, ok := obj.(*redis.Message)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
logger.Debugw("received redis message", "message", msg, "node", r.currentNode.Id)
|
||||
rm := livekit.RouterMessage{}
|
||||
err = proto.Unmarshal([]byte(msg.Payload), &rm)
|
||||
pId := rm.ParticipantId
|
||||
|
||||
Reference in New Issue
Block a user