func (l *loop) Kill(err error) {
	l.terminate(err)
}
Kill implements the Observable interface.
package loop
import (
	"fmt"
	"sync"
	"time"
	"github.com/tideland/golib/errors"
	"github.com/tideland/golib/identifier"
	"github.com/tideland/golib/logger"
)
func Go(lf LoopFunc, dps ...interface{}) Loop {
	descr := identifier.SepIdentifier("::", dps...)
	return goLoop(lf, nil, nil, nil, descr)
}
func GoRecoverable(lf LoopFunc, rf RecoverFunc, dps ...interface{}) Loop {
	descr := identifier.SepIdentifier("::", dps...)
	return goLoop(lf, rf, nil, nil, descr)
}
func GoSentinel(dps ...interface{}) Sentinel {
	descr := identifier.SepIdentifier("::", dps...)
	return goSentinel(nil, nil, descr)
}
func GoNotifiedSentinel(nhf NotificationHandlerFunc, dps ...interface{}) Sentinel {
	descr := identifier.SepIdentifier("::", dps...)
	return goSentinel(nhf, nil, descr)
}
type Recovering struct {
	Time   time.Time
	Reason interface{}
}
type Recoverings []*Recovering
func (rs Recoverings) Frequency(num int, dur time.Duration) bool {
	if len(rs) >= num {
		first := rs[len(rs)-num].Time
		last := rs[len(rs)-1].Time
		return last.Sub(first) <= dur
	}
	return false
}
func (rs Recoverings) Len() int {
	return len(rs)
}
func (rs Recoverings) Trim(l int) Recoverings {
	if l >= len(rs) {
		return rs
	}
	return rs[len(rs)-l:]
}
func (rs Recoverings) First() *Recovering {
	if len(rs) > 0 {
		return rs[0]
	}
	return nil
}
func (rs Recoverings) Last() *Recovering {
	if len(rs) > 0 {
		return rs[len(rs)-1]
	}
	return nil
}
type RecoverFunc func(rs Recoverings) (Recoverings, error)
type Observable interface {
	fmt.Stringer
	Stop() error
	Kill(err error)
	Wait() error
	Restart() error
	Error() (status int, err error)
	attachSentinel(s *sentinel)
}
const (
	Running = iota
	Stopping
	Stopped
)
type LoopFunc func(l Loop) error
type Loop interface {
	Observable
	ShallStop() <-chan struct{}
	IsStopping() <-chan struct{}
}
type loop struct {
	mux         sync.Mutex
	descr       string
	status      int
	err         error
	loopF       LoopFunc
	recoverF    RecoverFunc
	recoverings Recoverings
	startedC    chan struct{}
	stopC       chan struct{}
	doneC       chan struct{}
	owner       Observable
	sentinel    *sentinel
}
func goLoop(lf LoopFunc, rf RecoverFunc, o Observable, s *sentinel, d string) *loop {
	l := &loop{
		descr:    d,
		loopF:    lf,
		recoverF: rf,
		startedC: make(chan struct{}),
		stopC:    make(chan struct{}),
		doneC:    make(chan struct{}),
		owner:    o,
		sentinel: s,
	}
	if l.owner == nil {
		l.owner = l
	}
	l.logf(false, "loop %q starts", l)
	go l.run()
	<-l.startedC
	return l
}
func (l *loop) String() string {
	return l.descr
}
func (l *loop) Stop() error {
	l.terminate(nil)
	return l.Wait()
}