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.
This commit is contained in:
@@ -23,6 +23,7 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -89,7 +90,8 @@ func (obj *VUMeterFunc) Stream() error {
|
|||||||
// FIXME: this goChan seems to work better than the ticker :)
|
// FIXME: this goChan seems to work better than the ticker :)
|
||||||
// this is because we have a ~1sec delay in capturing the value in exec
|
// this is because we have a ~1sec delay in capturing the value in exec
|
||||||
goChan := make(chan struct{})
|
goChan := make(chan struct{})
|
||||||
close(goChan)
|
once := &sync.Once{}
|
||||||
|
onceFunc := func() { close(goChan) } // only run once!
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case input, ok := <-obj.init.Input:
|
case input, ok := <-obj.init.Input:
|
||||||
@@ -109,9 +111,11 @@ func (obj *VUMeterFunc) Stream() error {
|
|||||||
obj.symbol = input.Struct()["symbol"].Str()
|
obj.symbol = input.Struct()["symbol"].Str()
|
||||||
obj.multiplier = input.Struct()["multiplier"].Int()
|
obj.multiplier = input.Struct()["multiplier"].Int()
|
||||||
obj.peak = input.Struct()["peak"].Float()
|
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 <-ticker.C: // received the timer event
|
||||||
case <-goChan:
|
case <-goChan: // triggers constantly
|
||||||
|
|
||||||
if obj.last == nil {
|
if obj.last == nil {
|
||||||
continue // still waiting for input values
|
continue // still waiting for input values
|
||||||
|
|||||||
Reference in New Issue
Block a user