From da7982a6dc5c0387d4aadc996cfe5dc53dafbd45 Mon Sep 17 00:00:00 2001 From: Simon J Mudd Date: Wed, 3 Dec 2014 20:57:07 +0100 Subject: [PATCH] Remove time.Ticker and adjust polling mechanism --- main.go | 21 ++++++-------- wait_info/wait_info.go | 64 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 12 deletions(-) create mode 100644 wait_info/wait_info.go diff --git a/main.go b/main.go index 3981b1b..927a151 100644 --- a/main.go +++ b/main.go @@ -22,6 +22,7 @@ import ( "github.com/sjmudd/pstop/lib" "github.com/sjmudd/pstop/state" "github.com/sjmudd/pstop/version" + "github.com/sjmudd/pstop/wait_info" ) const ( @@ -150,7 +151,9 @@ func main() { } var state state.State - interval := time.Second + var wi wait_info.WaitInfo + wi.SetWaitInterval(time.Second) + sigChan := make(chan os.Signal, 1) done := make(chan struct{}) defer close(done) @@ -158,8 +161,6 @@ func main() { signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) - ticker := time.NewTicker(interval) // generate a periodic signal - state.Setup(dbh) finished := false @@ -171,8 +172,9 @@ func main() { case sig := <-sigChan: fmt.Println("Caught a signal", sig) done <- struct{}{} - case <-ticker.C: + case <-wi.WaitNextPeriod(): state.Collect() + wi.CollectedNow() state.Display() case event := <-termboxChan: // switch on event type @@ -187,15 +189,11 @@ func main() { } switch event.Ch { case '-': // decrease the interval if > 1 - if interval > time.Second { - ticker.Stop() - interval -= time.Second - ticker = time.NewTicker(interval) + if wi.WaitInterval() > time.Second { + wi.SetWaitInterval(wi.WaitInterval() - time.Second) } case '+': // increase interval by creating a new ticker - ticker.Stop() - interval += time.Second - ticker = time.NewTicker(interval) + wi.SetWaitInterval(wi.WaitInterval() + time.Second) case 'h': // help state.SetHelp(!state.Help()) case 'q': // quit @@ -216,6 +214,5 @@ func main() { } } state.Cleanup() - ticker.Stop() lib.Logger.Println("Terminating " + lib.MyName()) } diff --git a/wait_info/wait_info.go b/wait_info/wait_info.go new file mode 100644 index 0000000..24b8b77 --- /dev/null +++ b/wait_info/wait_info.go @@ -0,0 +1,64 @@ +package wait_info + +import ( + "github.com/sjmudd/pstop/lib" + "time" +) + +const extra_delay = 200 * time.Millisecond + +// used to record when we need to wa +type WaitInfo struct { + last_collected time.Time + collect_interval time.Duration +} + +// return the configured wait interval +func (wi *WaitInfo) WaitInterval() time.Duration { + return wi.collect_interval +} + +// recognise we've done a collection +func (wi *WaitInfo) CollectedNow() { + wi.SetCollected(time.Now()) +} + +// change the configured wait interval +func (wi *WaitInfo) SetWaitInterval(required_interval time.Duration) { + wi.collect_interval = required_interval +} + +// set the time the last collection happened +func (wi *WaitInfo) SetCollected(collect_time time.Time) { + wi.last_collected = collect_time + lib.Logger.Println("WaitInfo.SetCollected() last_collected=", wi.last_collected) +} + +// return the time the last collection happened +func (wi WaitInfo) LastCollected() time.Time { + return wi.last_collected +} + +// return the amount of time to wait before doing the next collection +func (wi WaitInfo) TimeToWait() time.Duration { + now := time.Now() + lib.Logger.Println("WaitInfo.TimeToWait() now: ", now) + + next_time := wi.last_collected.Add(wi.collect_interval) + lib.Logger.Println("WaitInfo.TimeToWait() next_time: ", next_time) + if next_time.Before(now) { + lib.Logger.Println("WaitInfo.TimeToWait() next_time scheduled time in the past, so schedule", extra_delay, "after", now) + next_time = now + next_time.Add(extra_delay) // add a deliberate tiny delay + lib.Logger.Println("WaitInfo.TimeToWait() next_time: ", next_time, "(corrected)") + } + wait_time := next_time.Sub(now) + lib.Logger.Println("WaitInfo.TimeToWait() returning wait_time:", wait_time) + + return wait_time +} + +// behave like time.After() +func (wi WaitInfo) WaitNextPeriod() <-chan time.Time { + return time.After(wi.TimeToWait()) +} -- 2.20.1