Documentation
¶
Index ¶
- func Buffer[A any](in <-chan A, size int) <-chan A
- func Drain[A any](in <-chan A)
- func FilterMap[A, B any](in <-chan A, n int, f func(A) (B, bool)) <-chan B
- func Loop[A, B any](in <-chan A, done chan<- B, n int, f func(A))
- func Merge[A any](ins ...<-chan A) <-chan A
- func OrderedFilterMap[A, B any](in <-chan A, n int, f func(A) (B, bool)) <-chan B
- func OrderedLoop[A, B any](in <-chan A, done chan<- B, n int, f func(a A, canWrite <-chan struct{}))
- func SendNB[A any](out chan<- A, x A) bool
- type DurableMutex
- type Pool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Loop ¶
Loop processes items from the input channel concurrently using n goroutines. If the done channel is not nil, it will be closed after all items are processed.
func OrderedFilterMap ¶ added in v0.3.0
func OrderedLoop ¶
func OrderedLoop[A, B any](in <-chan A, done chan<- B, n int, f func(a A, canWrite <-chan struct{}))
OrderedLoop is similar to Loop, but it allows the caller to write results to a channel in the same order as the items were read from the input. If the done channel is not nil, it will be closed after all items are processed. A special "canWrite" channel is passed to the user's function f. A typical implementation of f performs the following steps:
- Do some processing (this part is executed concurrently).
- Read from the canWrite channel exactly once. This step is required. Otherwise, behavior is undefined.
- Write the result of the processing somewhere. This step is optional.
This way, processing is done concurrently, but results are written in order.
Types ¶
type DurableMutex ¶ added in v0.9.0
type DurableMutex struct {
// contains filtered or unexported fields
}
DurableMutex is a mutual exclusion lock whose contending callers block durably when used inside a testing/synctest bubble.
Most critical sections do plain non-blocking work. For them, a stock sync.Mutex is the right choice, with or without synctest. DurableMutex exists for one specific pattern: critical sections that do durably blocking operations while holding the lock - for example, atomically receiving values from a channel and appending them to a slice.
Under synctest, the stock mutex breaks this pattern: while the holder is durably blocked on a channel, other goroutines are non-durably blocked on the Lock() calls, so the bubble can neither advance time nor report a deadlock, and the test freezes. With DurableMutex, the test runs normally, and a genuine deadlock is caught and reported by the framework.
DurableMutex does not attempt to prevent starvation. Under contention, acquisition order depends on runtime scheduling, and the same goroutine can win the lock 1000 times in a row. Use it only when waiters are interchangeable and it does not matter which one wins the lock.
A DurableMutex used within a synctest bubble is local to that bubble: every Lock and Unlock call on it must occur in the same bubble.
The zero value is ready to use. Unlocking an unlocked mutex panics, and a DurableMutex must not be copied after first use.
func (*DurableMutex) Lock ¶ added in v0.9.0
func (m *DurableMutex) Lock()
func (*DurableMutex) Unlock ¶ added in v0.9.0
func (m *DurableMutex) Unlock()
type Pool ¶ added in v0.9.0
type Pool[T any] struct { // New creates a value when the pool is empty. It is required. New func() T // Reset prepares a value for reuse. It may be nil. Reset func(T) // Unsynchronized disables the pool's own locking, leaving the caller // responsible for serializing Get and Put. It is for callers that already // hold a lock covering both. Unsynchronized bool // contains filtered or unexported fields }
Pool is a pool of reusable values. It grows on demand and never shrinks; it's the caller's responsibility to ensure that the pool stays bounded.
A Pool is safe for concurrent use unless Unsynchronized is set. Its fields must not be changed once the pool is in use.