semaphore

package
v1.13.4 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2022 License: MPL-2.0 Imports: 3 Imported by: 5

Documentation

Overview

Package semaphore implements a simple semaphore that is based on golang.org/x/sync/semaphore but doesn't support weights. It's advantage over a simple buffered chan is that the capacity of the semaphore (i.e. the number of slots available) can be changed dynamically at runtime without waiting for all existing work to stop. This makes it easier to implement e.g. concurrency limits on certain operations that can be reconfigured at runtime.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dynamic

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

Dynamic implements a semaphore whose capacity can be changed dynamically at run time.

func NewDynamic

func NewDynamic(n int64) *Dynamic

NewDynamic returns a dynamic semaphore with the given initial capacity. Note that this is for convenience and to match golang.org/x/sync/semaphore however it's possible to use a zero-value semaphore provided SetSize is called before use.

func (*Dynamic) Acquire

func (s *Dynamic) Acquire(ctx context.Context) error

Acquire attempts to acquire one "slot" in the semaphore, blocking only until ctx is Done. On success, returns nil. On failure, returns ctx.Err() and leaves the semaphore unchanged.

If ctx is already done, Acquire may still succeed without blocking.

func (*Dynamic) Release

func (s *Dynamic) Release()

Release releases the semaphore. It will panic if release is called on an empty semphore.

func (*Dynamic) SetSize

func (s *Dynamic) SetSize(n int64) error

SetSize dynamically updates the number of available slots. If there are more than n slots currently acquired, no further acquires will succeed until sufficient have been released to take the total outstanding below n again.

Jump to

Keyboard shortcuts

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