forked from keybase/client
/
proof_cache.go
267 lines (230 loc) · 5.87 KB
/
proof_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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package libkb
import (
"sync"
"time"
lru "github.com/hashicorp/golang-lru"
keybase1 "github.com/keybase/client/go/protocol/keybase1"
jsonw "github.com/keybase/go-jsonw"
)
type CheckResult struct {
Contextified
Status ProofError // Or nil if it was a success
Time time.Time // When the last check was
PvlHash string // Added after other fields. Some entries may not have this packed.
}
func (cr CheckResult) Pack() *jsonw.Wrapper {
p := jsonw.NewDictionary()
if cr.Status != nil {
s := jsonw.NewDictionary()
s.SetKey("code", jsonw.NewInt(int(cr.Status.GetProofStatus())))
s.SetKey("desc", jsonw.NewString(cr.Status.GetDesc()))
p.SetKey("status", s)
}
p.SetKey("time", jsonw.NewInt64(cr.Time.Unix()))
p.SetKey("pvlhash", jsonw.NewString(cr.PvlHash))
return p
}
func (cr CheckResult) Freshness() keybase1.CheckResultFreshness {
now := cr.G().Clock().Now()
age := now.Sub(cr.Time)
switch {
case cr.Status == nil:
switch {
case age < cr.G().Env.GetProofCacheMediumDur():
return keybase1.CheckResultFreshness_FRESH
case age < cr.G().Env.GetProofCacheLongDur():
return keybase1.CheckResultFreshness_AGED
}
case ProofErrorIsPvlBad(cr.Status):
// Don't use cache results for pvl problems.
// The hope is that they will soon be resolved server-side.
return keybase1.CheckResultFreshness_RANCID
case !ProofErrorIsSoft(cr.Status):
if age < cr.G().Env.GetProofCacheShortDur() {
return keybase1.CheckResultFreshness_FRESH
}
default:
// don't use cache results for "soft" errors (500s, timeouts)
// see issue #140
}
return keybase1.CheckResultFreshness_RANCID
}
func NewNowCheckResult(g *GlobalContext, pe ProofError) *CheckResult {
return &CheckResult{
Contextified: NewContextified(g),
Status: pe,
Time: g.Clock().Now(),
}
}
func NewCheckResult(g *GlobalContext, jw *jsonw.Wrapper) (res *CheckResult, err error) {
var ignoreErr error
var t int64
var code int
var desc string
var pvlHash string
jw.AtKey("time").GetInt64Void(&t, &err)
jw.AtKey("pvlhash").GetStringVoid(&pvlHash, &ignoreErr)
status := jw.AtKey("status")
var pe ProofError
if !status.IsNil() {
status.AtKey("desc").GetStringVoid(&desc, &err)
status.AtKey("code").GetIntVoid(&code, &err)
pe = NewProofError(keybase1.ProofStatus(code), desc)
}
if err == nil {
res = &CheckResult{
Contextified: NewContextified(g),
Status: pe,
Time: time.Unix(t, 0),
PvlHash: pvlHash,
}
}
return
}
type ProofCache struct {
Contextified
capac int
lru *lru.Cache
sync.RWMutex
noDisk bool
}
func NewProofCache(g *GlobalContext, capac int) *ProofCache {
return &ProofCache{Contextified: NewContextified(g), capac: capac}
}
func (pc *ProofCache) DisableDisk() {
pc.Lock()
defer pc.Unlock()
pc.noDisk = true
}
func (pc *ProofCache) Reset() error {
pc.Lock()
defer pc.Unlock()
return pc.initCache()
}
func (pc *ProofCache) setup() error {
pc.Lock()
defer pc.Unlock()
if pc.lru != nil {
return nil
}
return pc.initCache()
}
func (pc *ProofCache) initCache() error {
lru, err := lru.New(pc.capac)
if err != nil {
return err
}
pc.lru = lru
return nil
}
func (pc *ProofCache) memGet(sid keybase1.SigID) *CheckResult {
if err := pc.setup(); err != nil {
return nil
}
pc.RLock()
defer pc.RUnlock()
tmp, found := pc.lru.Get(sid)
if !found {
return nil
}
cr, ok := tmp.(CheckResult)
if !ok {
pc.G().Log.Errorf("Bad type assertion in ProofCache.Get")
return nil
}
if cr.Freshness() == keybase1.CheckResultFreshness_RANCID {
pc.lru.Remove(sid)
return nil
}
return &cr
}
func (pc *ProofCache) memPut(sid keybase1.SigID, cr CheckResult) {
if err := pc.setup(); err != nil {
return
}
pc.RLock()
defer pc.RUnlock()
pc.lru.Add(sid, cr)
}
func (pc *ProofCache) Get(sid keybase1.SigID, pvlHash PvlKitHash) *CheckResult {
if pc == nil {
return nil
}
cr := pc.memGet(sid)
if cr == nil {
cr = pc.dbGet(sid)
}
if cr == nil {
return nil
}
if cr.PvlHash == "" {
pc.G().Log.Debug("^ ProofCache ignoring entry with pvl-hash empty")
return nil
}
if cr.PvlHash != string(pvlHash) {
pc.G().Log.Debug("^ ProofCache ignoring entry with pvl-hash mismatch")
return nil
}
return cr
}
func (pc *ProofCache) dbKey(sid keybase1.SigID) (DbKey, string) {
sidstr := sid.ToString(true)
key := DbKey{Typ: DBProofCheck, Key: sidstr}
return key, sidstr
}
func (pc *ProofCache) dbGet(sid keybase1.SigID) (cr *CheckResult) {
dbkey, sidstr := pc.dbKey(sid)
pc.G().Log.Debug("+ ProofCache.dbGet(%s)", sidstr)
defer func() {
pc.G().Log.Debug("- ProofCache.dbGet(%s) -> %v", sidstr, (cr != nil))
}()
if pc.noDisk {
pc.G().Log.Debug("| disk proof cache disabled")
return nil
}
jw, err := pc.G().LocalDb.Get(dbkey)
if err != nil {
pc.G().Log.Errorf("Error lookup up proof check in DB: %s", err)
return nil
}
if jw == nil {
pc.G().Log.Debug("| Cached CheckResult for %s wasn't found ", sidstr)
return nil
}
cr, err = NewCheckResult(pc.G(), jw)
if err != nil {
pc.G().Log.Errorf("Bad cached CheckResult for %s", sidstr)
return nil
}
if cr.Freshness() == keybase1.CheckResultFreshness_RANCID {
if err := pc.G().LocalDb.Delete(dbkey); err != nil {
pc.G().Log.Errorf("Delete error: %s", err)
}
pc.G().Log.Debug("| Cached CheckResult for %s wasn't fresh", sidstr)
return nil
}
return cr
}
func (pc *ProofCache) dbPut(sid keybase1.SigID, cr CheckResult) error {
if pc.noDisk {
return nil
}
dbkey, _ := pc.dbKey(sid)
jw := cr.Pack()
return pc.G().LocalDb.Put(dbkey, []DbKey{}, jw)
}
func (pc *ProofCache) Put(sid keybase1.SigID, pe ProofError, pvlHash PvlKitHash) error {
if pc == nil {
return nil
}
cr := CheckResult{
Contextified: pc.Contextified,
Status: pe,
Time: pc.G().Clock().Now(),
PvlHash: string(pvlHash),
}
pc.memPut(sid, cr)
return pc.dbPut(sid, cr)
}