-
Notifications
You must be signed in to change notification settings - Fork 7
/
cache.go
109 lines (99 loc) · 2.69 KB
/
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package fronted
import (
"encoding/json"
"os"
"path/filepath"
"time"
)
func (d *fronted) initCaching(cacheFile string) {
d.prepopulateFronts(cacheFile)
go d.maintainCache(cacheFile)
}
func (d *fronted) prepopulateFronts(cacheFile string) {
bytes, err := os.ReadFile(cacheFile)
if os.IsNotExist(err) {
if err = os.MkdirAll(filepath.Dir(cacheFile), 0755); err != nil {
log.Errorf("Error creating cache directory: %v", err)
return
}
}
if err != nil {
log.Errorf("Error reading cache file: %v", err)
return
}
if len(bytes) == 0 {
// This can happen if the file is empty or just not there
log.Debug("ignorable error: Cache file is empty")
return
}
log.Debugf("Attempting to prepopulate masquerades from cache file: %v", cacheFile)
var cachedFronts []*front
if err := json.Unmarshal(bytes, &cachedFronts); err != nil {
log.Errorf("Error reading cached masquerades: %v", err)
return
}
log.Debugf("Cache contained %d masquerades", len(cachedFronts))
now := time.Now()
// update last succeeded status of masquerades based on cached values
for _, f := range d.fronts {
for _, cf := range cachedFronts {
sameFront := cf.ProviderID == f.getProviderID() && cf.Domain == f.getDomain() && cf.IpAddress == f.getIpAddress()
cachedValueFresh := now.Sub(f.lastSucceeded()) < d.maxAllowedCachedAge
if sameFront && cachedValueFresh {
f.setLastSucceeded(cf.LastSucceeded)
}
}
}
}
func (d *fronted) markCacheDirty() {
select {
case d.cacheDirty <- nil:
// okay
default:
// already dirty
}
}
func (d *fronted) maintainCache(cacheFile string) {
for {
select {
case <-d.cacheClosed:
return
case <-time.After(d.cacheSaveInterval):
select {
case <-d.cacheClosed:
return
case <-d.cacheDirty:
d.updateCache(cacheFile)
}
}
}
}
func (d *fronted) updateCache(cacheFile string) {
log.Debugf("Updating cache at %v", cacheFile)
cache := d.fronts.sortedCopy()
sizeToSave := len(cache)
if d.maxCacheSize < sizeToSave {
sizeToSave = d.maxCacheSize
}
b, err := json.Marshal(cache[:sizeToSave])
if err != nil {
log.Errorf("Unable to marshal cache to JSON: %v", err)
return
}
err = os.WriteFile(cacheFile, b, 0644)
if err != nil {
log.Errorf("Unable to save cache to disk: %v", err)
// Log the directory of the cache file and if it exists for debugging purposes
parent := filepath.Dir(cacheFile)
// check if the parent directory exists
if _, err := os.Stat(parent); err == nil {
// parent directory exists
log.Debugf("Parent directory of cache file exists: %v", parent)
} else {
// parent directory does not exist
log.Debugf("Parent directory of cache file does not exist: %v", parent)
}
} else {
log.Debugf("Cache saved to disk")
}
}