From 054f0a5e777fc88187c06cb1c9016c3db3fb7cbd Mon Sep 17 00:00:00 2001 From: Scott Date: Thu, 30 Jan 2020 13:20:44 +1030 Subject: [PATCH] revid: add Burst method to revid --- cmd/rv/main.go | 22 ++-------------------- revid/revid.go | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/cmd/rv/main.go b/cmd/rv/main.go index 3193f92f..1013821b 100644 --- a/cmd/rv/main.go +++ b/cmd/rv/main.go @@ -54,7 +54,6 @@ USAGE package main import ( - "fmt" "io" "os" "runtime/pprof" @@ -199,7 +198,8 @@ func run(rv *revid.Revid, ns *netsender.Sender, l *logger.Logger, nl *netlogger. continue } case modeBurst: - err = burst(l, rv, ns) + dur := time.Duration(rv.Config().BurstPeriod) * time.Second + err = rv.Burst(dur) if err != nil { l.Log(logger.Warning, pkg+"could not start burst", "error", err.Error()) ns.SetMode(modePaused, &vs) @@ -254,21 +254,3 @@ func readPin(rv *revid.Revid) func(pin *netsender.Pin) error { return nil } } - -// burst starts revid, waits for time specified in the Config.BurstPeriod -// field, and then stops revid. -// -// TODO: move this functionality to the revid API into a Revid.Burst(time) method. -func burst(l *logger.Logger, r *revid.Revid, s *netsender.Sender) error { - l.Log(logger.Info, pkg+"starting burst") - - err := r.Start() - if err != nil { - return fmt.Errorf("could not start revid: %w", err) - } - - time.Sleep(time.Duration(r.Config().BurstPeriod) * time.Second) - l.Log(logger.Info, pkg+"stopping burst") - r.Stop() - return nil -} diff --git a/revid/revid.go b/revid/revid.go index 4d0866d2..cfac213c 100644 --- a/revid/revid.go +++ b/revid/revid.go @@ -492,6 +492,21 @@ func (r *Revid) Stop() { r.running = false } +// Burst starts revid, waits for time specified, and then stops revid. +func (r *Revid) Burst(duration time.Duration) error { + r.cfg.Logger.Log(logger.Info, pkg+"starting burst") + + err := r.Start() + if err != nil { + return fmt.Errorf("could not start revid: %w", err) + } + + time.Sleep(duration) + r.cfg.Logger.Log(logger.Info, pkg+"stopping burst") + r.Stop() + return nil +} + func (r *Revid) IsRunning() bool { r.mu.Lock() defer r.mu.Unlock()