With go-cpulimit you can limit the CPU usage of your go program. It provides a function called Wait()
that holds your program, until the CPU usage is below max CPU usage.
You should use this Wait()
function in as many situations as possible, e.g. on every iteration in a for loop
.
Warning: This limiter does not work, if there are insufficient Wait()
calls.
This package requires github.com/shirou/gopsutil (Copyright (c) 2014, WAKAYAMA Shirou) to be installed.
limiter := &cpulimit.Limiter{
MaxCPUUsage: 50.0, // throttle CPU usage to 50%
MeasureInterval: time.Millisecond * 333, // measure cpu usage in an interval of 333 ms
Measurements: 3, // use the avg of the last 3 measurements
}
limiter.Start()
defer limiter.Stop()
for {
limiter.Wait() // wait until cpu usage is below 50%
/*
* do some work
*/
}