latest

package module
v0.0.0-...-a201efc Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 8, 2021 License: MIT, Unlicense Imports: 0 Imported by: 3

README

latest

A go package for channel types that attempt to always use the latest available value.

The fundamental primitive provided by this package is a channel with a special property: sending never blocks. Receiving still has the normal channel blocking semantics. This is achieved by violating the queue-like behavior of normal channels. Instead of data queuing up when sent, data sent later will replace data that is pending delivery. This isn't desired or useful for all applications, but sometimes it's just what you need.

The above is implemented in latest.Chan.

This package also provides a latest.Worker that sends and recieves on two latest.Chan instances and has a goroutine processing data on the other end. This can be useful for building data processing in which the only data whose results matter is the latest request.

License

Dual MIT/Unlicense

Contribute

Send questions, comments, and patches to my public inbox.

Documentation

Overview

Package latest provides concurrent data structures that waste the least possible work on state data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Chan

type Chan struct {
	// contains filtered or unexported fields
}

Chan acts like a normal channel except that sending will not block. Either it will send immediately, buffer into the single buffer slot, or overwrite the data already in that buffer slot.

Chan is safe to use with a single writer and any number of readers.

func NewChan

func NewChan() *Chan

NewChan initializes a chan.

func (*Chan) Close

func (n *Chan) Close()

Close closes the underlying channel. After closing, you should never Push data.

Like a regular channel close, use care orchestrating Push and Close in different goroutines. In particular, if you Push and Close in different goroutines, you could cause a panic, due to sending on a closed channel.

func (*Chan) Pull

func (n *Chan) Pull() interface{}

Pull blocks until it is able to receive on the channel.

func (*Chan) Push

func (n *Chan) Push(x interface{})

Push sends x. It should never block.

Like a regular channel send, use care orchestrating Push and Close in different goroutines. In particular, if you Push and Close in different goroutines, you could cause a panic, due to sending on a closed channel.

func (*Chan) Raw

func (n *Chan) Raw() <-chan interface{}

Raw exposes the underlying channel of the Chan for use in select statements.

type Worker

type Worker struct {
	// contains filtered or unexported fields
}

Worker provides a worker goroutine that uses the Chan type from this package to send and receive data. This makes it ideal for coordinating work between goroutines when new work always invalidates old work and neither goroutine can afford to block waiting on the other.

Worker should always be constructed by calling NewWorker.

func NewWorker

func NewWorker(work func(in interface{}) interface{}) Worker

NewWorker constructs a worker and launches a goroutine. The provided function will be run on each input to the Push method, and the return value will be available via Pull() or Raw(). Because the input and output are managed wtih Chans, the worker goroutine will only ever block waiting when there is no new input. It will never block trying to send output.

func (Worker) Close

func (w Worker) Close()

Close shuts down the input channel. This will cause the worker goroutine to terminate once it finishes its current work (if any). You can tell when the worker is stopped when the channel returned by Raw() closes.

func (Worker) Pull

func (w Worker) Pull() interface{}

Pull blocks until data is available on the output channel.

func (Worker) Push

func (w Worker) Push(data interface{})

Push sends data on the input channel. It will never block.

func (Worker) Raw

func (w Worker) Raw() <-chan interface{}

Raw returns the output channel for use in a select statement.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL