Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AtomicTime ¶
type AtomicTime struct {
// contains filtered or unexported fields
}
func AtomicTimeNow ¶
func AtomicTimeNow() *AtomicTime
func (*AtomicTime) Load ¶
func (at *AtomicTime) Load() time.Time
func (*AtomicTime) Store ¶
func (at *AtomicTime) Store(t time.Time)
func (*AtomicTime) TryAdvancingTo ¶
func (at *AtomicTime) TryAdvancingTo(t time.Time)
type AutoResetEvent ¶
type AutoResetEvent struct {
// contains filtered or unexported fields
}
The AutoResetEvent is a synchronization primitive that is automatically reset when successfully consumed. It supports two use cases:
Multiple-use, multiple-setters, single-consumer. In this case the event is Set() by one or more goroutines and Wait() is called by a single goroutine. The channel returned by Wait() blocks until Set() is called. When the channel read operation returns, the event is "cleared" (reset). The call to Set() may also precede the Wait()/channel read operation. If the event is already set, the channel read operation will return immediately.
The code that uses the event must ensure that for every Set() call there is AT MOST one attempt to wait on the event channel. The following sequence illustrates the problem:
goroutine 1: Set() goroutine 1: Set() // The event is already set, so this is a no-op goroutine 2: ch := Wait(); <-ch // Channel read immediately because the event was set. The event is cleared now. goroutine 2: <-ch // Expecting that since Set() was called twice, this will not block. // In fact, this will block until Set() is called again.
A rule of thumb is that if there is a single event consumer that only cares about THE FINAL STATE protected by the event (and not about intermediate states), then this is the right use case for AutoResetEvent.
Single-use, multiple-setters, multiple-consumer. In this case the event is by calling SetAndFreeze() by one goroutine and Wait() is called by one or more goroutines (consumers). These calls can happen in any order, and it is OK to call SetAndFreeze() multiple times, from multiple goroutines. Once event is frozen, it remains frozen forever, and the read operation on the channel returned by Wait() will always immediately return.
func NewAutoResetEvent ¶
func NewAutoResetEvent(initialState bool) *AutoResetEvent
func (*AutoResetEvent) Clear ¶
func (e *AutoResetEvent) Clear()
func (*AutoResetEvent) Frozen ¶
func (e *AutoResetEvent) Frozen() bool
func (*AutoResetEvent) Set ¶
func (e *AutoResetEvent) Set()
func (*AutoResetEvent) SetAndFreeze ¶
func (e *AutoResetEvent) SetAndFreeze()
func (*AutoResetEvent) Wait ¶
func (e *AutoResetEvent) Wait() <-chan struct{}
type ContextAwareLock ¶
type ContextAwareLock struct {
// contains filtered or unexported fields
}
ContextAwareLock is a type of lock that can be locked only if the context passed to the Lock() operation is not done. It also exposes a TryLock() operation that allows the caller to attempt to acquire the lock without blocking.
func NewContextAwareLock ¶
func NewContextAwareLock() *ContextAwareLock
func (*ContextAwareLock) TryLock ¶
func (sc *ContextAwareLock) TryLock() bool
func (*ContextAwareLock) Unlock ¶
func (sc *ContextAwareLock) Unlock()
type OneTimeJob ¶
type OneTimeJob[T any] struct { // contains filtered or unexported fields }
OneTimeJob represents an activity that must be done only once. Multiple goroutines can try to get the job, but only one of them will succeed. Those that fail will wait until the job is done.
func NewOneTimeJob ¶
func NewOneTimeJob[T any]() *OneTimeJob[T]
func (*OneTimeJob[T]) Complete ¶
func (otj *OneTimeJob[T]) Complete(res T)
Sets the job result and marks the job as done.
func (*OneTimeJob[T]) Done ¶
func (otj *OneTimeJob[T]) Done() <-chan struct{}
Returns the channel that will be closed when the job is done.
func (*OneTimeJob[T]) IsDone ¶
func (otj *OneTimeJob[T]) IsDone() bool
Returns true if the job is done, otherwise false.
func (*OneTimeJob[T]) TryTake ¶
func (otj *OneTimeJob[T]) TryTake() bool
Atomically tries to take the job. Returns true if the caller "got" the job and is supposed to perform it, otherwise false (the job is already taken).
func (*OneTimeJob[T]) WaitResult ¶
func (otj *OneTimeJob[T]) WaitResult() T
Waits for the job to be done and returns the result.
type Semaphore ¶
type Semaphore struct {
// contains filtered or unexported fields
}
Semaphore implements a counting semaphore synchronization primitive. The waiting goroutines are woken up in FIFO order.
func NewSemaphore ¶
func NewSemaphore() *Semaphore
func NewSemaphoreWithCount ¶
type UnboundedChan ¶
type UnboundedChan[T any] struct { In chan<- T // channel for writing data Out <-chan T // channel for reading data // contains filtered or unexported fields }
UnboundedChan implements an unbounded channel that exhibits at most a short block time when writing (data will be buffered for unlimited time if it is not read immediately). The implementation uses two channels (In and Out) and a buffer, with a dedicated goroutine that moves data between the In channel and the buffer, and between the buffer and the Out channel. The goroutine exits when the context associated with the UnboundedChan is canceled.
Multiple writers and readers are supported (UnboundedChan is goroutine-safe).
The input channel (In channel) can be closed by UnboundedChan users to signal that no more data will be written. If the channel context is not cancelled, the goroutine will continue to write buffered data to the Out channel until the buffer is empty. To guarantee that all data written to the input channel makes it to the output channel, make sure to use a non-buffered input channel.
Inspired by https://github.com/smallnest/chanx
func NewUnboundedChan ¶
func NewUnboundedChan[T any](ctx context.Context) *UnboundedChan[T]
NewUnboundedChan creates a new instance of unbounded channel with unbuffered In and Out channels.
func NewUnboundedChanBuffered ¶
func NewUnboundedChanBuffered[T any](ctx context.Context, inSize, outSize int) *UnboundedChan[T]
NewUnboundedChanBuffered creates a new instance of unbounded channel with specified buffer sizes for In and Out channels. If either size is zero, the corresponding channel will be unbuffered.
func (*UnboundedChan[T]) BufLen ¶
func (ch *UnboundedChan[T]) BufLen() int64
Returns the number of elements in the buffer. This method is safe for concurrent use, but it cannot be relied upon to provide an exact count.
type Waiter ¶
type Waiter struct {
Chan chan struct{}
// contains filtered or unexported fields
}
Waiter is a type that exposes a channel for waiting on a Semaphore, and a method to "cancel" the wait. If the Waiter is cancelled after the wait completed (i.e. the waiter channel is already closed), Cancel() is a no-op. Otherwise, the Cancel() method closes the channel and removes the Waiter from the Semaphore's waiters list.