-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.go
More file actions
96 lines (85 loc) · 2.61 KB
/
Copy pathcache.go
File metadata and controls
96 lines (85 loc) · 2.61 KB
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
package domainfront
import (
"encoding/json"
"time"
)
// CachedFront is the serialization format for cached fronts.
type CachedFront struct {
Domain string `json:"Domain"`
IpAddress string `json:"IpAddress"`
SNI string `json:"SNI,omitempty"`
VerifyHostname *string `json:"VerifyHostname,omitempty"`
LastSucceeded time.Time `json:"LastSucceeded"`
ProviderID string `json:"ProviderID"`
}
// Cache is an interface for persisting and loading front state.
type Cache interface {
Load() ([]*CachedFront, error)
Save(fronts []*CachedFront) error
}
// FileCache persists fronts as JSON to a file.
type FileCache struct {
Path string
}
func (fc *FileCache) Load() ([]*CachedFront, error) {
data, err := readFile(fc.Path)
if err != nil || len(data) == 0 {
return nil, err
}
var fronts []*CachedFront
if err := json.Unmarshal(data, &fronts); err != nil {
return nil, err
}
return fronts, nil
}
func (fc *FileCache) Save(fronts []*CachedFront) error {
data, err := json.Marshal(fronts)
if err != nil {
return err
}
return writeFile(fc.Path, data)
}
// NopCache is a no-op cache that never loads or saves anything.
type NopCache struct{}
func (NopCache) Load() ([]*CachedFront, error) { return nil, nil }
func (NopCache) Save([]*CachedFront) error { return nil }
// frontsToCache converts pool fronts to cache format, limited to maxSize entries.
// Only fronts that have previously succeeded are cached — this avoids allocating
// CachedFront structs for the (typically much larger) set of untested fronts.
func frontsToCache(fronts []*front, maxSize int) []*CachedFront {
cached := make([]*CachedFront, 0, min(len(fronts), maxSize))
for _, f := range fronts {
ts := f.lastSucceededTime()
if ts.IsZero() {
continue // never succeeded, skip
}
if len(cached) >= maxSize {
break
}
cached = append(cached, &CachedFront{
Domain: f.Domain,
IpAddress: f.IpAddress,
SNI: f.SNI,
VerifyHostname: f.VerifyHostname,
LastSucceeded: ts,
ProviderID: f.ProviderID,
})
}
return cached
}
// applyCachedState updates front LastSucceeded from cached values.
func applyCachedState(fronts []*front, cached []*CachedFront, maxAge time.Duration) {
type key struct{ provider, domain, ip string }
lookup := make(map[key]*CachedFront, len(cached))
for _, cf := range cached {
lookup[key{cf.ProviderID, cf.Domain, cf.IpAddress}] = cf
}
now := time.Now()
for _, f := range fronts {
if cf, ok := lookup[key{f.ProviderID, f.Domain, f.IpAddress}]; ok {
if now.Sub(cf.LastSucceeded) < maxAge {
f.setLastSucceeded(cf.LastSucceeded)
}
}
}
}