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:
@@ -137,3 +137,11 @@ keys:
|
||||
# - name: us-west-2
|
||||
# lat: 44.19434095976287
|
||||
# lon: -123.0674908379146
|
||||
|
||||
# # node limits
|
||||
# # set to -1 to disable a limit
|
||||
# limit:
|
||||
# # defaults to 400 tracks in & out per CPU, up to 8000
|
||||
# num_tracks: -1
|
||||
# # defaults to 1 GB/s, or just under 10 Gbps
|
||||
# bytes_per_sec: 1_000_000_000
|
||||
|
||||
+8
-7
@@ -183,18 +183,18 @@ func installDeps() error {
|
||||
}
|
||||
|
||||
func installTools(force bool) error {
|
||||
tools := []string{
|
||||
"github.com/google/wire/cmd/wire",
|
||||
tools := map[string]string{
|
||||
"github.com/google/wire/cmd/wire": "latest",
|
||||
}
|
||||
for _, t := range tools {
|
||||
if err := installTool(t, force); err != nil {
|
||||
for t, v := range tools {
|
||||
if err := installTool(t, v, force); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func installTool(url string, force bool) error {
|
||||
func installTool(url, version string, force bool) error {
|
||||
name := filepath.Base(url)
|
||||
if !force {
|
||||
_, err := getToolPath(name)
|
||||
@@ -204,8 +204,9 @@ func installTool(url string, force bool) error {
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("installing %s\n", name)
|
||||
cmd := exec.Command("go", "get", "-u", url)
|
||||
fmt.Printf("installing %s %s\n", name, version)
|
||||
urlWithVersion := fmt.Sprintf("%s@%s", url, version)
|
||||
cmd := exec.Command("go", "install", urlWithVersion)
|
||||
connectStd(cmd)
|
||||
if err := cmd.Run(); err != nil {
|
||||
return err
|
||||
|
||||
@@ -3,6 +3,7 @@ package config
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/mitchellh/go-homedir"
|
||||
@@ -12,6 +13,12 @@ import (
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultLimitNumTracksPerCPU int32 = 400
|
||||
defaultLimitMaxNumTracks int32 = 8000
|
||||
defaultLimitBytesPerSec float32 = 1_000_000_000 // just under 10 Gbps
|
||||
)
|
||||
|
||||
var DefaultStunServers = []string{
|
||||
"stun.l.google.com:19302",
|
||||
"stun1.l.google.com:19302",
|
||||
@@ -31,6 +38,7 @@ type Config struct {
|
||||
Keys map[string]string `yaml:"keys"`
|
||||
Region string `yaml:"region"`
|
||||
LogLevel string `yaml:"log_level"`
|
||||
Limit LimitConfig `yaml:"limit"`
|
||||
|
||||
Development bool `yaml:"development"`
|
||||
}
|
||||
@@ -123,6 +131,11 @@ type RegionConfig struct {
|
||||
Lon float64 `yaml:"lon"`
|
||||
}
|
||||
|
||||
type LimitConfig struct {
|
||||
NumTracks int32 `yaml:"num_tracks"`
|
||||
BytesPerSec float32 `yaml:"bytes_per_sec"`
|
||||
}
|
||||
|
||||
func NewConfig(confString string, c *cli.Context) (*Config, error) {
|
||||
// start with defaults
|
||||
conf := &Config{
|
||||
@@ -205,6 +218,17 @@ func NewConfig(confString string, c *cli.Context) (*Config, error) {
|
||||
}
|
||||
}
|
||||
|
||||
if conf.Limit.NumTracks == 0 {
|
||||
conf.Limit.NumTracks = defaultLimitNumTracksPerCPU * int32(runtime.NumCPU())
|
||||
if conf.Limit.NumTracks > defaultLimitMaxNumTracks {
|
||||
conf.Limit.NumTracks = defaultLimitMaxNumTracks
|
||||
}
|
||||
}
|
||||
|
||||
if conf.Limit.BytesPerSec == 0 {
|
||||
conf.Limit.BytesPerSec = defaultLimitBytesPerSec
|
||||
}
|
||||
|
||||
return conf, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ var (
|
||||
ErrHandlerNotDefined = errors.New("handler not defined")
|
||||
ErrIncorrectRTCNode = errors.New("current node isn't the RTC node for the room")
|
||||
ErrNodeNotFound = errors.New("could not locate the node")
|
||||
ErrNodeLimitReached = errors.New("reached configured limit for node")
|
||||
ErrInvalidRouterMessage = errors.New("invalid router message")
|
||||
ErrChannelClosed = errors.New("channel closed")
|
||||
ErrChannelFull = errors.New("channel is full")
|
||||
|
||||
@@ -444,6 +444,7 @@ func (r *RedisRouter) handleRTCMessage(rm *livekit.RTCNodeMessage) error {
|
||||
if err := prometheus.UpdateCurrentNodeStats(r.currentNode.Stats); err != nil {
|
||||
logger.Errorw("could not update node stats", err)
|
||||
}
|
||||
// TODO: check stats against config.Limit values
|
||||
if err := r.RegisterNode(); err != nil {
|
||||
logger.Errorw("could not update node", err)
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package selector
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/livekit/livekit-server/pkg/config"
|
||||
livekit "github.com/livekit/protocol/proto"
|
||||
"github.com/thoas/go-funk"
|
||||
)
|
||||
@@ -20,3 +21,19 @@ func GetAvailableNodes(nodes []*livekit.Node) []*livekit.Node {
|
||||
return IsAvailable(node) && node.State == livekit.NodeState_SERVING
|
||||
}).([]*livekit.Node)
|
||||
}
|
||||
|
||||
// TODO: check remote node configured limit, instead of this node's config
|
||||
func LimitsReached(limitConfig config.LimitConfig, nodeStats *livekit.NodeStats) bool {
|
||||
if nodeStats == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if limitConfig.NumTracks > 0 && limitConfig.NumTracks <= nodeStats.NumTracksIn+nodeStats.NumTracksOut {
|
||||
return true
|
||||
}
|
||||
if limitConfig.BytesPerSec > 0 && limitConfig.BytesPerSec <= nodeStats.BytesInPerSec+nodeStats.BytesOutPerSec {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ var (
|
||||
ErrRoomClosed = errors.New("room has already closed")
|
||||
ErrPermissionDenied = errors.New("no permissions to access the room")
|
||||
ErrMaxParticipantsExceeded = errors.New("room has exceeded its max participants")
|
||||
ErrLimitExceeded = errors.New("node has exceeded its configured limit")
|
||||
ErrAlreadyJoined = errors.New("a participant with the same identity is already in the room")
|
||||
ErrUnexpectedOffer = errors.New("expected answer SDP, received offer")
|
||||
ErrDataChannelUnavailable = errors.New("data channel is not available")
|
||||
|
||||
@@ -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