bufftee

package module
v0.0.0-...-0f18f95 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2019 License: MIT Imports: 3 Imported by: 1

README

Buffered and thread safe* tee

BuffTee is a Go package that buffers stream data and makes it available for multiple readers. Readers will start to receive data as soon as it appears in the buffer. Writing and reading can be performed on separate subroutines.

  • Thread safe*
  • Multiple non-blocking readers
  • Fast buffering (using linked data structure)
  • Readable after writing is finished
  • Cancellable

go report card pipeline status coverage report godoc

* Writing and reading can be done on separate subroutines. Separate reader instances can be used on separate subroutines.

When to use BuffTee?

  • Multiple stream data consumers
  • Data must be available after transfer is finished

Installation

Simple install the package to your $GOPATH with the go tool from shell:

$ go get gitlab.com/jonas.jasas/bufftee

Make sure Git is installed on your machine and in your system's PATH.

Example

Example show how to create BuffTee and use it with two consumers on separate go routines.

bt := bufftee.NewBuffTee(nil)

reader1 := bt.NewReader()
go myFirstConsumer(reader1)  // Do things with your data on separate routine

reader2 := bt.NewReader()
go mySecondConsumer(reader2) // Do other things with your data on separate routine

io.Copy(bt, source)
bt.Close()  // Important! Marking that no more data will be added

Working example can be found in examples directory.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BuffTee

type BuffTee struct {
	sync.Mutex
	// contains filtered or unexported fields
}

BuffTee is a Go package that buffers stream data and makes it available for multiple readers. Readers will start to receive data as soon as it appears in the buffer. Writing and reading can be performed on separate subroutines.

func NewBuffTee

func NewBuffTee(cancelChan <-chan struct{}) *BuffTee

NewBuffTee creates new BuffTee instance. cancelChan - a channel that when it is closed it signals that transmission is aborted.

func (*BuffTee) Close

func (t *BuffTee) Close() error

Close signals to all readers that at this point is EOF. Marks that transmission successfully finished.

func (*BuffTee) Memory

func (t *BuffTee) Memory() (n int64)

Memory returns memory amount occupied by the buffer. Returned value is always greater that data size itself.

func (*BuffTee) NewReader

func (t *BuffTee) NewReader() *TeeReader

NewReader creates new reader object that can be used on separate subroutine.

func (*BuffTee) Size

func (t *BuffTee) Size() (n int)

Size returns buffered data size

func (*BuffTee) Write

func (t *BuffTee) Write(b []byte) (n int, err error)

Write implements Writer interface. Write can't be called more than once at the same time on the same instance.

type TeeReader

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

TeeReader is a result type of BuffTee.NewReader() . It allows reading same stream multiple times.

func (*TeeReader) Read

func (tr *TeeReader) Read(b []byte) (n int, err error)

Read implements Reader interface. Starts reading buffered data from the beginning.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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