Refactor CalculateProcessScoreboard and states

This commit is contained in:
Enrico Stahn 2018-02-27 19:32:52 +11:00
parent a25479edf3
commit 1066444ca1
No known key found for this signature in database
GPG key ID: 5263621C269A50DE

View file

@ -29,8 +29,8 @@ import (
// PoolProcessRequestIdle defines a process that is idle. // PoolProcessRequestIdle defines a process that is idle.
const PoolProcessRequestIdle string = "Idle" const PoolProcessRequestIdle string = "Idle"
// PoolProcessRequestIdle defines a process that is active. // PoolProcessRequestRunning defines a process that is running.
const PoolProcessRequestActive string = "Running" const PoolProcessRequestRunning string = "Running"
// PoolProcessRequestFinishing defines a process that is about to finish. // PoolProcessRequestFinishing defines a process that is about to finish.
const PoolProcessRequestFinishing string = "Finishing" const PoolProcessRequestFinishing string = "Finishing"
@ -41,7 +41,7 @@ const PoolProcessRequestReadingHeaders string = "Reading headers"
// PoolProcessRequestInfo defines a process that is getting request information. // PoolProcessRequestInfo defines a process that is getting request information.
const PoolProcessRequestInfo string = "Getting request informations" const PoolProcessRequestInfo string = "Getting request informations"
// PoolProcessRequestFinishing defines a process that is about to end. // PoolProcessRequestEnding defines a process that is about to end.
const PoolProcessRequestEnding string = "Ending" const PoolProcessRequestEnding string = "Ending"
var log logger var log logger
@ -100,16 +100,14 @@ type PoolProcess struct {
LastRequestMemory int `json:"last request memory"` LastRequestMemory int `json:"last request memory"`
} }
// PoolProcessScoreboard holds the calculated metrics for pool processes. // PoolProcessStateCounter holds the calculated metrics for pool processes.
type PoolProcessScoreboard struct { type PoolProcessStateCounter struct {
Active int64 Running int64
Idle int64 Idle int64
Finishing int64 Finishing int64
ReadingHeaders int64 ReadingHeaders int64
Info int64 Info int64
Ending int64 Ending int64
Unknown int64
Total int64
} }
// Add will add a pool to the pool manager based on the given URI. // Add will add a pool to the pool manager based on the given URI.
@ -194,13 +192,24 @@ func (p *Pool) error(err error) error {
return err return err
} }
func CalculateProcessScoreboard(p Pool) PoolProcessScoreboard { // Active calculates the number of active processes.
pps := PoolProcessScoreboard{} func (pps *PoolProcessStateCounter) Active() int64 {
return pps.Running + pps.ReadingHeaders
}
// Total calculates the total number of process (Idle + Active).
func (pps *PoolProcessStateCounter) Total() int64 {
return pps.Idle + pps.Active()
}
// CalculateProcessScoreboard creates a scoreboard with the calculated process metrics.
func CalculateProcessScoreboard(p Pool) PoolProcessStateCounter {
pps := PoolProcessStateCounter{}
for idx := range p.Processes { for idx := range p.Processes {
switch p.Processes[idx].State { switch p.Processes[idx].State {
case PoolProcessRequestActive: case PoolProcessRequestRunning:
pps.Active++ pps.Running++
case PoolProcessRequestIdle: case PoolProcessRequestIdle:
pps.Idle++ pps.Idle++
case PoolProcessRequestEnding: case PoolProcessRequestEnding:
@ -212,13 +221,10 @@ func CalculateProcessScoreboard(p Pool) PoolProcessScoreboard {
case PoolProcessRequestReadingHeaders: case PoolProcessRequestReadingHeaders:
pps.ReadingHeaders++ pps.ReadingHeaders++
default: default:
pps.Unknown++
log.Errorf("Unknown process state '%v'", p.Processes[idx].State) log.Errorf("Unknown process state '%v'", p.Processes[idx].State)
} }
} }
pps.Total = pps.Active + pps.Idle + pps.Ending + pps.Finishing + pps.Info + pps.ReadingHeaders + pps.Unknown
return pps return pps
} }