iohelper

package
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2020 License: Apache-2.0 Imports: 13 Imported by: 10

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDeadlineExceeded = os.ErrDeadlineExceeded
)

Functions

func CheckBytesToRead added in v0.3.3

func CheckBytesToRead(fd uintptr) (int, error)

CheckBytesToRead calls ioctl(fd, FIONREAD) to check ready data size of fd

func CopyDir

func CopyDir(src string, dst string) (err error)

CopyDir recursively copies a directory tree, attempting to preserve permissions. Source directory must exist, destination directory must *not* exist. Symlinks are ignored and skipped.

func CopyFile

func CopyFile(src, dst string) (err error)

CopyFile copies the contents of the file named src to the file named by dst. The file will be created if it does not already exist. If the destination file exists, all it's contents will be replaced by the contents of the source file. The file mode will be copied from the source and the copied data is synced/flushed to stable storage.

func NopWriteCloser added in v0.3.1

func NopWriteCloser(w io.Writer) io.WriteCloser

func Pipe

func Pipe() (io.ReadCloser, io.WriteCloser)

Pipe returns io.Pipe if os.Pipe errored

func TempFilename added in v0.2.0

func TempFilename(dir, pattern string) (string, error)

func WriteFile

func WriteFile(file string, data []byte, perm os.FileMode, overwrite bool) (undo func() error, _ error)

WriteFile write data to target file

Types

type TimeoutReader

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

TimeoutReader is a reader with read timeout

It is designed for those want to read some data from a stream, and the size of the data is unknown, but still want to pipe data to some destination at certain interval

example use case: data streaming for shell interaction over MQTT

	when user input/output is slow, shall we send one character a time for real-time
	interaction? what if the user executed `cat some-large-file`? what if user
	was sending a large chunk of data over stdin?

	for raw tcp connection that's fine if you have configured tcp buffering correctly,
	but for packet oriented connections (in this case MQTT), send one packet per byte
	will signaficantly increase protocol overhead.

	with TimeoutReader we can read data generated in some interval (say 20ms), no
 real-time experience lost while still keep protocol overhead at a reasonable level

func NewTimeoutReader

func NewTimeoutReader(r io.Reader) *TimeoutReader

NewTimeoutReader creates a new idle timeout reader

func (*TimeoutReader) Error

func (t *TimeoutReader) Error() error

Error returns the error happened during reading in background

func (*TimeoutReader) FallbackReading added in v0.3.3

func (t *TimeoutReader) FallbackReading()

FallbackReading is a helper routine for data reading from readers has no SetReadDeadline or SetReadDeadline failed when being called

this function will block until EOF or error, so must be called in a goroutine other than the one you are reading data

func (*TimeoutReader) Read added in v0.3.3

func (t *TimeoutReader) Read(maxWait time.Duration, p []byte) (n int, err error)

Read performs a read operation with timeout option, function will return when maxWait exceeded or p is full if the function returned because of timeout, the returned error is ErrDeadlineExceeded for go1.15 and on, it's os.ErrDeadlineExceeded

func (*TimeoutReader) WaitForData added in v0.3.3

func (t *TimeoutReader) WaitForData(stopSig <-chan struct{}) bool

WaitForData is a helper function used to check if there is data available in reader so we can reduce actual call of Read when the timeout is a short duration

when return value is true, you can call Read to read data, otherwise, false means the stopSig has signaled, and we have no idea whether you should continue Read from the reader

Jump to

Keyboard shortcuts

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