extio

package module
v0.0.0-...-04f50a5 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2018 License: MIT Imports: 4 Imported by: 2

README

#extio

extended io library

docs

examples

##License MIT 2016

Documentation

Overview

Package extio contains extended io strategies

Index

Constants

View Source
const (
	// DefaultBufferSize is the default size used for internal buffers (8kb)
	DefaultBufferSize = 8 << 10
	// DefaultReadChanLength is the default size of channels used to buffer communication
	DefaultReadChanLength = 32
	// DefaultWriteChanLength is the default size of channels used to buffer communication
	DefaultWriteChanLength = 32
)

Variables

View Source
var (
	// ErrAborted indicates an abort was initiated
	ErrAborted = errors.New("aborted")
	// ErrClosed indicates the requested service is closed
	ErrClosed = errors.New("closed")
)

Functions

This section is empty.

Types

type AsyncReader

type AsyncReader struct {
	BufferSize  int
	ChannelSize int
	// contains filtered or unexported fields
}

An AsyncReader takes an io.Reader and buffers it in a goroutine subsequent Read([]byte) calls are populated from buffers sent over an internal channel.

func NewAsyncReader

func NewAsyncReader(r io.Reader) *AsyncReader

NewAsyncReader creates a new AsyncReader from the supplied io.Reader and populates it with defaults

func (*AsyncReader) Close

func (ar *AsyncReader) Close() error

Close aborts the buffering goroutine and emits no more data on subsequent Read([]byte) calls

func (*AsyncReader) Read

func (ar *AsyncReader) Read(b []byte) (int, error)

Read takes a byte slice and copies bytes into it and returns number of bytes read and any error encountered. Will emit io.EOF at completion.

func (*AsyncReader) Start

func (ar *AsyncReader) Start()

Start initializes the goroutine that buffers data from the io.Reader

type Broadcaster

type Broadcaster struct {

	// ReadChanLength is the size of the channel that each
	// BroadcasterReader receives reads from.  This allows
	// parallel broadcasting without requiring lock-step
	// synchronization.  This must be set before calling
	// NewReader().  (default: 32)
	ReadChanLength int

	// ReadBufferSize controls the size in bytes of the buffer
	// allocated for each read by the Broadcaster.  It accomplishes
	// buffered reading, as a bufio.ReaderSize does.  This must
	// not be set after calling Broadcast(). (default: 32kb)
	ReadBufferSize int
	// contains filtered or unexported fields
}

A Broadcaster takes a single io.Reader and broadcasts reads from it in parallel to all BroadcasterReaders.

func NewBroadcaster

func NewBroadcaster(r io.Reader) *Broadcaster

NewBroadcaster creates a new Broadcaster from the supplied io.Reader and sets ReadChanLength and ReadBufferSize to default values.

func (*Broadcaster) Abort

func (b *Broadcaster) Abort()

Abort aborts the broadcast. Causes the Broadcaster and all BroadcasterReaders to stop reading and return ErrAborted.

func (*Broadcaster) Broadcast

func (b *Broadcaster) Broadcast() error

Broadcast initiates reads from the supplied io.Reader and sends them to the BroadcasterReaders. The bytes read from the io.Reader are sent over channels so the entire sequence is safely concurrent. It returns any error returned by from the underlying io.Reader, except io.EOF. If Abort() was called, returns ErrAborted. All errors are passed to all the BroadcasterReaders. Broadcast will block until all BroadcasterReaders close.

func (*Broadcaster) NewReader

func (b *Broadcaster) NewReader() *BroadcasterReader

NewReader creates a new BroadcasterReader that can be consumed as though it were the original io.Reader supplied to the Broadcaster.

type BroadcasterReader

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

A BroadcasterReader satisfies the io.ReadCloser interface and receives it's bytes from the Broadcaster's io.Reader

func (*BroadcasterReader) Close

func (br *BroadcasterReader) Close() error

Close removes the BroadcasterReader from the broadcast stream and causes ErrClosed to be returned on subsequent reads. Close will not block until complete.

func (*BroadcasterReader) Read

func (br *BroadcasterReader) Read(b []byte) (int, error)

Read takes a byte slice and copies broadcast bytes into it and returns number of bytes read and any error encountered.

type MultiWriter

type MultiWriter struct {
	WriteChanLength int
	// contains filtered or unexported fields
}

A MultiWriter satisfies the io.WriteCloser interface and allows for multiple io.Writers to be written to concurrently from a single write. The functionality is similar to the io.MultiWriter except that each io.Writer receives it's data in a separate goroutine.

func NewMultiWriter

func NewMultiWriter(ws ...io.Writer) *MultiWriter

NewMultiWriter creates a MultiWriter from the io.Writer(s) specified as args. This only creates the data structure and does not initialize any goroutines.

func (*MultiWriter) Close

func (mw *MultiWriter) Close() error

Close closes each data channel. After the remaining data is drained from the data channels, each io.Writer is checked for a `Close() error` method. If the method is found it is called. This method blocks until all io.Writers have completed consuming their data channels, and optionally closed. The first error encountered is returned, or nil if none.

func (*MultiWriter) Write

func (mw *MultiWriter) Write(data []byte) (int, error)

Write takes a byte slice and writes it to each io.Writer of the MultiWriter. This happens through channels to allow each io.Writer to process the data concurrently. Any alteration of the byte slice by any io.Writers will produce undefined behavior. Write returns the number of bytes written and any error returned by an io.Writer since the first Write. Due to the buffering of channels, this error is not guaranteed to be present for the write that it fails on.

type ScannerWriter

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

ScannerWriter satisfies the io.WriteCloser interface and turns a series of writes into a stream of tokens that can be processed by a callback.

func NewScannerWriter

func NewScannerWriter(splitFunc bufio.SplitFunc, maxBufSize int, tokenFunc func([]byte) error) *ScannerWriter

NewScannerWriter creates a new ScannerWriter. Arguments are a function that satifies the bufio.SplitFunc type. This is used to parse the incoming byte stream. A maxBufSize, which determines how far to read into the byte stream without finding a token, before throwing an io.ErrShortBuffer. And a tokenFunc that takes the next token identified by splitFunc, and returns an error. An error returned by a splitFunc is returned to the caller of Write().

func (*ScannerWriter) Close

func (sc *ScannerWriter) Close() error

Close closes the ScannerWriter after calling Flush(). Any subsequent writes will return ErrClosed.

func (*ScannerWriter) Flush

func (sc *ScannerWriter) Flush() error

Flush fluses the contents of the buffer to the splitFunc signalling EOF.

func (*ScannerWriter) Write

func (sc *ScannerWriter) Write(data []byte) (int, error)

Write writes the contents of data to the buffer and immediately parses the buffer for as many tokens as splitFunc identifies. Any remaining data is left in the buffer until the next Write or Flush. Returns number of bytes written and any error.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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