Configurable limit for number of tracks (#197)
* configurable node track limit * sample config * todos * end of file new line * default max num tracks * bandwidth limit * client message for limit exceeded node * 10 Gbps default network limit
This commit is contained in:
@@ -77,6 +77,11 @@ func (r *StandardRoomAllocator) CreateRoom(ctx context.Context, req *livekit.Cre
|
||||
|
||||
// if already assigned and still available, keep it on that node
|
||||
if err == nil && selector.IsAvailable(existing) {
|
||||
// if node hosting the room is full, deny entry
|
||||
if selector.LimitsReached(r.config.Limit, existing.Stats) {
|
||||
return nil, routing.ErrNodeLimitReached
|
||||
}
|
||||
|
||||
return rm, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -15,24 +15,58 @@ import (
|
||||
)
|
||||
|
||||
func TestCreateRoom(t *testing.T) {
|
||||
ra, conf := newTestRoomAllocator(t)
|
||||
|
||||
t.Run("ensure default room settings are applied", func(t *testing.T) {
|
||||
conf, err := config.NewConfig("", nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
node, err := routing.NewLocalNode(conf)
|
||||
require.NoError(t, err)
|
||||
|
||||
ra, conf := newTestRoomAllocator(t, conf, node)
|
||||
|
||||
room, err := ra.CreateRoom(context.Background(), &livekit.CreateRoomRequest{Name: "myroom"})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, conf.Room.EmptyTimeout, room.EmptyTimeout)
|
||||
require.NotEmpty(t, room.EnabledCodecs)
|
||||
})
|
||||
|
||||
t.Run("reject new participants when track limit has been reached", func(t *testing.T) {
|
||||
conf, err := config.NewConfig("", nil)
|
||||
require.NoError(t, err)
|
||||
conf.Limit.NumTracks = 10
|
||||
|
||||
node, err := routing.NewLocalNode(conf)
|
||||
require.NoError(t, err)
|
||||
node.Stats.NumTracksIn = 100
|
||||
node.Stats.NumTracksOut = 100
|
||||
|
||||
ra, conf := newTestRoomAllocator(t, conf, node)
|
||||
|
||||
_, err = ra.CreateRoom(context.Background(), &livekit.CreateRoomRequest{Name: "low-limit-room"})
|
||||
require.ErrorIs(t, err, routing.ErrNodeLimitReached)
|
||||
})
|
||||
|
||||
t.Run("reject new participants when bandwidth limit has been reached", func(t *testing.T) {
|
||||
conf, err := config.NewConfig("", nil)
|
||||
require.NoError(t, err)
|
||||
conf.Limit.BytesPerSec = 100
|
||||
|
||||
node, err := routing.NewLocalNode(conf)
|
||||
require.NoError(t, err)
|
||||
node.Stats.BytesInPerSec = 1000
|
||||
node.Stats.BytesOutPerSec = 1000
|
||||
|
||||
ra, conf := newTestRoomAllocator(t, conf, node)
|
||||
|
||||
_, err = ra.CreateRoom(context.Background(), &livekit.CreateRoomRequest{Name: "low-limit-room"})
|
||||
require.ErrorIs(t, err, routing.ErrNodeLimitReached)
|
||||
})
|
||||
}
|
||||
|
||||
func newTestRoomAllocator(t *testing.T) (service.RoomAllocator, *config.Config) {
|
||||
func newTestRoomAllocator(t *testing.T, conf *config.Config, node *livekit.Node) (service.RoomAllocator, *config.Config) {
|
||||
store := &servicefakes.FakeRoomStore{}
|
||||
store.LoadRoomReturns(nil, service.ErrRoomNotFound)
|
||||
router := &routingfakes.FakeRouter{}
|
||||
conf, err := config.NewConfig("", nil)
|
||||
require.NoError(t, err)
|
||||
node, err := routing.NewLocalNode(conf)
|
||||
require.NoError(t, err)
|
||||
|
||||
router.GetNodeForRoomReturns(node, nil)
|
||||
|
||||
|
||||
@@ -14,26 +14,29 @@ import (
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/config"
|
||||
"github.com/livekit/livekit-server/pkg/routing"
|
||||
"github.com/livekit/livekit-server/pkg/routing/selector"
|
||||
"github.com/livekit/livekit-server/pkg/rtc"
|
||||
"github.com/livekit/livekit-server/pkg/rtc/types"
|
||||
"github.com/livekit/livekit-server/pkg/telemetry/prometheus"
|
||||
)
|
||||
|
||||
type RTCService struct {
|
||||
router routing.MessageRouter
|
||||
router routing.Router
|
||||
roomAllocator RoomAllocator
|
||||
upgrader websocket.Upgrader
|
||||
currentNode routing.LocalNode
|
||||
isDev bool
|
||||
limits config.LimitConfig
|
||||
}
|
||||
|
||||
func NewRTCService(conf *config.Config, ra RoomAllocator, router routing.MessageRouter, currentNode routing.LocalNode) *RTCService {
|
||||
func NewRTCService(conf *config.Config, ra RoomAllocator, router routing.Router, currentNode routing.LocalNode) *RTCService {
|
||||
s := &RTCService{
|
||||
router: router,
|
||||
roomAllocator: ra,
|
||||
upgrader: websocket.Upgrader{},
|
||||
currentNode: currentNode,
|
||||
isDev: conf.Development,
|
||||
limits: conf.Limit,
|
||||
}
|
||||
|
||||
// allow connections from any origin, since script may be hosted anywhere
|
||||
@@ -74,6 +77,12 @@ func (s *RTCService) validate(r *http.Request) (string, routing.ParticipantInit,
|
||||
roomName = onlyName
|
||||
}
|
||||
|
||||
if foundNode, err := s.router.GetNodeForRoom(r.Context(), roomName); err == nil {
|
||||
if selector.LimitsReached(s.limits, foundNode.Stats) {
|
||||
return "", routing.ParticipantInit{}, http.StatusServiceUnavailable, rtc.ErrLimitExceeded
|
||||
}
|
||||
}
|
||||
|
||||
pi := routing.ParticipantInit{
|
||||
Reconnect: boolValue(reconnectParam),
|
||||
Identity: claims.Identity,
|
||||
|
||||
Reference in New Issue
Block a user