ErrMaxWaiters is returned when Acquire is called, but there are more than maxWaiters waiters.
var ErrMaxWaiters = errors.New("too many waiters")
Weighted provides a way to bound concurrent access to a resource. The callers can request access with a given weight.
type Weighted struct {
// contains filtered or unexported fields
}
func NewWeighted(n int64, maxWaiters int) *Weighted
NewWeighted creates a new weighted semaphore with the given maximum combined weight for concurrent access. maxWaiters provides a limit such that calls to Acquire will immediately error if the number of waiters is that high. A maxWaiters of zero means no limit.
func (s *Weighted) Acquire(ctx context.Context, n int64) error
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.
If ctx is already done, Acquire may still succeed without blocking.
If there are maxWaiters waiters, Acquire will return an error immediately.
func (s *Weighted) NumWaiters() int
func (s *Weighted) Release(n int64)
Release releases the semaphore with a weight of n.
func (s *Weighted) TryAcquire(n int64) bool
TryAcquire acquires the semaphore with a weight of n without blocking. On success, returns true. On failure, returns false and leaves the semaphore unchanged.