workpool

package
v11.3.3 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2022 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package workpool provies the `Pool` type which functions as a means of managing the number of concurrent workers, grouped by key. You can think of this type as functioning like a collection of semaphores, except that multiple distinct resources may exist (distinguished by keys), and the target concurrent worker count may change at runtime. The basic usage pattern is as follows:

1. The desired number of workers for a given key is specified or updated via Pool.Set.

2. Workers are spawned as leases become available on Pool.Acquire.

3. Workers relenquish their leases when they finish their work by calling Lease.Release.

4. New leases become available as old leases are relenquished, or as the target concurrent lease count increases.

This is a generalization of logic originally written to manage the number of concurrent reversetunnel agents per proxy endpoint.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Counts

type Counts struct {
	// Target is the number of active leases that we would
	// like to converge toward.
	Target uint64
	// Active is the current active lease count.
	Active uint64
}

Counts holds the target and active counts for a key/group.

type Lease

type Lease struct {
	// contains filtered or unexported fields
}

Lease grants access to a resource or group. When the lease is received, work can begin. Leases are held by workers and must be released when the worker has finished its work.

func (Lease) ID

func (l Lease) ID() uint64

ID returns the unique ID of this lease.

func (Lease) IsZero

func (l Lease) IsZero() bool

IsZero checks if this is the zero value of Lease.

func (Lease) Release

func (l Lease) Release()

Release relinquishes this lease. Each lease is unique, so double-calling Release() on the same Lease has no effect.

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

Pool manages a collection of work group by key and is the primary means by which group are managed. Each work group has an adjustable target value which is the number of target leases which should be active for the given group.

func NewPool

func NewPool(ctx context.Context) *Pool

func (*Pool) Acquire

func (p *Pool) Acquire() <-chan Lease

Acquire is the channel which must be received on to acquire new leases. Each lease acquired in this way *must* have its Release method called when the lease is no longer needed. Note this channel will deliver leases from all active work group. It's up to the receiver to differentiate what group the lease refers to and act accordingly.

func (*Pool) Done

func (p *Pool) Done() <-chan struct{}

Done signals pool closure.

func (*Pool) Get

func (p *Pool) Get() Counts

Get gets the current counts for the specified key.

func (*Pool) Set

func (p *Pool) Set(target uint64)

Set sets the target for the specified key.

func (*Pool) Stop

func (p *Pool) Stop()

Stop permanently halts all associated group.

Jump to

Keyboard shortcuts

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