mirror of
https://github.com/prometheus-community/smartctl_exporter.git
synced 2024-12-21 02:21:55 +01:00
Merge pull request #94 from NiceGuyIT/issue-23
Fixes #23: Race condition with maps and goroutines
This commit is contained in:
commit
97947b0e27
1 changed files with 12 additions and 7 deletions
19
readjson.go
19
readjson.go
|
@ -18,6 +18,7 @@ import (
|
|||
"io/ioutil"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-kit/log"
|
||||
|
@ -32,11 +33,11 @@ type JSONCache struct {
|
|||
}
|
||||
|
||||
var (
|
||||
jsonCache map[string]JSONCache
|
||||
jsonCache sync.Map
|
||||
)
|
||||
|
||||
func init() {
|
||||
jsonCache = make(map[string]JSONCache)
|
||||
jsonCache.Store("", JSONCache{})
|
||||
}
|
||||
|
||||
// Parse json to gjson object
|
||||
|
@ -93,16 +94,20 @@ func readData(logger log.Logger, device string) (gjson.Result, error) {
|
|||
return readFakeSMARTctl(logger, device), nil
|
||||
}
|
||||
|
||||
cacheValue, cacheOk := jsonCache[device]
|
||||
if !cacheOk || time.Now().After(cacheValue.LastCollect.Add(*smartctlInterval)) {
|
||||
cacheValue, cacheOk := jsonCache.Load(device)
|
||||
if !cacheOk || time.Now().After(cacheValue.(JSONCache).LastCollect.Add(*smartctlInterval)) {
|
||||
json, ok := readSMARTctl(logger, device)
|
||||
if ok {
|
||||
jsonCache[device] = JSONCache{JSON: json, LastCollect: time.Now()}
|
||||
return jsonCache[device].JSON, nil
|
||||
jsonCache.Store(device, JSONCache{JSON: json, LastCollect: time.Now()})
|
||||
j, found := jsonCache.Load(device)
|
||||
if !found {
|
||||
level.Warn(logger).Log("msg", "device not found", "device", device)
|
||||
}
|
||||
return j.(JSONCache).JSON, nil
|
||||
}
|
||||
return gjson.Parse("{}"), fmt.Errorf("smartctl returned bad data for device %s", device)
|
||||
}
|
||||
return cacheValue.JSON, nil
|
||||
return cacheValue.(JSONCache).JSON, nil
|
||||
}
|
||||
|
||||
// Parse smartctl return code
|
||||
|
|
Loading…
Reference in a new issue