semaphore

Imports

Imports #

"container/list"
"context"
"sync"

Structs

Weighted struct #

Weighted provides a way to bound concurrent access to a resource. The callers can request access with a given weight.

type Weighted struct {
size int64
cur int64
mu sync.Mutex
waiters list.List
}

waiter struct #

type waiter struct {
n int64
ready chan<- struct{...}
}

Functions

Acquire method #

Acquire acquires the semaphore with a weight of n, blocking until resources are available or ctx is done. On success, returns nil. On failure, returns ctx.Err() and leaves the semaphore unchanged.

func (s *Weighted) Acquire(ctx context.Context, n int64) error

NewWeighted function #

NewWeighted creates a new weighted semaphore with the given maximum combined weight for concurrent access.

func NewWeighted(n int64) *Weighted

Release method #

Release releases the semaphore with a weight of n.

func (s *Weighted) Release(n int64)

TryAcquire method #

TryAcquire acquires the semaphore with a weight of n without blocking. On success, returns true. On failure, returns false and leaves the semaphore unchanged.

func (s *Weighted) TryAcquire(n int64) bool

notifyWaiters method #

func (s *Weighted) notifyWaiters()

Generated with Arrow