bird

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

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

Go to latest
Published: Mar 13, 2016 License: MIT Imports: 5 Imported by: 0

README

bird

Simple restarter for Go rountings

Example


func SomethingThatCanFail(kill <-chan int) error {
  var err error
  // do something ...
  return err
}

func Test(){
  restart:=1*time.Second
  gun := Fly(SomethingThatCanFail, restart)
  // do something
  gun() // stop go-routing
}

Smart bird

Smart bird can be started and stopped multiple times. Start and stop are thread-safe operations

func SomethingThatCanFail(kill bird.Gun) error {
  var err error
  // do something ...
  return err
}

func Test(){
  restart:=1*time.Second
  smartBird:= NewSmartBird(SomethingThatCanFail, restart, "Sokol")
  smartBird.Start() // Start when needs
  // do something
  smartBird.Stop()
  //....
  // Start again if needed
  smartBird.Start()
}

Flocks

Smart birds can be grouped into flock with common operations: raise, land and others (see doc)

flock := NewFlock()
bird := NewSmartBird(noop, 20*time.Second, "Betty")
flock.Include(bird) // Add bird to flock (dublicates are ignored)
flock.Raise() // Raise all birds in flock
// ... Do something
flock.Dissolve(true) // Land all birds and dissolve flock

REST JSON API

Gazer provides JSON API for flock

gz := NewGazer(bird.NewFlock(), nest)
http.Handle("/birds", gz)
http.ListenAndServe(":9090", nil)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action struct {
	Birds     []*SmartBird
	Operation Operation
}

Action details of single manipulation with birds group

type Bird

type Bird interface {
	Run(stop Gun) error // Stop - is signal to force stop
}

Bird is an interface of object which must run forver untill stop signal occrued. If bird stops whenever with or without error it will be restarted after specified timeout. External systems may request force stop by sending any value to stop channel

type ErrorFunc

type ErrorFunc func(err error)

The ErrorFunc function hould process error produced by Bird function

type Flock

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

Flock is a collection of smart birds

func NewFlock

func NewFlock() *Flock

NewFlock initializes new flock of smart birds

func (*Flock) Dissolve

func (f *Flock) Dissolve(land bool)

Dissolve all smart birds from a flock and optionally land them

func (*Flock) Exclude

func (f *Flock) Exclude(landBird bool, birdNames ...string) []*SmartBird

Exclude some smart birds from a flock by their names and then returns them as list

func (*Flock) Include

func (f *Flock) Include(smartBird *SmartBird)

Include new smart bird to a flock or do nothing if it is already in

func (*Flock) Journal

func (f *Flock) Journal() Journal

Journal of all manipulations with birds in the flock. If journal was invoked, reader must read it all otherwise all block

func (*Flock) Land

func (f *Flock) Land(names ...string)

Land some smart birds in a flock by their names. If names not specified - all birds are used

func (*Flock) Raise

func (f *Flock) Raise(names ...string)

Raise all smart birds in a flock. If names not specified - all birds are used

func (*Flock) Select

func (f *Flock) Select(names ...string) []*SmartBird

Select some smart birds from a flock by their names If names not specified - all birds are used

func (*Flock) Take

func (f *Flock) Take(birdToExclude *SmartBird, landBird bool) *SmartBird

Take out smart bird from a flock, land it (if required) and return it. If bird wasn't be in a flock then returns nil

type Gun

type Gun <-chan int

Gun is a channel with stop signal. Any value from this channel indicates that bird must die

type Journal

type Journal <-chan Action

Journal channel of actions in flock

type Operation

type Operation int

Operation type of manipulation

const (
	Land Operation = iota
	Raise
	Include
	Exclude
)

Possible operation types

func (Operation) String

func (op Operation) String() string

type SmartBird

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

A SmartBird is smarter bird then usual: it can starts and stops many times

func NewSmartBird

func NewSmartBird(bird Bird, restart time.Duration, name string) *SmartBird

NewSmartBird constructs new Smart Bird based on usuall bird, but without auto start

func (*SmartBird) Body

func (g *SmartBird) Body() Bird

Body of bird - instance of bird

func (*SmartBird) Flying

func (g *SmartBird) Flying() bool

Flying status

func (*SmartBird) Interval

func (g *SmartBird) Interval() time.Duration

Interval between restart

func (*SmartBird) Name

func (g *SmartBird) Name() string

Name of bird

func (*SmartBird) Start

func (g *SmartBird) Start()

Start bird flying. Thread-safe

func (*SmartBird) Stop

func (g *SmartBird) Stop()

Stop bird flying but without bird's death. Thread-safe

type StopFunc

type StopFunc func()

StopFunc is a gun which may kill the Bird

func Fly

func Fly(bird Bird, restartTimeout time.Duration) StopFunc

Fly forver with specified restart timeout. Returns gun which can interrupt bird flying

func FlyNamed

func FlyNamed(bird Bird, restartTimeout time.Duration, name string) StopFunc

FlyNamed is same as Fly but with custom bird name in log

func FlyWithError

func FlyWithError(bird Bird, errorHanlder ErrorFunc, restartTimeout time.Duration) StopFunc

FlyWithError is a function which monitor and restart bird flying untill gun invoked. Returns gun which can interrupt bird flying

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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