mirror of
https://github.com/livekit/livekit.git
synced 2026-05-11 23:25:05 +00:00
cac6d22a72
* store cpu load in node stats * num cpus uint32 * cpu load selector test * dep update
43 lines
967 B
Go
43 lines
967 B
Go
package selector
|
|
|
|
import (
|
|
"github.com/thoas/go-funk"
|
|
|
|
"github.com/livekit/protocol/livekit"
|
|
)
|
|
|
|
// CPULoadSelector eliminates nodes that have CPU usage higher than CPULoadLimit
|
|
// then selects a node randomly from nodes that are not overloaded
|
|
type CPULoadSelector struct {
|
|
CPULoadLimit float32
|
|
}
|
|
|
|
func (s *CPULoadSelector) filterNodes(nodes []*livekit.Node) ([]*livekit.Node, error) {
|
|
nodes = GetAvailableNodes(nodes)
|
|
if len(nodes) == 0 {
|
|
return nil, ErrNoAvailableNodes
|
|
}
|
|
|
|
nodesLowLoad := make([]*livekit.Node, 0)
|
|
for _, node := range nodes {
|
|
stats := node.Stats
|
|
if stats.CpuLoad < s.CPULoadLimit {
|
|
nodesLowLoad = append(nodesLowLoad, node)
|
|
}
|
|
}
|
|
if len(nodesLowLoad) > 0 {
|
|
nodes = nodesLowLoad
|
|
}
|
|
return nodes, nil
|
|
}
|
|
|
|
func (s *CPULoadSelector) SelectNode(nodes []*livekit.Node) (*livekit.Node, error) {
|
|
nodes, err := s.filterNodes(nodes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
idx := funk.RandomInt(0, len(nodes))
|
|
return nodes[idx], nil
|
|
}
|