Skip to content

Latest commit

 

History

History
31 lines (24 loc) · 1.34 KB

README.md

File metadata and controls

31 lines (24 loc) · 1.34 KB

go-cpulimit

License: MIT Go Report Card GoDoc

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.

Example

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
     */
}