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)
if err != nil {
ErrorinPoll(url)
resp.Body.Close()
_ = resp.Body.Close()
} else {
resp.Body.Close()
_ = resp.Body.Close()
var parsed V13
err = json.Unmarshal(data, &parsed)
@ -30,12 +30,12 @@ func Poll(url string) {
if parsed.State.Open != nil {
open := parsed.State.Open.(bool)
if open {
spacestates[url] = Open
SetSpaceState(url, Open)
} else {
spacestates[url] = Close
SetSpaceState(url, Close)
}
} else {
spacestates[url] = Unknown
SetSpaceState(url, Unknown)
}
}
@ -47,6 +47,12 @@ func Poll(url string) {
func ErrorinPoll(url string) {
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"
io "io/ioutil"
"net/http"
"sync"
"time"
)
@ -14,6 +15,7 @@ var conffile = "conf.yml"
var sleeptime time.Duration = 60000000000 // nanoseconds
var leds [][]string
var spacestates map[string]State
var lock = sync.RWMutex{}
func SetConf(s string) {
conffile = s