mirror of https://github.com/gofiber/fiber.git
fix: monitor middleware reporting of CPU usage (#2984)
monitPIDCPU should be transient, not persistent. Co-authored-by: Juan Calderon-Perez <835733+gaby@users.noreply.github.com>pull/3043/head
parent
232c0fac0d
commit
4262f5b591
|
@ -2,6 +2,7 @@ package monitor
|
|||
|
||||
import (
|
||||
"os"
|
||||
"runtime"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
@ -59,14 +60,14 @@ func New(config ...Config) fiber.Handler {
|
|||
// Start routine to update statistics
|
||||
once.Do(func() {
|
||||
p, _ := process.NewProcess(int32(os.Getpid())) //nolint:errcheck // TODO: Handle error
|
||||
|
||||
updateStatistics(p)
|
||||
numcpu := runtime.NumCPU()
|
||||
updateStatistics(p, numcpu)
|
||||
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(cfg.Refresh)
|
||||
|
||||
updateStatistics(p)
|
||||
updateStatistics(p, numcpu)
|
||||
}
|
||||
}()
|
||||
})
|
||||
|
@ -101,10 +102,10 @@ func New(config ...Config) fiber.Handler {
|
|||
}
|
||||
}
|
||||
|
||||
func updateStatistics(p *process.Process) {
|
||||
pidCPU, err := p.CPUPercent()
|
||||
func updateStatistics(p *process.Process, numcpu int) {
|
||||
pidCPU, err := p.Percent(0)
|
||||
if err == nil {
|
||||
monitPIDCPU.Store(pidCPU / 10)
|
||||
monitPIDCPU.Store(pidCPU / float64(numcpu))
|
||||
}
|
||||
|
||||
if osCPU, err := cpu.Percent(0, false); err == nil && len(osCPU) > 0 {
|
||||
|
|
Loading…
Reference in New Issue