-
Notifications
You must be signed in to change notification settings - Fork 13
/
config_cache.go
69 lines (58 loc) · 1.52 KB
/
config_cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package circonus
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"github.com/circonus-labs/go-apiclient"
apiclicfg "github.com/circonus-labs/go-apiclient/config"
)
//
// Check bundle config caching
//
func loadCheckConfig(id string) *apiclient.CheckBundle {
if !ch.circCfg.CacheConfigs {
return nil
}
if id == "" || ch.circCfg.CacheDir == "" {
return nil
}
path := ch.circCfg.CacheDir
checkConfigFile := filepath.Join(path, strings.ReplaceAll(id, ":", "_")+".json")
data, err := os.ReadFile(checkConfigFile)
if err != nil {
if !os.IsNotExist(err) {
ch.logger.Warnf("unable to read %s: %s", checkConfigFile, err)
}
return nil
}
var bundle apiclient.CheckBundle
if err := json.Unmarshal(data, &bundle); err != nil {
ch.logger.Warnf("parsing check config %s: %s", checkConfigFile, err)
return nil
}
ch.logger.Infof("using cached config: %s - %s", checkConfigFile, bundle.Config[apiclicfg.SubmissionURL])
return &bundle
}
func saveCheckConfig(id string, bundle *apiclient.CheckBundle) {
if !ch.circCfg.CacheConfigs {
return
}
if id == "" || ch.circCfg.CacheDir == "" {
return
}
if bundle == nil {
return
}
path := ch.circCfg.CacheDir
checkConfigFile := filepath.Join(path, strings.ReplaceAll(id, ":", "_")+".json")
data, err := json.Marshal(bundle)
if err != nil {
ch.logger.Warnf("marshal check conf: %s", err)
return
}
if err := os.WriteFile(checkConfigFile, data, 0644); err != nil { //nolint:gosec
ch.logger.Warnf("save check conf %s: %s", checkConfigFile, err)
return
}
}