nbreader

package module
v0.0.0-...-7cef48d Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2015 License: BSD-3-Clause Imports: 4 Imported by: 20

README

go-nbreader: a non-blocking io.Reader for go

go-nbreader provides a non-blocking io.Reader for go (golang).

NBReader allows to specify two timeouts:

Timeout: Read() returns after the specified timeout, even if no data has been read.

ChunkTimeout: Read() returns if no data has been read for the specified time, even if the overall timeout has not been hit yet.

ChunkTimeout must be smaller than Timeout.

When the internal buffer contains at least blockSize bytes, Read() returns regardless of the specified timeouts.

Example Usage:

// Create a NBReader that immediately returns on Read(), whether any data has been read or not
nbr := nbreader.NewNBReader(reader, 1 << 16)

// Create a NBReader that tries to return on Read() after no data has been read for 200ms
// or when at least 64k bytes have been read or the maximum timeout of 2 seconds is hit.
nbr := nbreader.NewNBReader(reader, 1 << 16, nbreader.Timeout(2000 * time.Millisecond), nbreader.ChunkTimeout(200 * time.Millisecond))

The full documentation can be found here: http://godoc.org/github.com/svent/go-nbreader

Documentation

Overview

Package nbreader implements a non-blocking io.Reader.

The blockSize defines the buffer size that is used to read from the underlying io.Reader.

NBReader allows to specify two timeouts:

Timeout: Read() returns after the specified timeout, even if no data has been read.

ChunkTimeout: Read() returns if no data has been read for the specified time, even if the overall timeout has not been hit yet.

ChunkTimeout must be smaller than Timeout.

Example Usage:

// Create a NBReader that immediately returns on Read(), whether any data has been read or not
nbr := nbreader.NewNBReader(reader, 1 << 16)

// Create a NBReader that tries to return on Read() after no data has been read for 200ms
// or when the maximum timeout of 2 seconds is hit.
nbr := nbreader.NewNBReader(reader, 1 << 16, nbreader.Timeout(2000 * time.Millisecond), nbreader.ChunkTimeout(200 * time.Millisecond))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NBReader

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

NBReader implements a non-blocking io.Reader.

func NewNBReader

func NewNBReader(reader io.Reader, blockSize int, options ...Option) *NBReader

NewNBReader returns a new NBReader with the given block size.

func (*NBReader) Read

func (r *NBReader) Read(buffer []byte) (int, error)

Read reads data into buffer. It returns the number of bytes read into buffer. At EOF, err will be io.EOF. Read() might still have read data when EOF is returned for the first time.

Note: Read() is not safe for concurrent use.

type Option

type Option func(r *NBReader)

Option implements options that can be passed to NewNBReader.

func ChunkTimeout

func ChunkTimeout(duration time.Duration) Option

ChunkTimeout allows to set the timeout after which the end of a chunk of data is assumed and the read data is returned by Read().

func Timeout

func Timeout(duration time.Duration) Option

Timeout allows to set the timeout after which Read() returns, even if no data has been read.

Jump to

Keyboard shortcuts

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