Skip to content

Commit

Permalink
Fix race referencing config
Browse files Browse the repository at this point in the history
Three data races existed referencing the Config item - these
can be detected by running after building with 'go build -race'
on go 1.4.2. This fixes them by adding an RWMutex to protect
the config structure.
  • Loading branch information
abligh committed Aug 31, 2015
1 parent c9d0540 commit 75f2193
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
24 changes: 23 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"os"
"sync"
"time"

"github.com/abh/geodns/Godeps/_workspace/src/code.google.com/p/gcfg"
Expand All @@ -23,6 +24,25 @@ type AppConfig struct {
}

var Config = new(AppConfig)
var cfgMutex sync.RWMutex

func (conf *AppConfig) HasStatHat() bool {
cfgMutex.RLock()
defer cfgMutex.RUnlock()
return conf.Flags.HasStatHat
}

func (conf *AppConfig) StatHatApiKey() string {
cfgMutex.RLock()
defer cfgMutex.RUnlock()
return conf.StatHat.ApiKey
}

func (conf *AppConfig) GeoIPDirectory() string {
cfgMutex.RLock()
defer cfgMutex.RUnlock()
return conf.GeoIP.Directory
}

func configWatcher(fileName string) {

Expand Down Expand Up @@ -91,7 +111,9 @@ func configReader(fileName string) error {
// log.Println("STATHAT APIKEY:", cfg.StatHat.ApiKey)
// log.Println("STATHAT FLAG :", cfg.Flags.HasStatHat)

Config = cfg
cfgMutex.Lock()
*Config = *cfg // shallow copy to prevent race conditions in referring to Config.foo()
cfgMutex.Unlock()

return nil
}
4 changes: 2 additions & 2 deletions geodns.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,6 @@ func main() {
metrics := NewMetrics()
go metrics.Updater()

go statHatPoster()

if *flaginter == "*" {
addrs, _ := net.InterfaceAddrs()
ips := make([]string, 0)
Expand All @@ -161,6 +159,8 @@ func main() {

inter := getInterfaces()

go statHatPoster()

Zones := make(Zones)

go monitor(Zones)
Expand Down
5 changes: 3 additions & 2 deletions geoip.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ func (g *GeoIP) GetASN(ip net.IP) (asn string, netmask int) {
}

func (g *GeoIP) setDirectory() {
if len(Config.GeoIP.Directory) > 0 {
geoip.SetCustomDirectory(Config.GeoIP.Directory)
directory := Config.GeoIPDirectory()
if len(directory) > 0 {
geoip.SetCustomDirectory(directory)
}
}

Expand Down
14 changes: 7 additions & 7 deletions stathat.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func (zs *Zones) statHatPoster() {

if len(Config.StatHat.ApiKey) == 0 {
if !Config.HasStatHat() {
return
}

Expand Down Expand Up @@ -40,17 +40,17 @@ func (zs *Zones) statHatPoster() {

apiKey := zone.Logging.StatHatAPI
if len(apiKey) == 0 {
apiKey = Config.StatHat.ApiKey
apiKey = Config.StatHatApiKey()
}
if len(apiKey) == 0 {
continue
}
stathat.PostEZCount("zone "+name+" queries~"+suffix, Config.StatHat.ApiKey, int(newCount))
stathat.PostEZCount("zone "+name+" queries~"+suffix, Config.StatHatApiKey(), int(newCount))

ednsCount := zone.Metrics.EdnsQueries.Count()
newEdnsCount := ednsCount - lastEdnsCounts[name]
lastEdnsCounts[name] = ednsCount
stathat.PostEZCount("zone "+name+" edns queries~"+suffix, Config.StatHat.ApiKey, int(newEdnsCount))
stathat.PostEZCount("zone "+name+" edns queries~"+suffix, Config.StatHatApiKey(), int(newEdnsCount))

}
}
Expand All @@ -68,7 +68,7 @@ func statHatPoster() {
for {
time.Sleep(60 * time.Second)

if !Config.Flags.HasStatHat {
if !Config.HasStatHat() {
log.Println("No stathat configuration")
continue
}
Expand All @@ -79,8 +79,8 @@ func statHatPoster() {
newQueries := current - lastQueryCount
lastQueryCount = current

stathat.PostEZCount("queries~"+suffix, Config.StatHat.ApiKey, int(newQueries))
stathat.PostEZValue("goroutines "+serverID, Config.StatHat.ApiKey, float64(runtime.NumGoroutine()))
stathat.PostEZCount("queries~"+suffix, Config.StatHatApiKey(), int(newQueries))
stathat.PostEZValue("goroutines "+serverID, Config.StatHatApiKey(), float64(runtime.NumGoroutine()))

}
}

0 comments on commit 75f2193

Please sign in to comment.