gorc

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2019 License: MIT Imports: 2 Imported by: 0

README

gorc

set thresholds on number of running goroutines

Can increase and decrease a counter when starting or stopping a goroutine. It can wait for a minimum or maximum number of goroutines running, thus allowing to set thresholds for the number of gorc governed goroutines running at the same time.

import "github.com/GetTerminus/gorc"

an example:

var gorc0 gorc.Gorc

func main() {
	gorc0 = gorc.NewGorc(100)
	for i := 0; i < 20; i++ {
		gorc0.Inc()
		go withgorc(i)

		gorc0.WaitLow(5) // no more then five goroutines governed by gorc0 are allowed at the same time
	}
}

func withgorc(i int) {
	defer gorc0.Dec() // decrease counter when finished

	fmt.Println("Nr.", i, " ", gorc0.GetCount(), "gorc goroutines running..")
	time.Sleep(time.Duration(rand.Int31n(2000)) * time.Millisecond)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Gorc

type Gorc interface {
	// IncBy increases the counter by b.
	IncBy(b int)
	// Inc increases the counter by one.
	Inc()

	// DecBy decreases the counter by b.
	DecBy(b int)
	// Dec decreases the counter by one.
	Dec()

	// GetCount returns an integer holding the count.
	GetCount() int

	// WaitLow will return as soon as the Gorc counter falls below w.
	// e.g. wait until all but w goroutines are stopped.
	WaitLow(w int)

	// WaitHigh will return as soon as the Gorc counter goes above w.
	// e.g. wait until at least w-1 goroutines are started.
	WaitHigh(w int)
}

Gorc is used to keep track of goroutines running.

func NewGorc

func NewGorc(waitDwellMilliseconds int) Gorc

NewGorc makes you a new Gorc

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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