file_streamer

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2017 License: MIT Imports: 11 Imported by: 0

README

FileStreamer

Streams given file data into any buffered writer. Uses fsNotify for new data detection in files.

The most fresh documentation can be found on GoDoc

Concepts

The library consists of 2 main instances:

Listener:

listener, err := file_streamer.NewListener(<file to watch>, <buffer to write data to>)

Streamer:

streamer := file_streamer.New(<logger>)
err := streamer.Start()

Streamer is a heart of package. In most cases you don't need to create more than one Streamer in your application.

Listener represents a Streamer 'subscription' for data streaming, it binds file to be streamed and buffered writer to be used as a file data receiver.

Examples

The minimal working example (and most trivial I can imagine) is:

package main

import (
	"bufio"
	"github.com/badoo/file-streamer"
	"io/ioutil"
	"log"
	"os"
)

func main() {
	nullLogger := log.New(ioutil.Discard, "", 0)

	streamer := file_streamer.New(nullLogger)
	err := streamer.Start()
	if err != nil {
		log.Fatalln(err)
	}

	targetFile := os.Args[1]
	readFrom, err := os.Open(targetFile)
	if err != nil {
		log.Fatalln(err)
	}

	logTo := bufio.NewWriter(os.Stdout)

	listener := file_streamer.NewListener(readFrom, logTo)
	streamer.StreamTo(listener, 0)
}

It provides similar functionality to GNU tail -f command, but with streaming start from the beginning of the file.

You can find more examples in 'examples/' directory of the package.

Documentation

Overview

Package file_streamer provides a file streaming service that streams all data from a file into a buffered writer.

It uses fsNotify for detecting changes in files, reads that changes (new data) and writes them into a buffered writer.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotRunning is an error returned when action can't be finished because Streamer service is not running yet
	// Streamer.Start() was not called
	ErrNotRunning = errors.New("streamer is not running")

	// ErrRunning is an error returned when action can't be finished because Streamer service is in running state
	ErrRunning = errors.New("streamer is running")

	// ErrListenerClosed is an error returned when Listener can't be used for streaming because it is already closed.
	ErrListenerClosed = errors.New("listener is closed")
)

Functions

func StreamRawData

func StreamRawData(filePath string, initialOffset int64, streamer *Streamer, w http.ResponseWriter, timeout time.Duration) error

StreamRawData hijacks HTTP connection and sends raw file data into a connection buffer.

It does not send any additional information like HTTP headers and does not check connection in any way before sending data. Being used 'as is' it breaks HTTP protocol (sends no headers to a clien) and should not be considered as a correct way to send file data as a part of regular HTTP interaction.

To stream file data inside a valid HTTP response without breaking a connection structure, send headers and any other metadata you want to a client before calling StreamRawData().

Types

type Listener

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

Listener is used for making Streamer to stream data from specific sile to specific buffered writer. Simply, it binds some *os.File to some *bufio.Writer Use NewListener for getting initialized Listener structure ready for usage in Streamer.

func NewListener

func NewListener(file *os.File, writeDataTo *bufio.Writer) *Listener

NewListener creates initialized Listener ready to be provided to Streamer.StreamTo() function

func (*Listener) Close

func (bs *Listener) Close()

Close prevents Streamer to stream any more data to this listener.

Listeners are not reusable. After streaming was stopped (listener was closed), you can't use listener again for another streaming.

func (*Listener) IsClosed

func (bs *Listener) IsClosed() bool

IsClosed returns true when listener is not available to receive data from the file and send it to the buffer any more.

type Streamer

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

Streamer is a main package instance that provides streaming service to all Listeners

func New

func New(logger *log.Logger) *Streamer

New creates new instance of file streamer. Usually, you don't need more than one instance of Streamer.

func (*Streamer) IsRunning

func (s *Streamer) IsRunning() bool

IsRunning is a thread-safe way to check Streamer is in 'Running' state.

'Running' state means Streamer reads fsNotify events on files, reads data from files and streams it to buffered writers.

func (*Streamer) SetLogger

func (s *Streamer) SetLogger(l *log.Logger)

SetLogger changes main Streamer logger

func (*Streamer) Start

func (s *Streamer) Start() error

Start streamer.

Makes Streamer's .StreamTo() available for Listeners. After return from this function Streamer is able to stream files into buffered writers.

Thread safe.

func (*Streamer) Stop

func (s *Streamer) Stop() error

Stop streamer.

Prevents new streams creation, finishes all existing subscriptions and stops all internal goroutines.

Thread safe.

func (*Streamer) StreamTo

func (s *Streamer) StreamTo(listener *Listener, timeout time.Duration) error

StreamTo makes streamer to start data streaming for Listener. StreamTo will block until <listener> is closed (listener.Close() is called) or file is not modified more than <timeout> time.

Zero value ('0') as <timeout> disables timeout at all, making Streamer to stream data until Listener is closed (by .Close() call)

returns ErrNotRunning when Streamer is not ready for streaming data (was not Start()'ed, or was Stop()'ed)

returns ErrListenerClosed when listener is not ready for accepting data.

Notes

Bugs

  • on kqueue systems (like MacOS X, for example) file_streamer can't stream named pipes. 'kqueue' can't notify about FIFO file events, so fsNotify just does not provide any signals on data writes to a FIFO file.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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