cleanup room state, proper participant termination
This commit is contained in:
@@ -5,6 +5,7 @@ import "errors"
|
||||
var (
|
||||
ErrNotFound = errors.New("could not find object")
|
||||
ErrHandlerNotDefined = errors.New("handler not defined")
|
||||
ErrNoAvailableNodes = errors.New("could not find any available nodes")
|
||||
ErrIncorrectNodeForRoom = errors.New("incorrect node for the current room")
|
||||
errInvalidRouterMessage = errors.New("invalid router message")
|
||||
)
|
||||
|
||||
@@ -8,7 +8,8 @@ import (
|
||||
|
||||
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
|
||||
|
||||
// routes signaling message
|
||||
// MessageSink is an abstraction for writing protobuf messages and having them read by a MessageSource,
|
||||
// potentially on a different node via a transport
|
||||
//counterfeiter:generate . MessageSink
|
||||
type MessageSink interface {
|
||||
WriteMessage(msg proto.Message) error
|
||||
@@ -23,10 +24,12 @@ type MessageSource interface {
|
||||
|
||||
type ParticipantCallback func(roomId, participantId, participantName string, requestSource MessageSource, responseSink MessageSink)
|
||||
|
||||
// Router allows multiple nodes to coordinate the participant session
|
||||
//counterfeiter:generate . Router
|
||||
type Router interface {
|
||||
GetNodeForRoom(roomName string) (string, error)
|
||||
SetNodeForRoom(roomName string, nodeId string) error
|
||||
ClearRoomState(roomName string) error
|
||||
RegisterNode() error
|
||||
UnregisterNode() error
|
||||
GetNode(nodeId string) (*livekit.Node, error)
|
||||
@@ -43,3 +46,9 @@ type Router interface {
|
||||
Start() error
|
||||
Stop()
|
||||
}
|
||||
|
||||
// NodeSelector selects an appropriate node to run the current session
|
||||
//counterfeiter:generate . NodeBalancer
|
||||
type NodeSelector interface {
|
||||
SelectNode(nodes []*livekit.Node, room *livekit.Room) (*livekit.Node, error)
|
||||
}
|
||||
|
||||
@@ -33,6 +33,11 @@ func (r *LocalRouter) SetNodeForRoom(roomName string, nodeId string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *LocalRouter) ClearRoomState(roomName string) error {
|
||||
// do nothing
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *LocalRouter) RegisterNode() error {
|
||||
return nil
|
||||
}
|
||||
@@ -105,11 +110,11 @@ func (r *LocalRouter) getOrCreateMessageChannel(target map[string]*MessageChanne
|
||||
}
|
||||
|
||||
mc = NewMessageChannel()
|
||||
mc.onClose = func() {
|
||||
mc.OnClose(func() {
|
||||
r.lock.Lock()
|
||||
delete(target, participantId)
|
||||
r.lock.Unlock()
|
||||
}
|
||||
})
|
||||
r.lock.Lock()
|
||||
target[participantId] = mc
|
||||
r.lock.Unlock()
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
"github.com/thoas/go-funk"
|
||||
|
||||
"github.com/livekit/livekit-server/proto/livekit"
|
||||
)
|
||||
|
||||
type RandomSelector struct {
|
||||
}
|
||||
|
||||
func (s *RandomSelector) SelectNode(nodes []*livekit.Node, room *livekit.Room) (*livekit.Node, error) {
|
||||
nodes = GetAvailableNodes(nodes)
|
||||
if len(nodes) == 0 {
|
||||
return nil, ErrNoAvailableNodes
|
||||
}
|
||||
|
||||
idx := funk.RandomInt(0, len(nodes))
|
||||
return nodes[idx], nil
|
||||
}
|
||||
@@ -17,10 +17,12 @@ import (
|
||||
const (
|
||||
// expire participant mappings after a day
|
||||
participantMappingTTL = 24 * time.Hour
|
||||
statsUpdateInterval = 10 * time.Second
|
||||
)
|
||||
|
||||
// TODO: need to implement redis key cleanup, for when clients disconnect and such
|
||||
|
||||
// RedisRouter uses Redis pub/sub to route signaling messages across different nodes
|
||||
// It relies on the RTC node to be the primary driver of the participant connection.
|
||||
// Because
|
||||
type RedisRouter struct {
|
||||
LocalRouter
|
||||
useLocal bool
|
||||
@@ -72,10 +74,16 @@ func (r *RedisRouter) GetNodeForRoom(roomName string) (string, error) {
|
||||
}
|
||||
|
||||
func (r *RedisRouter) SetNodeForRoom(roomName string, nodeId string) error {
|
||||
// TODO: how do we clear this periodically to remove old rooms?
|
||||
return r.rc.HSet(r.ctx, NodeRoomKey, roomName, nodeId).Err()
|
||||
}
|
||||
|
||||
func (r *RedisRouter) ClearRoomState(roomName string) error {
|
||||
if err := r.rc.HDel(r.ctx, NodeRoomKey, roomName).Err(); err != nil {
|
||||
return errors.Wrap(err, "could not clear room state")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RedisRouter) GetNode(nodeId string) (*livekit.Node, error) {
|
||||
data, err := r.cr.CachedHGet(NodesKey, nodeId)
|
||||
if err != nil {
|
||||
@@ -242,7 +250,7 @@ func (r *RedisRouter) getParticipantSignalNode(participantId string) (nodeId str
|
||||
func (r *RedisRouter) statsWorker() {
|
||||
for r.ctx.Err() == nil {
|
||||
// update every 10 seconds
|
||||
<-time.After(10 * time.Second)
|
||||
<-time.After(statsUpdateInterval)
|
||||
r.currentNode.Stats.UpdatedAt = time.Now().Unix()
|
||||
if err := r.RegisterNode(); err != nil {
|
||||
logger.Errorw("could not update node", "error", err)
|
||||
|
||||
@@ -9,6 +9,17 @@ import (
|
||||
)
|
||||
|
||||
type FakeRouter struct {
|
||||
ClearRoomStateStub func(string) error
|
||||
clearRoomStateMutex sync.RWMutex
|
||||
clearRoomStateArgsForCall []struct {
|
||||
arg1 string
|
||||
}
|
||||
clearRoomStateReturns struct {
|
||||
result1 error
|
||||
}
|
||||
clearRoomStateReturnsOnCall map[int]struct {
|
||||
result1 error
|
||||
}
|
||||
GetNodeStub func(string) (*livekit.Node, error)
|
||||
getNodeMutex sync.RWMutex
|
||||
getNodeArgsForCall []struct {
|
||||
@@ -153,6 +164,67 @@ type FakeRouter struct {
|
||||
invocationsMutex sync.RWMutex
|
||||
}
|
||||
|
||||
func (fake *FakeRouter) ClearRoomState(arg1 string) error {
|
||||
fake.clearRoomStateMutex.Lock()
|
||||
ret, specificReturn := fake.clearRoomStateReturnsOnCall[len(fake.clearRoomStateArgsForCall)]
|
||||
fake.clearRoomStateArgsForCall = append(fake.clearRoomStateArgsForCall, struct {
|
||||
arg1 string
|
||||
}{arg1})
|
||||
stub := fake.ClearRoomStateStub
|
||||
fakeReturns := fake.clearRoomStateReturns
|
||||
fake.recordInvocation("ClearRoomState", []interface{}{arg1})
|
||||
fake.clearRoomStateMutex.Unlock()
|
||||
if stub != nil {
|
||||
return stub(arg1)
|
||||
}
|
||||
if specificReturn {
|
||||
return ret.result1
|
||||
}
|
||||
return fakeReturns.result1
|
||||
}
|
||||
|
||||
func (fake *FakeRouter) ClearRoomStateCallCount() int {
|
||||
fake.clearRoomStateMutex.RLock()
|
||||
defer fake.clearRoomStateMutex.RUnlock()
|
||||
return len(fake.clearRoomStateArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakeRouter) ClearRoomStateCalls(stub func(string) error) {
|
||||
fake.clearRoomStateMutex.Lock()
|
||||
defer fake.clearRoomStateMutex.Unlock()
|
||||
fake.ClearRoomStateStub = stub
|
||||
}
|
||||
|
||||
func (fake *FakeRouter) ClearRoomStateArgsForCall(i int) string {
|
||||
fake.clearRoomStateMutex.RLock()
|
||||
defer fake.clearRoomStateMutex.RUnlock()
|
||||
argsForCall := fake.clearRoomStateArgsForCall[i]
|
||||
return argsForCall.arg1
|
||||
}
|
||||
|
||||
func (fake *FakeRouter) ClearRoomStateReturns(result1 error) {
|
||||
fake.clearRoomStateMutex.Lock()
|
||||
defer fake.clearRoomStateMutex.Unlock()
|
||||
fake.ClearRoomStateStub = nil
|
||||
fake.clearRoomStateReturns = struct {
|
||||
result1 error
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeRouter) ClearRoomStateReturnsOnCall(i int, result1 error) {
|
||||
fake.clearRoomStateMutex.Lock()
|
||||
defer fake.clearRoomStateMutex.Unlock()
|
||||
fake.ClearRoomStateStub = nil
|
||||
if fake.clearRoomStateReturnsOnCall == nil {
|
||||
fake.clearRoomStateReturnsOnCall = make(map[int]struct {
|
||||
result1 error
|
||||
})
|
||||
}
|
||||
fake.clearRoomStateReturnsOnCall[i] = struct {
|
||||
result1 error
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeRouter) GetNode(arg1 string) (*livekit.Node, error) {
|
||||
fake.getNodeMutex.Lock()
|
||||
ret, specificReturn := fake.getNodeReturnsOnCall[len(fake.getNodeArgsForCall)]
|
||||
@@ -870,6 +942,8 @@ func (fake *FakeRouter) UnregisterNodeReturnsOnCall(i int, result1 error) {
|
||||
func (fake *FakeRouter) Invocations() map[string][][]interface{} {
|
||||
fake.invocationsMutex.RLock()
|
||||
defer fake.invocationsMutex.RUnlock()
|
||||
fake.clearRoomStateMutex.RLock()
|
||||
defer fake.clearRoomStateMutex.RUnlock()
|
||||
fake.getNodeMutex.RLock()
|
||||
defer fake.getNodeMutex.RUnlock()
|
||||
fake.getNodeForRoomMutex.RLock()
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package routing
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/thoas/go-funk"
|
||||
|
||||
"github.com/livekit/livekit-server/proto/livekit"
|
||||
)
|
||||
|
||||
// checks if a node has been updated recently to be considered for selection
|
||||
func IsAvailable(node *livekit.Node) bool {
|
||||
delta := time.Now().Unix() - node.Stats.UpdatedAt
|
||||
limit := statsUpdateInterval.Seconds() * 2
|
||||
return float64(delta) < limit
|
||||
}
|
||||
|
||||
func GetAvailableNodes(nodes []*livekit.Node) []*livekit.Node {
|
||||
return funk.Filter(nodes, func(node *livekit.Node) bool {
|
||||
return IsAvailable(node)
|
||||
}).([]*livekit.Node)
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package routing_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/livekit-server/proto/livekit"
|
||||
)
|
||||
|
||||
func TestIsAvailable(t *testing.T) {
|
||||
t.Run("still available", func(t *testing.T) {
|
||||
n := &livekit.Node{
|
||||
Stats: &livekit.NodeStats{
|
||||
UpdatedAt: time.Now().Unix() - 10,
|
||||
},
|
||||
}
|
||||
assert.True(t, routing.IsAvailable(n))
|
||||
})
|
||||
|
||||
t.Run("expired", func(t *testing.T) {
|
||||
n := &livekit.Node{
|
||||
Stats: &livekit.NodeStats{
|
||||
UpdatedAt: time.Now().Unix() - 20,
|
||||
},
|
||||
}
|
||||
assert.False(t, routing.IsAvailable(n))
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user