asyncreader

package
v0.0.0-...-b4aeae5 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package asyncreader provides an asynchronous reader which reads independently of write

Index

Constants

View Source
const (
	// BufferSize is the default size of the async buffer
	BufferSize = 1024 * 1024
)

Variables

View Source
var ErrorStreamAbandoned = errors.New("stream abandoned")

ErrorStreamAbandoned is returned when the input is closed before the end of the stream

Functions

This section is empty.

Types

type AsyncReader

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

AsyncReader will do async read-ahead from the input reader and make the data available as an io.Reader. This should be fully transparent, except that once an error has been returned from the Reader, it will not recover.

func New

func New(ctx context.Context, rd io.ReadCloser, buffers int) (*AsyncReader, error)

New returns a reader that will asynchronously read from the supplied Reader into a number of buffers each of size BufferSize It will start reading from the input at once, maybe even before this function has returned. The input can be read from the returned reader. When done use Close to release the buffers and close the supplied input.

func (*AsyncReader) Abandon

func (a *AsyncReader) Abandon()

Abandon will ensure that the underlying async reader is shut down and memory is returned. It does everything but close the input.

It will NOT close the input supplied on New.

func (*AsyncReader) Close

func (a *AsyncReader) Close() (err error)

Close will ensure that the underlying async reader is shut down. It will also close the input supplied on New.

func (*AsyncReader) Read

func (a *AsyncReader) Read(p []byte) (n int, err error)

Read will return the next available data.

func (*AsyncReader) SkipBytes

func (a *AsyncReader) SkipBytes(skip int) (ok bool)

SkipBytes will try to seek 'skip' bytes relative to the current position. On success it returns true. If 'skip' is outside the current buffer data or an error occurs, Abandon is called and false is returned.

func (*AsyncReader) StopBuffering

func (a *AsyncReader) StopBuffering()

StopBuffering will ensure that the underlying async reader is shut down so no more is read from the input.

This does not free the memory so Abandon() or Close() need to be called on the input.

This does not wait for Read/WriteTo to complete so can be called concurrently to those.

func (*AsyncReader) WriteTo

func (a *AsyncReader) WriteTo(w io.Writer) (n int64, err error)

WriteTo writes data to w until there's no more data to write or when an error occurs. The return value n is the number of bytes written. Any error encountered during the write is also returned.

Jump to

Keyboard shortcuts

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