prox5/scale_util.go

65 lines
1.5 KiB
Go
Raw Normal View History

package prox5
import (
"strconv"
2022-12-27 15:25:10 +00:00
"sync/atomic"
"time"
)
func (p5 *ProxyEngine) scaleDbg() {
if !p5.DebugEnabled() {
return
}
msg := strs.Get()
msg.MustWriteString("job spawner auto scaling, new count: ")
msg.MustWriteString(strconv.Itoa(p5.pool.Cap()))
p5.dbgPrint(msg)
}
2022-12-27 15:25:10 +00:00
func (p5 *ProxyEngine) scale() (sleep bool) {
select {
2022-12-27 15:25:10 +00:00
case <-p5.scaleTimer.C:
bad := int64(0)
totalBadNow := p5.GetTotalBad()
accountedFor := p5.stats.badAccounted.Load()
2023-01-31 09:21:29 +00:00
netFactors := totalBadNow - accountedFor
if time.Since(*p5.stats.accountingLastDone.Load()) > 5*time.Second && netFactors > 0 {
bad = netFactors
2022-12-27 15:25:10 +00:00
if p5.DebugEnabled() {
2023-01-31 09:21:29 +00:00
p5.DebugLogger.Printf("accounting: %d bad - %d accounted for = %d net factors",
totalBadNow, accountedFor, netFactors)
2022-12-27 15:25:10 +00:00
}
tnow := time.Now()
p5.stats.accountingLastDone.Store(&tnow)
2022-12-27 15:25:10 +00:00
}
// this shouldn't happen..?
if bad < 0 {
panic("scale_util.go: bad < 0")
}
if p5.pool.IsClosed() {
return
}
2022-12-27 15:25:10 +00:00
totalValidated := p5.GetTotalValidated()
totalConsidered := p5.GetStatistics().Dispensed.Load() + bad
2022-12-27 15:25:10 +00:00
// if we are considering more than we have validated, cap it at validated so that it registers properly.
// additionally, signal the dialer to slow down a little.
2023-01-01 00:40:32 +00:00
if totalConsidered >= totalValidated {
2022-12-27 15:25:10 +00:00
sleep = true
2023-01-01 00:40:32 +00:00
totalConsidered = totalValidated - atomic.LoadInt64(p5.scaler.Threshold)/2
2022-12-27 15:25:10 +00:00
}
if p5.scaler.ScaleAnts(
p5.pool,
totalValidated,
totalConsidered,
) {
p5.scaleDbg()
}
default:
return
}
2022-12-27 15:25:10 +00:00
return
}