gcpulimit

package
v0.0.0-...-047dcd9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 25, 2021 License: MIT, MIT Imports: 3 Imported by: 0

README

go-cpulimit

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

Documentation

Index

Constants

View Source
const (
	// DefaultLimit is 80%
	DefaultLimit float64 = 80.0
	// DefaultInterval is 333 ms
	DefaultInterval = time.Millisecond * 333
	// DefaultMeasurements is 3
	DefaultMeasurements int = 3
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Limiter

type Limiter struct {
	MaxCPUUsage     float64
	MeasureInterval time.Duration
	Measurements    int
	// contains filtered or unexported fields
}

Limiter limits the CPU usage

func (*Limiter) Start

func (l *Limiter) Start()

Start starts the CPU limiter. If there are undefined variables Start() will set them to the default values.

func (*Limiter) Stop

func (l *Limiter) Stop()

Stop stops the limiter. After this stop, Wait() won't block anymore.

func (*Limiter) Wait

func (l *Limiter) Wait()

Wait waits until the CPU usage is below MaxCPUUsage

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL