From 4ae4d6def2f4daee345d6053fb51e58a0daa7e8f Mon Sep 17 00:00:00 2001 From: Tyler Date: Sun, 13 Oct 2019 04:44:51 -0400 Subject: [PATCH] Properly implement penalties for frame stats --- balancer.go | 24 ++++++++++++++++-------- node.go | 7 +++++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/balancer.go b/balancer.go index 7a1cc54..473deef 100644 --- a/balancer.go +++ b/balancer.go @@ -13,18 +13,26 @@ type balancePenalty struct { func BestNodeByPenalties(nodes []*Node) (*Node, error) { penalties := make([]*balancePenalty, len(nodes)) - var playerPenalty, cpuPenalty int + var playerPenalty, cpuPenalty, deficitFramePenalty, nullFramePenalty int for i, node := range nodes { - if node.stats == nil { - penalties[i] = &balancePenalty{node, 1} - continue + playerPenalty = 0 + cpuPenalty = 0 + deficitFramePenalty = 0 + nullFramePenalty = 0 + + if node.stats != nil { + playerPenalty = node.stats.ActivePlayers + cpuPenalty = int(math.Pow(1.05, 100*node.stats.Cpu.SystemLoad)*10 - 10) + + if node.stats.Frames != nil && node.stats.Frames.Deficit != -1 { + deficitFramePenalty = int(math.Pow(1.03, 500*float64(node.stats.Frames.Deficit/3000))*600 - 600) + nullFramePenalty = int(math.Pow(1.03, 500*float64(node.stats.Frames.Nulled/3000))*300 - 300) + nullFramePenalty *= 2 + } } - playerPenalty = node.stats.ActivePlayers - cpuPenalty = int(math.Pow(1.05, 100*node.stats.Cpu.SystemLoad)*10 - 10) - - penalties[i] = &balancePenalty{node, playerPenalty + cpuPenalty} + penalties[i] = &balancePenalty{node, playerPenalty + cpuPenalty + deficitFramePenalty + nullFramePenalty} } sort.SliceStable(penalties, func(i, j int) bool { diff --git a/node.go b/node.go index 9a1ea1e..f296f72 100644 --- a/node.go +++ b/node.go @@ -42,6 +42,7 @@ type RemoteStats struct { Uptime int64 `json:"uptime"` Memory *MemoryStats `json:"memory"` Cpu *CpuStats `json:"cpu"` + Frames *FrameStats `json:"frameStats"` } type MemoryStats struct { @@ -57,6 +58,12 @@ type CpuStats struct { LavalinkLoad float64 `json:"lavalinkLoad"` } +type FrameStats struct { + Sent int `json:"sent"` + Nulled int `json:"nulled"` + Deficit int `json:"deficit"` +} + func (node *Node) open() error { header := http.Header{}