From bbd9eb5c779d0ee7e47ff2144f8f76cb0479feeb Mon Sep 17 00:00:00 2001 From: "Denis V. Dedkov" Date: Sat, 23 Jul 2016 19:09:18 +0500 Subject: [PATCH] Mutex was added --- noolite-cli.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/noolite-cli.go b/noolite-cli.go index 551c918..e24e676 100644 --- a/noolite-cli.go +++ b/noolite-cli.go @@ -4,13 +4,14 @@ import ( "errors" "flag" "fmt" + "sync" "github.com/dedkovd/noolite" "net/http" "strings" "strconv" ) -func sendCommand(command string, channel, value, r, g, b int) error { +func sendCommand(mutex *sync.Mutex, command string, channel, value, r, g, b int) error { if channel == -1 { return errors.New("Channel was not set") } @@ -19,6 +20,8 @@ func sendCommand(command string, channel, value, r, g, b int) error { return errors.New("Command was not set") } + mutex.Lock() + defer mutex.Unlock() n, err := noolite.DefaultNooliteAdapter() if err != nil { @@ -94,8 +97,10 @@ func main() { flag.Parse() + mutex := &sync.Mutex{} + if *http_port < 0 { - err := sendCommand(*command, *channel, *value, *red, *green, *blue) + err := sendCommand(mutex, *command, *channel, *value, *red, *green, *blue) if err != nil { panic(err) @@ -104,7 +109,7 @@ func main() { http.HandleFunc("/noolite/", func(w http.ResponseWriter, r *http.Request) { command, channel, value, red, green, blue := parseParams(r.URL.Path[1:]) - err := sendCommand(command, channel, value, red, green, blue) + err := sendCommand(mutex, command, channel, value, red, green, blue) if err != nil { fmt.Fprintf(w, "{\"error\": %q}", err)