Documentation ¶
Overview ¶
Package semaphore provides an implementation of counting semaphore primitive with possibility to change limit after creation. This implementation is based on Compare-and-Swap primitive that in general case works faster than other golang channel-based semaphore implementations.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Semaphore ¶
type Semaphore interface { // Acquire enters the semaphore a specified number of times, blocking only until ctx is done. // This operation can be cancelled via passed context (but it's allowed to pass ctx='nil'). // Method returns context error (ctx.Err()) if the passed context is cancelled, // but this behavior is not guaranteed and sometimes semaphore will still be acquired. Acquire(ctx context.Context, n int) error // TryAcquire acquires the semaphore without blocking. // On success, returns true. On failure, returns false and leaves the semaphore unchanged. TryAcquire(n int) bool // Release exits the semaphore a specified number of times and returns the previous count. Release(n int) int // SetLimit changes current semaphore limit in concurrent way. // It is allowed to change limit many times and it's safe to set limit higher or lower. SetLimit(limit int) // GetLimit returns current semaphore limit. GetLimit() int // GetCount returns current number of occupied entries in semaphore. GetCount() int }
Semaphore counting resizable semaphore synchronization primitive. Use the Semaphore to control access to a pool of resources. There is no guaranteed order, such as FIFO or LIFO, in which blocked goroutines enter the semaphore. A goroutine can enter the semaphore multiple times, by calling the Acquire or TryAcquire methods repeatedly. To release some or all of these entries, the goroutine can call the Release method that specifies the number of entries to be released. Change Semaphore capacity to lower or higher by SetLimit.