semaphore

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2024 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

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

func (*Item) Buf

func (i *Item) Buf() []byte

Buf borrows this item's buffer. Caller must not use it after releasing the item.

func (*Item) Release

func (i *Item) Release()

Release returns capacity to the semaphore. It must be called exactly once after acquisition.

type S

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

S is a concurrency limiter. Holders of a resource unit (represented by *Item) also get temporary ownership of a memory buffer. TODO: Consider implementing some ordering or prioritization. For example, while copying many files in multiple chunks each, maybe we prefer to use available concurrency to complete all the chunks of one file rather than a couple of chunks of many, so that if there's an error we have made some useful progress.

func New

func New(capacity int, bufSize int) S

New constructs S. Total possible buffer allocation is capacity*bufSize, but it's allocated lazily per-Item.

func (S) Acquire

func (s S) Acquire(ctx context.Context) (*Item, error)

Acquire waits for semaphore capacity, respecting context cancellation. Returns exactly one of item or error. If non-nil, Item must be released.

Intended usage:

item, err := sema.Acquire(ctx)
if err != nil {
	return /* ..., */ err
)
defer item.Release()
doMyThing(item.Buf())

func (S) Cap

func (s S) Cap() int

Cap returns the capacity (that was passed to New).

Jump to

Keyboard shortcuts

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