tweaking names and adding some more callbacks
This commit is contained in:
+26
-12
@@ -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.
|
||||
// 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
|
||||
IdleProcessError func(error)
|
||||
Tick time.Duration
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -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)
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user