Documentation ¶
Index ¶
- Constants
- func AbortAfter(t time.Duration, routine func()) bool
- func Fanout(in interface{}, out ...interface{})
- func Funnel(out interface{}, in ...interface{})
- func Turnout(in []interface{}, out []interface{})
- type Fuse
- type LimitError
- type LockedError
- type Mutex
- type Queue
- type SpinPriority
- type Spinner
- type Stack
- type TimeoutError
- type WaitGroup
Constants ¶
const ( // SpinPrioritySuspend should be used for spinning loops that are expected // to wait for very long periods of time. The loop will sleep for 1 second // after 100 iterations. SpinPrioritySuspend = SpinPriority(iota) // SpinPriorityLow should be used for spinning loops that are expected to // spin for a rather long time before being able to exit. // After 100 loops the caller waits for 100 milliseconds. SpinPriorityLow = SpinPriority(iota) // SpinPriorityMedium should be used for spinning loops that are expected to // spin for a short amount of time before being able to exit. // After 100 loops the caller waits for 1 millisecond. SpinPriorityMedium = SpinPriority(iota) // SpinPriorityHigh should be used for spinning loops that are expected to // almost never spin. // After 100 loops the caller waits for 10 microseconds. SpinPriorityHigh = SpinPriority(iota) // SpinPriorityRealtime should be used for loops that need to run as fast // as possible. After 100 loops the go scheduler is triggered. SpinPriorityRealtime = SpinPriority(iota) )
Variables ¶
This section is empty.
Functions ¶
func AbortAfter ¶
func Fanout ¶
func Fanout(in interface{}, out ...interface{})
Fanout receives from the given in channel and forwards the data to the first non-blocking out channel. Fanout returns when in has been closed.
Types ¶
type Fuse ¶
type Fuse struct {
// contains filtered or unexported fields
}
Fuse is a local circuit breaker implementation that is ment to be used to manage the state of a given resource between different threads of execution (consumer/producer). If the resource is not available the fuse is "burned". Components may now wait on that fuse and are woken as soon as the resource becomes available again (the fuse is "activated" again).
func NewFuse ¶
func NewFuse() *Fuse
NewFuse creates a new Fuse and returns it. A new fuse is always active.
func (*Fuse) Activate ¶
func (fuse *Fuse) Activate()
Activate sets the fuse back to the "running" state. An already active fuse cannot be activated again (call is ignored).
func (*Fuse) Burn ¶
func (fuse *Fuse) Burn()
Burn sets the fuse back to the "inactive" state. An already burned fuse cannot be burned again (call is ignored).
type LimitError ¶
type LimitError terrors.SimpleError
LimitError is returned when a datastructure reached its limit
func (LimitError) Error ¶
func (err LimitError) Error() string
type LockedError ¶
type LockedError terrors.SimpleError
LockedError is returned when an item has been encountered as locked
func (LockedError) Error ¶
func (err LockedError) Error() string
type Mutex ¶
type Mutex struct {
// contains filtered or unexported fields
}
Mutex is a lightweight, spinner based mutex implementation, extending the standard go mutex by the possibility to query the mutex' state and by adding a TryLock function.
func NewMutex ¶
func NewMutex(priority SpinPriority) *Mutex
NewMutex creates a new mutex with the given spin priority used during Lock.
func (*Mutex) IsLocked ¶
IsLocked returns the state of this mutex. The result of this function might change directly after call so it should only be used in situations where this fact is not considered problematic.
type Queue ¶
type Queue struct {
// contains filtered or unexported fields
}
Queue implements a multi-producer, multi-consumer, fair, lockfree queue. Please note that while this queue is faster than a chan interface{} it is not faster than a channel of a specific type due to the overhead that interface{} brings.
How it works: Given a queue of capacity 7, we have two write indexes "p" and "n" for process and next.
reader n/p v
[ |1|2|3| | | | ]
^ ^ p n
Here threads 1,2 and 3 are writing to slots 1,2,3. When 3 is done writing it has to wait for p to move to slot 3. Same for slot 2. When 1 is done writing it immediatly moves p to slot 2, making it possible for 2 to continue. Same for slot 3. While p is still on 1 the "reader n" can be fetched but Pop will wait until p has moved on. If "reader n" is done, "reader p" is moved in similar fashion as p for writes. This implements a FIFO queue for writes and reads and makes sure that no incomplete reads or overwrites occur.
func NewQueueWithSpinner ¶
NewQueueWithSpinner allows to set the spinning priority of the queue to be created.
func (*Queue) Close ¶
func (q *Queue) Close()
Close blocks the queue from write access. It also allows Pop() to return false as a second return value
func (*Queue) IsEmpty ¶
IsEmpty returns true if there is no item in the queue to be processed. Please note that this state is extremely volatile unless IsClosed returned true.
func (*Queue) Pop ¶
func (q *Queue) Pop() interface{}
Pop removes an item from the queue. This call may block if the queue is empty. If the queue is drained Pop() will not block and return nil.
type Spinner ¶
type Spinner struct {
// contains filtered or unexported fields
}
Spinner is a helper struct for spinning loops.
func NewCustomSpinner ¶
NewCustomSpinner creates a new spinner with a custom delay.
func NewSpinner ¶
func NewSpinner(priority SpinPriority) Spinner
NewSpinner creates a new helper for spinning loops
type Stack ¶
type Stack struct {
// contains filtered or unexported fields
}
Stack implements a simple, growing, lockfree stack. The main idea is to use the sign bit of the head index as a mutex. If the index is negative, the stack is locked so we need to spin. If the index is non-negative the stack is unlocked and we can write or read.
func NewStack ¶
NewStack creates a new stack with the given initial size. The given size will also be used as grow size. SpinPriorityMedium is used to initialize the spinner.
func NewStackWithGrowSize ¶
NewStackWithGrowSize allows to pass a custom grow size to the stack. SpinPriorityMedium is used to initialize the spinner.
func NewStackWithSpinner ¶
NewStackWithSpinner allows to pass a custom spinner to the stack. The given size will also be used as grow size.
func NewStackWithSpinnerAndGrowSize ¶
NewStackWithSpinnerAndGrowSize allows to fully configure the new stack.
func (*Stack) Len ¶
Len returns the number of elements on the stack. Please note that this value can be highly unreliable in multithreaded environments as this is only a snapshot of the state at calltime.
type TimeoutError ¶
type TimeoutError terrors.SimpleError
TimeoutError is returned when a function returned because of a timeout
func (TimeoutError) Error ¶
func (err TimeoutError) Error() string
type WaitGroup ¶
type WaitGroup struct {
// contains filtered or unexported fields
}
WaitGroup is a replacement for sync/waitgroup that allows the internal counter to go negative. This version allows a missed Done to be recovered but will make it a lot harder to detect missing Done or Add calls. Use only where needed.
func (*WaitGroup) Add ¶
Add increments the waitgroup counter by the given value. Delta may be negative.
func (*WaitGroup) IncWhenDone ¶
func (wg *WaitGroup) IncWhenDone()
IncWhenDone wait until the counter is exactly 0 and triggeres an increment if this is found to be true