tweaking names and adding some more callbacks

master
Stephen Searles 7 years ago
parent 9b3a2a5b2a
commit e3f958c10e
  1. 40
      idleshut.go
  2. 4
      idleshut_test.go

@ -17,13 +17,21 @@ type Config struct {
// stopping the process is aborted. // stopping the process is aborted.
Stop func() error Stop func() error
// IdleTick is called by Process for every iteration of Tick. If // If TickDuration is non-zero, the process will count ticks where
// an error is returned, it is sent to IdleProcessError. IdleTick // the process is active or idle, identified as active by calling
// and IdleProcessError must both be set or unset; mixed set-edness // the Touch() method. When the process has been idle for more than
// will result in a panic. // MaxIdleTicks, it will be stopped. If ticking is enabled, TickError
IdleTick func() error // must be set or operations will result in a panic.
IdleProcessError func(error) TickDuration time.Duration
Tick 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 // MaxIdleTicks, if nonzero and ticking is configured, will cause
// the process to stop once the maximum number of ticks has been // the process to stop once the maximum number of ticks has been
@ -32,8 +40,8 @@ type Config struct {
} }
func (cfg Config) validate() { func (cfg Config) validate() {
allSet := cfg.IdleTick == nil && cfg.IdleProcessError == nil && cfg.Tick == 0 allSet := cfg.TickError == nil && cfg.TickDuration == 0
noneSet := cfg.IdleTick != nil && cfg.IdleProcessError != nil && cfg.Tick != 0 noneSet := cfg.TickError != nil && cfg.TickDuration != 0
if !allSet && !noneSet { if !allSet && !noneSet {
panic("idleshut: Config.IdleTick, config.IdleProcessError, and config.Tick must all be set or all be unset") 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() { func (p *Process) startTicker() {
if p.cfg.Tick == 0 || p.cfg.IdleTick == nil { if p.cfg.TickDuration == 0 || p.cfg.IdleTick == nil {
return return
} }
startingGen := p.generation() startingGen := p.generation()
tk := time.NewTicker(p.cfg.Tick) tk := time.NewTicker(p.cfg.TickDuration)
for { for {
<-tk.C <-tk.C
// if we stopped running, or we started again and thus the geneneration has advanced, return // if we stopped running, or we started again and thus the geneneration has advanced, return
@ -125,10 +133,16 @@ func (p *Process) startTicker() {
func() { func() {
p.mtx.Lock() p.mtx.Lock()
defer p.mtx.Unlock() defer p.mtx.Unlock()
if p.cfg.Tick != nil {
p.idleProcessError(p.cfg.Tick())
}
if p.touched { if p.touched {
p.idleTicks = 0 p.idleTicks = 0
p.touched = false //reset p.touched = false //reset
if p.cfg.ActiveTick != nil {
p.idleProcessError(p.cfg.ActiveTick())
}
} else { } else {
p.idleTicks++ p.idleTicks++
if p.cfg.IdleTick != nil { if p.cfg.IdleTick != nil {
@ -143,8 +157,8 @@ func (p *Process) startTicker() {
} }
func (p *Process) idleProcessError(err error) { func (p *Process) idleProcessError(err error) {
if err != nil && p.cfg.IdleProcessError != nil { if err != nil && p.cfg.TickError != nil {
go p.cfg.IdleProcessError(err) go p.cfg.TickError(err)
} }
} }

@ -28,13 +28,13 @@ func TestTicking(t *testing.T) {
var idleTicks = &idleTicksNonPtr var idleTicks = &idleTicksNonPtr
p := New(Config{ p := New(Config{
Tick: 10 * time.Millisecond, TickDuration: 10 * time.Millisecond,
MaxIdleTicks: 10, MaxIdleTicks: 10,
IdleTick: func() error { IdleTick: func() error {
atomic.AddUint32(idleTicks, 1) atomic.AddUint32(idleTicks, 1)
return nil return nil
}, },
IdleProcessError: func(err error) { TickError: func(err error) {
t.Fatal("got an idle process error:", err) t.Fatal("got an idle process error:", err)
}, },
}) })

Loading…
Cancel
Save