Skip to content

Codehardt/go-cpulimit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Throttle the CPU usage to a maximum

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages