safe data for continues

This commit is contained in:
Martin/Geno 2019-06-16 19:41:44 +02:00 committed by genofire
parent ed53d1bae7
commit dd98f656d1
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
3 changed files with 8 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package respondd
import ( import (
"io/ioutil" "io/ioutil"
"strings" "strings"
"sync"
"github.com/FreifunkBremen/yanic/data" "github.com/FreifunkBremen/yanic/data"
"github.com/FreifunkBremen/yanic/lib/duration" "github.com/FreifunkBremen/yanic/lib/duration"
@ -26,6 +27,7 @@ type Daemon struct {
Babel string `toml:"babel"` Babel string `toml:"babel"`
babelData *babelState.BabelState `toml:"-"` babelData *babelState.BabelState `toml:"-"`
dataMX sync.Mutex
dataByInterface map[string]*data.ResponseData dataByInterface map[string]*data.ResponseData
Answer *AnswerConfig `toml:"defaults"` Answer *AnswerConfig `toml:"defaults"`

View File

@ -22,6 +22,8 @@ func (d *Daemon) updateData() {
} }
func (d *Daemon) getData(iface string) *data.ResponseData { func (d *Daemon) getData(iface string) *data.ResponseData {
d.dataMX.Lock()
defer d.dataMX.Unlock()
if iData, ok := d.dataByInterface[iface]; ok { if iData, ok := d.dataByInterface[iface]; ok {
return iData return iData
} }

View File

@ -16,7 +16,9 @@ func (d *Daemon) Start() {
d.AnswerByZones = make(map[string]*AnswerConfig) d.AnswerByZones = make(map[string]*AnswerConfig)
} }
d.dataMX.Lock()
d.updateData() d.updateData()
d.dataMX.Unlock()
go d.updateWorker() go d.updateWorker()
if d.Babel != "" { if d.Babel != "" {
@ -69,6 +71,8 @@ func (d *Daemon) updateWorker() {
c := time.Tick(d.DataInterval.Duration) c := time.Tick(d.DataInterval.Duration)
for range c { for range c {
d.dataMX.Lock()
d.updateData() d.updateData()
d.dataMX.Unlock()
} }
} }