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 implements a counting semaphore: When multiple calls to its methods are made concurrently, only a maximum number of them may execute at the same time, and additional calls are made to wait until earlier ones finish.
This is most commonly used with expensive operations (either in terms of CPU or RAM) in order to avoid consuming all available resources or running into an OOM (Out Of Memory) error.
The implementation is based on <https://eli.thegreenplace.net/2019/on-concurrency-in-go-http-servers/>.
func NewSemaphore ¶
NewSemaphore creates a new semaphore that allows up to the given number of concurrent operations.
func (*Semaphore) Run ¶
func (s *Semaphore) Run(action func())
Run executes the given function, ensuring that no more than the maximum number of ops for this semaphore execute concurrently.
func (*Semaphore) RunFallible ¶
RunFallible is like Run, but allows the callback to return an error.