diff --git a/lang/funcs/core/deploy/abspath_func.go b/lang/funcs/core/deploy/abspath_func.go index 56ad2fed..490961c4 100644 --- a/lang/funcs/core/deploy/abspath_func.go +++ b/lang/funcs/core/deploy/abspath_func.go @@ -43,8 +43,8 @@ type AbsPathFunc struct { data *interfaces.FuncData last types.Value // last value received to use for diff - path string // the active path - result string // last calculated output + path string // the active path + result *string // last calculated output closeChan chan struct{} } @@ -130,10 +130,10 @@ func (obj *AbsPathFunc) Stream() error { } result += obj.path - if obj.result == result { + if obj.result != nil && *obj.result == result { continue // result didn't change } - obj.result = result // store new result + obj.result = &result // store new result case <-obj.closeChan: return nil @@ -141,7 +141,7 @@ func (obj *AbsPathFunc) Stream() error { select { case obj.init.Output <- &types.StrValue{ - V: obj.result, + V: *obj.result, }: case <-obj.closeChan: return nil diff --git a/lang/funcs/core/deploy/readfile_func.go b/lang/funcs/core/deploy/readfile_func.go index 870538d3..70dca5e8 100644 --- a/lang/funcs/core/deploy/readfile_func.go +++ b/lang/funcs/core/deploy/readfile_func.go @@ -40,8 +40,8 @@ type ReadFileFunc struct { data *interfaces.FuncData last types.Value // last value received to use for diff - filename string // the active filename - result string // last calculated output + filename string // the active filename + result *string // last calculated output closeChan chan struct{} } @@ -139,10 +139,10 @@ func (obj *ReadFileFunc) Stream() error { result := string(content) // convert to string - if obj.result == result { + if obj.result != nil && *obj.result == result { continue // result didn't change } - obj.result = result // store new result + obj.result = &result // store new result case <-obj.closeChan: return nil @@ -150,7 +150,7 @@ func (obj *ReadFileFunc) Stream() error { select { case obj.init.Output <- &types.StrValue{ - V: obj.result, + V: *obj.result, }: case <-obj.closeChan: return nil diff --git a/lang/funcs/core/deploy/readfileabs_func.go b/lang/funcs/core/deploy/readfileabs_func.go index cd6aa658..2ac736c3 100644 --- a/lang/funcs/core/deploy/readfileabs_func.go +++ b/lang/funcs/core/deploy/readfileabs_func.go @@ -40,8 +40,8 @@ type ReadFileAbsFunc struct { data *interfaces.FuncData last types.Value // last value received to use for diff - filename string // the active filename - result string // last calculated output + filename string // the active filename + result *string // last calculated output closeChan chan struct{} } @@ -125,10 +125,10 @@ func (obj *ReadFileAbsFunc) Stream() error { result := string(content) // convert to string - if obj.result == result { + if obj.result != nil && *obj.result == result { continue // result didn't change } - obj.result = result // store new result + obj.result = &result // store new result case <-obj.closeChan: return nil @@ -136,7 +136,7 @@ func (obj *ReadFileAbsFunc) Stream() error { select { case obj.init.Output <- &types.StrValue{ - V: obj.result, + V: *obj.result, }: case <-obj.closeChan: return nil diff --git a/lang/funcs/core/example/vumeter_func.go b/lang/funcs/core/example/vumeter_func.go index 91ebd224..d103c553 100644 --- a/lang/funcs/core/example/vumeter_func.go +++ b/lang/funcs/core/example/vumeter_func.go @@ -45,7 +45,7 @@ type VUMeterFunc struct { multiplier int64 peak float64 - result string // last calculated output + result *string // last calculated output closeChan chan struct{} } @@ -153,10 +153,10 @@ func (obj *VUMeterFunc) Stream() error { return errwrap.Wrapf(err, "could not generate visual") } - if obj.result == result { + if obj.result != nil && *obj.result == result { continue // result didn't change } - obj.result = result // store new result + obj.result = &result // store new result case <-obj.closeChan: return nil @@ -164,7 +164,7 @@ func (obj *VUMeterFunc) Stream() error { select { case obj.init.Output <- &types.StrValue{ - V: obj.result, + V: *obj.result, }: case <-obj.closeChan: return nil diff --git a/lang/funcs/core/fmt/printf_func.go b/lang/funcs/core/fmt/printf_func.go index fa95777c..97bc09b8 100644 --- a/lang/funcs/core/fmt/printf_func.go +++ b/lang/funcs/core/fmt/printf_func.go @@ -51,7 +51,7 @@ type PrintfFunc struct { init *interfaces.Init last types.Value // last value received to use for diff - result string // last calculated output + result *string // last calculated output closeChan chan struct{} } @@ -224,10 +224,10 @@ func (obj *PrintfFunc) Stream() error { return err // no errwrap needed b/c helper func } - if obj.result == result { + if obj.result != nil && *obj.result == result { continue // result didn't change } - obj.result = result // store new result + obj.result = &result // store new result case <-obj.closeChan: return nil @@ -235,7 +235,7 @@ func (obj *PrintfFunc) Stream() error { select { case obj.init.Output <- &types.StrValue{ - V: obj.result, + V: *obj.result, }: case <-obj.closeChan: return nil diff --git a/lang/funcs/core/os/readfile_func.go b/lang/funcs/core/os/readfile_func.go index 7ab57761..e08048de 100644 --- a/lang/funcs/core/os/readfile_func.go +++ b/lang/funcs/core/os/readfile_func.go @@ -46,7 +46,7 @@ type ReadFileFunc struct { events chan error // internal events wg *sync.WaitGroup - result string // last calculated output + result *string // last calculated output closeChan chan struct{} } @@ -195,10 +195,10 @@ func (obj *ReadFileFunc) Stream() error { } result := string(content) // convert to string - if obj.result == result { + if obj.result != nil && *obj.result == result { continue // result didn't change } - obj.result = result // store new result + obj.result = &result // store new result case <-obj.closeChan: return nil @@ -206,7 +206,7 @@ func (obj *ReadFileFunc) Stream() error { select { case obj.init.Output <- &types.StrValue{ - V: obj.result, + V: *obj.result, }: case <-obj.closeChan: return nil diff --git a/lang/funcs/core/sys/cpucount_fact.go b/lang/funcs/core/sys/cpucount_fact.go index eb05f837..ee4943f2 100644 --- a/lang/funcs/core/sys/cpucount_fact.go +++ b/lang/funcs/core/sys/cpucount_fact.go @@ -89,6 +89,8 @@ func (obj CPUCountFact) Stream() error { closeChan := make(chan struct{}) // channel to unblock selects in goroutine defer close(closeChan) + var once bool // did we send at least once? + // wait for kernel to poke us about new device changes on the system wg.Add(1) go func() { @@ -121,6 +123,8 @@ func (obj CPUCountFact) Stream() error { if err != nil { obj.init.Logf("Could not get initial CPU count. Setting to zero.") } + // TODO: would we rather error instead of sending zero? + case event, ok := <-eventChan: if !ok { continue @@ -142,7 +146,7 @@ func (obj CPUCountFact) Stream() error { return nil } - if newCount == cpuCount { + if once && newCount == cpuCount { continue } cpuCount = newCount @@ -151,6 +155,7 @@ func (obj CPUCountFact) Stream() error { case obj.init.Output <- &types.IntValue{ V: cpuCount, }: + once = true // send case <-obj.closeChan: return nil diff --git a/lang/funcs/core/template_func.go b/lang/funcs/core/template_func.go index 8b0905d7..465ac4ad 100644 --- a/lang/funcs/core/template_func.go +++ b/lang/funcs/core/template_func.go @@ -68,7 +68,7 @@ type TemplateFunc struct { init *interfaces.Init last types.Value // last value received to use for diff - result string // last calculated output + result *string // last calculated output closeChan chan struct{} } @@ -353,10 +353,10 @@ func (obj *TemplateFunc) Stream() error { return err // no errwrap needed b/c helper func } - if obj.result == result { + if obj.result != nil && *obj.result == result { continue // result didn't change } - obj.result = result // store new result + obj.result = &result // store new result case <-obj.closeChan: return nil @@ -364,7 +364,7 @@ func (obj *TemplateFunc) Stream() error { select { case obj.init.Output <- &types.StrValue{ - V: obj.result, + V: *obj.result, }: case <-obj.closeChan: return nil diff --git a/lang/funcs/simple/simple.go b/lang/funcs/simple/simple.go index b61c2b7c..7d3e23cb 100644 --- a/lang/funcs/simple/simple.go +++ b/lang/funcs/simple/simple.go @@ -140,6 +140,7 @@ func (obj *WrappedFunc) Stream() error { return errwrap.Wrapf(err, "simple function errored") } + // TODO: do we want obj.result to be a pointer instead? if obj.result == result { continue // result didn't change } diff --git a/lang/funcs/simplepoly/simplepoly.go b/lang/funcs/simplepoly/simplepoly.go index 53f3fe93..017166b4 100644 --- a/lang/funcs/simplepoly/simplepoly.go +++ b/lang/funcs/simplepoly/simplepoly.go @@ -280,6 +280,7 @@ func (obj *WrappedFunc) Stream() error { obj.init.Logf("Function returned with: %+v", values) } + // TODO: do we want obj.result to be a pointer instead? if obj.result == result { continue // result didn't change }