From 28451d1e146ebd23b58eb102197e9af94e812836 Mon Sep 17 00:00:00 2001 From: James Shubin Date: Sat, 6 Feb 2021 23:59:06 -0500 Subject: [PATCH] lang: funcs: Fixup race in vumeter example The vumeter example was written quickly and without much care. This fixes a possible race (panic) and also removes the busy loop that wastes CPU while we're waiting for the first value to come in. --- lang/funcs/core/example/vumeter_func.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lang/funcs/core/example/vumeter_func.go b/lang/funcs/core/example/vumeter_func.go index 198b3c36..7f0f7104 100644 --- a/lang/funcs/core/example/vumeter_func.go +++ b/lang/funcs/core/example/vumeter_func.go @@ -23,6 +23,7 @@ import ( "os/exec" "strconv" "strings" + "sync" "syscall" "time" @@ -89,7 +90,8 @@ func (obj *VUMeterFunc) Stream() error { // FIXME: this goChan seems to work better than the ticker :) // this is because we have a ~1sec delay in capturing the value in exec goChan := make(chan struct{}) - close(goChan) + once := &sync.Once{} + onceFunc := func() { close(goChan) } // only run once! for { select { case input, ok := <-obj.init.Input: @@ -109,9 +111,11 @@ func (obj *VUMeterFunc) Stream() error { obj.symbol = input.Struct()["symbol"].Str() obj.multiplier = input.Struct()["multiplier"].Int() obj.peak = input.Struct()["peak"].Float() + once.Do(onceFunc) + continue // we must wrap around and go in through goChan //case <-ticker.C: // received the timer event - case <-goChan: + case <-goChan: // triggers constantly if obj.last == nil { continue // still waiting for input values