Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Semaphore ¶
type Semaphore struct {
// contains filtered or unexported fields
}
Semaphore provides functionality to limit and control concurrent access to a resource. The concurrency limit is controlled by the limited provided when a Semaphore is created using the New function.
Setting the limit to one will result in the Semaphore behaving like a mutex, but if you need the behavior of a mutex you should use sync.Mutex instead.
The zero-value is not usable and a Semaphore should be created using the New function.
func New ¶
New creates a new Semaphore with the given concurrency limit.
If a limit value of < 1 is provided this function will panic.
func (*Semaphore) Acquire ¶
Acquire acquires the semaphore blocking until the semaphore/resource is available or the context is done. On success a nil error value is returned. If the context is done before the semaphore is acquired ctx.Err() is returned and the semaphore is left unchanged.
If ctx is already done Acquire might still succeed without blocking.
func (*Semaphore) Release ¶
func (s *Semaphore) Release()
Release releases a single acquirement of the Semaphore.
Note: Improper use of this API can lead to a deadlock! Release should always be paired with either Acquire or TryAcquire, and only if they successfully acquired the Semaphore.
func (*Semaphore) TryAcquire ¶
TryAcquire acquires the semaphore without blocking. On success returns true. On failure returns false and leaves the semaphore unchanged.