From e3f958c10e39b8409db8d2e6ca1f9a686652d025 Mon Sep 17 00:00:00 2001 From: Stephen Searles Date: Fri, 11 Aug 2017 18:36:32 -0700 Subject: [PATCH] tweaking names and adding some more callbacks --- idleshut.go | 40 +++++++++++++++++++++++++++------------- idleshut_test.go | 4 ++-- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/idleshut.go b/idleshut.go index 109b655..5e5ad6a 100644 --- a/idleshut.go +++ b/idleshut.go @@ -17,13 +17,21 @@ type Config struct { // stopping the process is aborted. Stop func() error - // IdleTick is called by Process for every iteration of Tick. If - // an error is returned, it is sent to IdleProcessError. IdleTick - // and IdleProcessError must both be set or unset; mixed set-edness - // will result in a panic. - IdleTick func() error - IdleProcessError func(error) - Tick time.Duration + // If TickDuration is non-zero, the process will count ticks where + // the process is active or idle, identified as active by calling + // the Touch() method. When the process has been idle for more than + // MaxIdleTicks, it will be stopped. If ticking is enabled, TickError + // must be set or operations will result in a panic. + TickDuration time.Duration + TickError func(error) + + // Tick is called by Process for every iteration of Tick. If + // an error is returned, it is sent to TickError. Additionally, + // IdleTick and ActiveTick are called for idle and active ticks + // respectively. + Tick func() error + IdleTick func() error + ActiveTick func() error // MaxIdleTicks, if nonzero and ticking is configured, will cause // the process to stop once the maximum number of ticks has been @@ -32,8 +40,8 @@ type Config struct { } func (cfg Config) validate() { - allSet := cfg.IdleTick == nil && cfg.IdleProcessError == nil && cfg.Tick == 0 - noneSet := cfg.IdleTick != nil && cfg.IdleProcessError != nil && cfg.Tick != 0 + allSet := cfg.TickError == nil && cfg.TickDuration == 0 + noneSet := cfg.TickError != nil && cfg.TickDuration != 0 if !allSet && !noneSet { panic("idleshut: Config.IdleTick, config.IdleProcessError, and config.Tick must all be set or all be unset") @@ -108,13 +116,13 @@ func (p *Process) generation() uint { } func (p *Process) startTicker() { - if p.cfg.Tick == 0 || p.cfg.IdleTick == nil { + if p.cfg.TickDuration == 0 || p.cfg.IdleTick == nil { return } startingGen := p.generation() - tk := time.NewTicker(p.cfg.Tick) + tk := time.NewTicker(p.cfg.TickDuration) for { <-tk.C // if we stopped running, or we started again and thus the geneneration has advanced, return @@ -125,10 +133,16 @@ func (p *Process) startTicker() { func() { p.mtx.Lock() defer p.mtx.Unlock() + if p.cfg.Tick != nil { + p.idleProcessError(p.cfg.Tick()) + } if p.touched { p.idleTicks = 0 p.touched = false //reset + if p.cfg.ActiveTick != nil { + p.idleProcessError(p.cfg.ActiveTick()) + } } else { p.idleTicks++ if p.cfg.IdleTick != nil { @@ -143,8 +157,8 @@ func (p *Process) startTicker() { } func (p *Process) idleProcessError(err error) { - if err != nil && p.cfg.IdleProcessError != nil { - go p.cfg.IdleProcessError(err) + if err != nil && p.cfg.TickError != nil { + go p.cfg.TickError(err) } } diff --git a/idleshut_test.go b/idleshut_test.go index 85cd526..b41bba7 100644 --- a/idleshut_test.go +++ b/idleshut_test.go @@ -28,13 +28,13 @@ func TestTicking(t *testing.T) { var idleTicks = &idleTicksNonPtr p := New(Config{ - Tick: 10 * time.Millisecond, + TickDuration: 10 * time.Millisecond, MaxIdleTicks: 10, IdleTick: func() error { atomic.AddUint32(idleTicks, 1) return nil }, - IdleProcessError: func(err error) { + TickError: func(err error) { t.Fatal("got an idle process error:", err) }, })