Documentation
ΒΆ
Overview ΒΆ
Package concurrency provides concurrency utilities.
Index ΒΆ
Constants ΒΆ
This section is empty.
Variables ΒΆ
This section is empty.
Functions ΒΆ
func WithLock ΒΆ
WithLock executes the given action while holding the provided lock.
It accepts any sync.Locker (e.g., *sync.Mutex, *sync.RWMutex) and a function with no parameters or return values. If the action is nil, nothing is executed.
The lock is guaranteed to be released after the action completes, even if the action panics or returns early.
Types ΒΆ
type Future ΒΆ added in v1.0.1
type Future[T any] struct { // contains filtered or unexported fields }
Future represents a read-only view of a promised value. It provides a way to retrieve the value asynchronously, blocking if necessary.
type FutureAction ΒΆ added in v1.0.1
type FutureAction[T any] struct { // contains filtered or unexported fields }
FutureAction is an abstraction over a channel that models a task and its result. It allows executing a computation asynchronously in a goroutine and retrieving the result later via a blocking call. This is similar to the Future pattern in other languages, providing a simple way to handle asynchronous results without manual channel management.
The channel is closed after the result is sent, ensuring proper resource cleanup.
Example usage:
func main() {
callback := func() any {
time.Sleep(time.Second)
return "success"
}
future := NewFutureAction(callback)
result := future.Get()
fmt.Println(result) // Output: success
}
func NewFutureAction ΒΆ added in v1.0.1
func NewFutureAction[T any](action func() T) *FutureAction[T]
NewFutureAction creates and returns a new FutureAction. It starts the provided action function in a separate goroutine. The action's return value is sent to the internal channel. The channel is closed after sending the result to allow safe ranging or detection of completion.
func (*FutureAction[T]) Get ΒΆ added in v1.0.1
func (f *FutureAction[T]) Get() T
Get returns the result of the asynchronous task. This method blocks until the result is available from the channel. If the action function blocks indefinitely (e.g., due to an infinite loop or deadlock), Get will never return, potentially causing the caller to hang. It is the caller's responsibility to ensure the action completes.
type FutureError ΒΆ added in v1.0.1
PromiseError and FutureError are type aliases for Promise and Future specialized for error handling. This allows for easy propagation of errors in asynchronous operations.
type Promise ΒΆ added in v1.0.1
type Promise[T any] struct { // contains filtered or unexported fields }
Promise represents a writable, single-assignment container for a future value. It allows setting a value exactly once. Attempting to set the value more than once is ignored. The internal channel is buffered to hold one value and is closed after setting. Synchronization is handled via atomic operations and a mutex for thread safety.
Example usage:
func main() {
promise := NewPromise[string]()
go func() {
time.Sleep(time.Second)
promise.Set("Cake")
}()
future := promise.GetFuture()
value := future.Get()
fmt.Println(value) // Output: Cake
}
func NewPromise ΒΆ added in v1.0.1
NewPromise creates and returns a new Promise. The internal channel is buffered with capacity 1 to hold the future value.
type PromiseError ΒΆ added in v1.0.1
PromiseError and FutureError are type aliases for Promise and Future specialized for error handling. This allows for easy propagation of errors in asynchronous operations.
type Semaphore ΒΆ
type Semaphore struct {
// contains filtered or unexported fields
}
Semaphore is a counting semaphore that bounds the number of concurrent holders.
The zero value (and a nil *Semaphore) is an unlimited semaphore: all acquire operations succeed immediately and Release is a no-op.
All methods are safe for concurrent use by multiple goroutines.
func NewSemaphore ΒΆ
NewSemaphore returns a semaphore with the provided capacity.
If capacity <= 0, it returns an unlimited semaphore, for which all acquire operations succeed immediately and Release does nothing.
func (*Semaphore) Acquire ΒΆ
func (s *Semaphore) Acquire()
Acquire obtains one slot from s, blocking until a slot is available. For an unlimited semaphore, Acquire is a no-op.
func (*Semaphore) AcquireContext ΒΆ
AcquireContext attempts to obtain one slot, blocking until a slot is available or the context is canceled or its deadline is exceeded. It returns ctx.Err() if the context is done first. For an unlimited semaphore, AcquireContext returns nil immediately.
func (*Semaphore) Cap ΒΆ
Cap returns the maximum number of concurrent holders (the capacity). For an unlimited semaphore, Cap returns 0.
func (*Semaphore) InUse ΒΆ
InUse reports the current number of acquired slots. For an unlimited semaphore, InUse returns 0.
func (*Semaphore) Release ΒΆ
func (s *Semaphore) Release()
Release releases one previously acquired slot. On a limited semaphore, calling Release without a matching acquire panics. On an unlimited semaphore, Release is a no-op.
func (*Semaphore) TryAcquire ΒΆ
TryAcquire attempts to obtain one slot without blocking. It returns true if a slot was acquired and false otherwise. For an unlimited semaphore, TryAcquire always returns true.