44 lines
880 B
Go
44 lines
880 B
Go
package gavalink
|
|
|
|
import (
|
|
"math"
|
|
"sort"
|
|
)
|
|
|
|
type balancePenalty struct {
|
|
node *Node
|
|
penalty int
|
|
}
|
|
|
|
func BestNodeByPenalties(nodes []*Node) (*Node, error) {
|
|
penalties := make([]*balancePenalty, len(nodes))
|
|
|
|
var playerPenalty, cpuPenalty int
|
|
|
|
for i, node := range nodes {
|
|
if node.stats == nil {
|
|
penalties[i] = &balancePenalty{node, 1}
|
|
continue
|
|
}
|
|
|
|
playerPenalty = node.stats.ActivePlayers
|
|
cpuPenalty = int(math.Pow(1.05, 100*node.stats.Cpu.SystemLoad)*10 - 10)
|
|
|
|
penalties[i] = &balancePenalty{node, playerPenalty + cpuPenalty}
|
|
}
|
|
|
|
sort.SliceStable(penalties, func(i, j int) bool {
|
|
return penalties[i].penalty < penalties[j].penalty
|
|
})
|
|
|
|
return penalties[0].node, nil
|
|
}
|
|
|
|
func BestNodeByLoad(n []*Node) (*Node, error) {
|
|
sort.SliceStable(n, func(i, j int) bool {
|
|
return n[i].stats.Cpu.LavalinkLoad < n[j].stats.Cpu.LavalinkLoad
|
|
})
|
|
|
|
return n[0], nil
|
|
}
|