stream

package
v0.0.0-...-c283e9f Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2021 License: BSD-3-Clause Imports: 0 Imported by: 35

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Basic

type Basic struct {
	StopCh chan struct{}
	Error  error
}

Basic is a common implementation of Stream.

Internally it contains:

- a channel that indicates stopping, which the producer side of the stream should use in a select (see the package examples), - an error field that the producer ride of the stream should set in case of problems (just before closing the associated data channel).

func New

func New() *Basic

Initializer for a Basic Stream.

Suggested usage is to only return the 'Stream' interface from your worker method (see also the package examples); however, you'll need the stream.Basic type reference available in your worker method so that it may modify the error status and select on the stop chan.

func (*Basic) Close

func (s *Basic) Close() error

func (*Basic) Err

func (s *Basic) Err() error

type Stream

type Stream interface {
	// Close signals the sender to stop sending and then closes the channel.
	Close() error

	// Err reads the error (if any) that occurred while receiving the stream. It
	// must only be called after the channel has been closed.
	Err() error
}

A Stream allows control over a stream sent to a channel.

Typical usage is in a function signature resembling the following:

func Follow(ch chan<- *Interesting) Stream

Such a function signature is common when spawning a goroutine to produce information (one common example being a goroutine which receives information from the network, deserializes it, and then passes it on to another process as a stream, via the provided channel). Returning the Stream interface solves the twin problems of allowing the worker goroutine to tell the consumer about later errors, and allowing the consumer to tell the worker that it's no longer interested in more information.

Note that though this interface describes its operation in relationship to a channel, the channel is not included in the interface. This is making an end-run around the lack of generics in golang. Using the idiom above to provide the (typed!) channel as a function parameter and returning a Stream is the recommended mechanism for avoiding complication.

Jump to

Keyboard shortcuts

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