-
Notifications
You must be signed in to change notification settings - Fork 3
/
cache_store.go
172 lines (148 loc) · 3.46 KB
/
cache_store.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package mtasts
import (
"encoding/json"
"io/ioutil"
"net"
"os"
"path/filepath"
"sync"
"time"
)
type fsStore struct {
Dir string
}
func (s fsStore) List() ([]string, error) {
info, err := ioutil.ReadDir(s.Dir)
if err != nil {
return nil, err
}
domains := make([]string, 0, len(info))
for _, ent := range info {
if ent.IsDir() {
continue
}
domains = append(domains, ent.Name())
}
return domains, nil
}
func (s fsStore) Store(domain, id string, fetchTime time.Time, p *Policy) error {
path := filepath.Join(s.Dir, domain)
f, err := os.Create(path + ".tmp")
if err != nil {
return err
}
defer f.Close()
err = json.NewEncoder(f).Encode(map[string]interface{}{
"ID": id,
"FetchTime": fetchTime,
"Policy": p,
})
if err != nil {
return err
}
return os.Rename(f.Name(), path)
}
func (s fsStore) Load(domain string) (id string, fetchTime time.Time, p *Policy, err error) {
f, err := os.Open(filepath.Join(s.Dir, domain))
if err != nil {
if os.IsNotExist(err) {
return "", time.Time{}, nil, ErrNoPolicy
}
return "", time.Time{}, nil, err
}
defer f.Close()
data := struct {
ID string
FetchTime time.Time
Policy *Policy
}{}
if err := json.NewDecoder(f).Decode(&data); err != nil {
return "", time.Time{}, nil, err
}
return data.ID, data.FetchTime, data.Policy, nil
}
// NewFSCache creates the Cache object using FS directory to store cached
// policies.
//
// The specified directory should exist and be writtable.
func NewFSCache(directory string) *Cache {
return &Cache{
Store: fsStore{Dir: directory},
Resolver: net.DefaultResolver,
}
}
type ramStore struct {
lock sync.RWMutex
m map[string]struct {
id string
fetchtime time.Time
policy *Policy
}
}
func (s *ramStore) List() ([]string, error) {
s.lock.RLock()
defer s.lock.RUnlock()
keys := make([]string, 0, len(s.m))
for k := range s.m {
keys = append(keys, k)
}
return keys, nil
}
func (s *ramStore) Store(key string, id string, fetchTime time.Time, policy *Policy) error {
s.lock.Lock()
defer s.lock.Unlock()
s.m[key] = struct {
id string
fetchtime time.Time
policy *Policy
}{
id, fetchTime, policy,
}
return nil
}
func (s *ramStore) Load(key string) (id string, fetchTime time.Time, policy *Policy, err error) {
s.lock.RLock()
defer s.lock.RUnlock()
data, ok := s.m[key]
if !ok {
return "", time.Time{}, nil, ErrNoPolicy
}
return data.id, data.fetchtime, data.policy, nil
}
func newRAMStore() *ramStore {
return &ramStore{m: make(map[string]struct {
id string
fetchtime time.Time
policy *Policy
})}
}
// NewRAMCache creates the Cache object using RAM map to store cached policies.
//
// The underlying Store implementation is goroutine-safe.
func NewRAMCache() *Cache {
return &Cache{
Store: newRAMStore(),
Resolver: net.DefaultResolver,
}
}
type nopStore struct{}
func (nopStore) List() ([]string, error) {
return nil, nil
}
func (nopStore) Store(key string, id string, fetchTime time.Time, policy *Policy) error {
return nil
}
func (nopStore) Load(key string) (id string, fetchTime time.Time, policy *Policy, err error) {
return "", time.Time{}, nil, ErrNoPolicy
}
// NewNopCache creates the Cache object that never stores fetched policies and
// always repeats the lookup.
//
// It should be used only for tests, caching is criticial for the MTA-STS
// security model.
func NewNopCache() *Cache {
return &Cache{
Store: nopStore{},
Resolver: net.DefaultResolver,
}
}