iostream

package module
v0.0.0-...-ef8f7f0 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2016 License: Apache-2.0 Imports: 4 Imported by: 0

README

io-stream-go

Helper to stream between Reader and Writer which may close at any point suddenly.

Docs at godocs.org.

Use Case

For one of our services we needed to stream logging data from a backend (available via HTTP) to the frontend (requested also via HTTP). We initially used simply io.Copy but ran into a few problems:

  • If the backend does not provide any more data (but the stream should be keept open), io.Copy blocks on r.Read() even though the client request was closed. Thus the request to the backend ain't closed for a long time (maybe never).
  • http.ResponseWriter uses a buffer internally so not all written data is immediately sent to the client

For this we implemented our own streaming version of io.Copy with the following features:

  • Still copies data with io.Copy (so it still supports io.WriteTo and others)
  • Watches for closing of the clients request using net/http.CloseNotifier and closes the backend reader
  • Any copy error after closing is ignored
  • Data to the net/http.ResponseWriter is automatically flushed (WriteFlusher is copied from Docker)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HttpStream

func HttpStream(res http.ResponseWriter, w io.Writer, r io.ReadCloser) error

HttpStream streams the given reader to the given writer using the ResponseWriter flush and watch canceling the request.

The ResponseWriter must implement `net/http.CloseNotifier` and `net/http.Flusher`. If the request is canceled, the reader will be closed. Any returned error of `io.Copy` is ignored, if the request is already canceled.

github.com/juju/errgo.Mask() is used internally.

func Stream

func Stream(w io.Writer, r io.ReadCloser, cancel <-chan bool) error

Stream continously reads the data from r and writes them w using io.Copy. The copy operation can by sending any bool to cancel. Any returned error of `io.Copy` is ignored, if the request is already canceled.

github.com/juju/errgo.Mask() is used internally.

Types

type WriteFlusher

type WriteFlusher struct {
	W       io.Writer
	Flusher http.Flusher
}

WriteFlusher is a port of Docker's implementation used in their API (we dropped the Mutex). (https://github.com/docker/docker/blob/9ae3134dc9f0652ef48ec1fd445f42d8fe26de35/utils/utils.go#L269) It combines io.Writer and http.Flusher to enable streaming of constant data flows via http connections.

func (*WriteFlusher) Flush

func (wf *WriteFlusher) Flush()

func (*WriteFlusher) Write

func (wf *WriteFlusher) Write(b []byte) (n int, err error)

Write flushes the data immediately after every write operation

Jump to

Keyboard shortcuts

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