Skip to content

Commit d33d18e

Browse files
authored
Return the cached value if it's not time to scan again yet (#18)
* Return the cached value if it's not time to scan again yet This should ensure that if we have a valid value cached (which ought to be every time after the first scan), we return it as metrics. This fixes the crashes that would happen if queries happened earlier than the re-scan interval allowed. * Address review feedback: Shorten the time-to-scan logic We can express this in a single if statement, so it takes fewer lines to do the "should we check again" check.
1 parent 1625848 commit d33d18e

File tree

1 file changed

+2
-9
lines changed

1 file changed

+2
-9
lines changed

Diff for: readjson.go

+2-9
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,15 @@ func readData(device string) (gjson.Result, error) {
7676

7777
if _, err := os.Stat(device); err == nil {
7878
cacheValue, cacheOk := jsonCache[device]
79-
timeToScan := false
80-
if cacheOk {
81-
timeToScan = time.Now().After(cacheValue.LastCollect.Add(options.SMARTctl.CollectPeriodDuration))
82-
} else {
83-
timeToScan = true
84-
}
85-
86-
if timeToScan {
79+
if !cacheOk || time.Now().After(cacheValue.LastCollect.Add(options.SMARTctl.CollectPeriodDuration)) {
8780
json, ok := readSMARTctl(device)
8881
if ok {
8982
jsonCache[device] = JSONCache{JSON: json, LastCollect: time.Now()}
9083
return jsonCache[device].JSON, nil
9184
}
9285
return gjson.Parse("{}"), fmt.Errorf("smartctl returned bad data for device %s", device)
9386
}
94-
return gjson.Parse("{}"), fmt.Errorf("Too early collect called for device %s", device)
87+
return cacheValue.JSON, nil
9588
}
9689
return gjson.Parse("{}"), fmt.Errorf("Device %s unavialable", device)
9790
}

0 commit comments

Comments
 (0)