Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

monitPIDCPU should be transient, not persistent. #2984

Open
wants to merge 1 commit into
base: v2
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 7 additions & 6 deletions middleware/monitor/monitor.go
Expand Up @@ -2,6 +2,7 @@ package monitor

import (
"os"
"runtime"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -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)
}
}()
})
Expand Down Expand Up @@ -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 {
Expand Down