added Mutex for Mapaccess

This commit is contained in:
Malte Münch 2020-01-31 15:20:40 +01:00
parent 3ac7d60864
commit 88b94d10be
2 changed files with 14 additions and 6 deletions

View File

@ -18,9 +18,9 @@ func Poll(url string) {
data, err := ioutil.ReadAll(resp.Body) data, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
ErrorinPoll(url) ErrorinPoll(url)
resp.Body.Close() _ = resp.Body.Close()
} else { } else {
resp.Body.Close() _ = resp.Body.Close()
var parsed V13 var parsed V13
err = json.Unmarshal(data, &parsed) err = json.Unmarshal(data, &parsed)
@ -30,12 +30,12 @@ func Poll(url string) {
if parsed.State.Open != nil { if parsed.State.Open != nil {
open := parsed.State.Open.(bool) open := parsed.State.Open.(bool)
if open { if open {
spacestates[url] = Open SetSpaceState(url, Open)
} else { } else {
spacestates[url] = Close SetSpaceState(url, Close)
} }
} else { } else {
spacestates[url] = Unknown SetSpaceState(url, Unknown)
} }
} }
@ -47,6 +47,12 @@ func Poll(url string) {
func ErrorinPoll(url string) { func ErrorinPoll(url string) {
if spacestates[url] != Unknown { if spacestates[url] != Unknown {
spacestates[url] = Outdated SetSpaceState(url, Outdated)
} }
} }
func SetSpaceState(url string, s State) {
lock.Lock()
spacestates[url] = s
lock.Unlock()
}

View File

@ -6,6 +6,7 @@ import (
yaml "gopkg.in/yaml.v2" yaml "gopkg.in/yaml.v2"
io "io/ioutil" io "io/ioutil"
"net/http" "net/http"
"sync"
"time" "time"
) )
@ -14,6 +15,7 @@ var conffile = "conf.yml"
var sleeptime time.Duration = 60000000000 // nanoseconds var sleeptime time.Duration = 60000000000 // nanoseconds
var leds [][]string var leds [][]string
var spacestates map[string]State var spacestates map[string]State
var lock = sync.RWMutex{}
func SetConf(s string) { func SetConf(s string) {
conffile = s conffile = s