AlertFunc type is an adapter to allow the use of ordinary functions as Alert handlers.
type AlertFunc func(missed int)
func (f AlertFunc) Alert(missed int)
Alert calls f(missed)
Alerter is used to report how many values were overwritten since the last write.
type Alerter interface { Alert(missed int) }
Diode is any implementation of a diode.
type Diode interface { Set(GenericDataType) TryNext() (GenericDataType, bool) }
GenericDataType is the data type the diodes operate on.
type GenericDataType unsafe.Pointer
ManyToOne diode is optimal for many writers (go-routines B-n) and a single reader (go-routine A). It is not thread safe for multiple readers.
type ManyToOne struct {
// contains filtered or unexported fields
}
func NewManyToOne(size int, alerter Alerter) *ManyToOne
NewManyToOne creates a new diode (ring buffer). The ManyToOne diode is optimized for many writers (on go-routines B-n) and a single reader (on go-routine A). The alerter is invoked on the read's go-routine. It is called when it notices that the writer go-routine has passed it and wrote over data. A nil can be used to ignore alerts.
func (d *ManyToOne) Set(data GenericDataType)
Set sets the data in the next slot of the ring buffer.
func (d *ManyToOne) TryNext() (data GenericDataType, ok bool)
TryNext will attempt to read from the next slot of the ring buffer. If there is no data available, it will return (nil, false).
OneToOne diode is meant to be used by a single reader and a single writer. It is not thread safe if used otherwise.
type OneToOne struct {
// contains filtered or unexported fields
}
func NewOneToOne(size int, alerter Alerter) *OneToOne
NewOneToOne creates a new diode is meant to be used by a single reader and a single writer. The alerter is invoked on the read's go-routine. It is called when it notices that the writer go-routine has passed it and wrote over data. A nil can be used to ignore alerts.
func (d *OneToOne) Set(data GenericDataType)
Set sets the data in the next slot of the ring buffer.
func (d *OneToOne) TryNext() (data GenericDataType, ok bool)
TryNext will attempt to read from the next slot of the ring buffer. If there is no data available, it will return (nil, false).
Poller will poll a diode until a value is available.
type Poller struct { Diode // contains filtered or unexported fields }
func NewPoller(d Diode, opts ...PollerConfigOption) *Poller
NewPoller returns a new Poller that wraps the given diode.
func (p *Poller) Next() GenericDataType
Next polls the diode until data is available or until the context is done. If the context is done, then nil will be returned.
PollerConfigOption can be used to setup the poller.
type PollerConfigOption func(*Poller)
func WithPollingContext(ctx context.Context) PollerConfigOption
WithPollingContext sets the context to cancel any retrieval (Next()). It will not change any results for adding data (Set()). Default is context.Background().
func WithPollingInterval(interval time.Duration) PollerConfigOption
WithPollingInterval sets the interval at which the diode is queried for new data. The default is 10ms.
Waiter will use a conditional mutex to alert the reader to when data is available.
type Waiter struct { Diode // contains filtered or unexported fields }
func NewWaiter(d Diode, opts ...WaiterConfigOption) *Waiter
NewWaiter returns a new Waiter that wraps the given diode.
func (w *Waiter) Next() GenericDataType
Next returns the next data point on the wrapped diode. If there is not any new data, it will Wait for set to be called or the context to be done. If the context is done, then nil will be returned.
func (w *Waiter) Set(data GenericDataType)
Set invokes the wrapped diode's Set with the given data and uses Broadcast to wake up any readers.
WaiterConfigOption can be used to setup the waiter.
type WaiterConfigOption func(*Waiter)
func WithWaiterContext(ctx context.Context) WaiterConfigOption
WithWaiterContext sets the context to cancel any retrieval (Next()). It will not change any results for adding data (Set()). Default is context.Background().