-
Notifications
You must be signed in to change notification settings - Fork 6
/
cache.go
108 lines (97 loc) · 2.73 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
package fronted
import (
"encoding/json"
"os"
"path/filepath"
"time"
)
func (d *fronted) initCaching(cacheFile string) {
d.prepopulateMasquerades(cacheFile)
go d.maintainCache(cacheFile)
}
func (d *fronted) prepopulateMasquerades(cacheFile string) {
bytes, err := os.ReadFile(cacheFile)
if err != nil {
// This is not a big deal since we'll just fill the cache later
log.Debugf("ignorable error: Unable to read cache file for prepopulation: %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 cachedMasquerades []*masquerade
if err := json.Unmarshal(bytes, &cachedMasquerades); err != nil {
log.Errorf("Error reading cached masquerades: %v", err)
return
}
log.Debugf("Cache contained %d masquerades", len(cachedMasquerades))
now := time.Now()
// update last succeeded status of masquerades based on cached values
for _, m := range d.masquerades {
for _, cm := range cachedMasquerades {
sameMasquerade := cm.ProviderID == m.getProviderID() && cm.Domain == m.getDomain() && cm.IpAddress == m.getIpAddress()
cachedValueFresh := now.Sub(m.lastSucceeded()) < d.maxAllowedCachedAge
if sameMasquerade && cachedValueFresh {
m.setLastSucceeded(cm.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.masquerades.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)
}
}
}
func (d *fronted) closeCache() {
d.closeCacheOnce.Do(func() {
close(d.cacheClosed)
})
}