limited

package
v0.0.0-...-a5b82e5 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package limited provides a groutine execution Pool that spins a goroutine per Submit() but is hard limited to the number of goroutines that can run at any time.

As Go has matured, goroutines have become more efficient. This type of pool starts very fast and is only slightly slower than our pooled version. For pools that you want to start up and tear down quickly, this might be the best choice.

See the examples in the parent package "goroutines" for an overview of using pools.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Caller

func Caller(name string) goroutines.SubmitOption

Caller sets the name of the calling function so that metrics can differentiate who is using the goroutines in the pool. With the introduction of generics, there is no way to get the name of function call reliably, as generic functions are written dynamically and runtime.FuncForPC does not work for generics. If this is not set, we will use runtime.FuncForPC().

func NonBlocking

func NonBlocking() goroutines.SubmitOption

NonBlocking indicates that if we are at our limit, we still run the goroutine and it is not counted against the total. This is useful when you want to track the statistics still but need this goroutine to run and don't want to do it naked.

Types

type Pool

type Pool struct {
	pool.Pool // Implements the pool.Preventer interface
	// contains filtered or unexported fields
}

Pool is a pool of goroutines.

func New

func New(name string, size int) (*Pool, error)

New creates a new Pool. "name" is the name of the pool which is used to get OTEL metrics and traces. These names must be globally unique, so it is best to set it to the package path + pool name that is using it. However, if not unique, a unique name will be created. If name is the empty string, the pool will not be registered, which is useful if creating and tearing down the pool instead of using it for the lifetime of the program. Names cannot contain spaces, hyphens, or numbers. "size" is the number of goroutines that can execute concurrently.

func (*Pool) Close

func (p *Pool) Close()

Close waits for all submitted jobs to stop, then stops all goroutines.

func (*Pool) GetName

func (p *Pool) GetName() string

GetName gets the name of the goroutines pool.

func (*Pool) Len

func (p *Pool) Len() int

Len returns the current size of the pool.

func (*Pool) Running

func (p *Pool) Running() int

Running returns the number of running jobs in the pool.

func (*Pool) Submit

func (p *Pool) Submit(ctx context.Context, runner goroutines.Job, options ...goroutines.SubmitOption) error

Submit submits the runner to be executed.

func (*Pool) Wait

func (p *Pool) Wait()

Wait will wait for all goroutines in the pool to finish. If you need to only wait on a subset of jobs, use a WaitGroup in your job.

Jump to

Keyboard shortcuts

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