+25
-25
atproto/identity/cache_directory.go
+25
-25
atproto/identity/cache_directory.go
···
16
16
Inner Directory
17
17
ErrTTL time.Duration
18
18
InvalidHandleTTL time.Duration
19
-
handleCache *expirable.LRU[syntax.Handle, HandleEntry]
20
-
identityCache *expirable.LRU[syntax.DID, IdentityEntry]
19
+
handleCache *expirable.LRU[syntax.Handle, handleEntry]
20
+
identityCache *expirable.LRU[syntax.DID, identityEntry]
21
21
didLookupChans sync.Map
22
22
handleLookupChans sync.Map
23
23
}
24
24
25
-
type HandleEntry struct {
25
+
type handleEntry struct {
26
26
Updated time.Time
27
27
DID syntax.DID
28
28
Err error
29
29
}
30
30
31
-
type IdentityEntry struct {
31
+
type identityEntry struct {
32
32
Updated time.Time
33
33
Identity *Identity
34
34
Err error
···
42
42
ErrTTL: errTTL,
43
43
InvalidHandleTTL: invalidHandleTTL,
44
44
Inner: inner,
45
-
handleCache: expirable.NewLRU[syntax.Handle, HandleEntry](capacity, nil, hitTTL),
46
-
identityCache: expirable.NewLRU[syntax.DID, IdentityEntry](capacity, nil, hitTTL),
45
+
handleCache: expirable.NewLRU[syntax.Handle, handleEntry](capacity, nil, hitTTL),
46
+
identityCache: expirable.NewLRU[syntax.DID, identityEntry](capacity, nil, hitTTL),
47
47
}
48
48
}
49
49
50
-
func (d *CacheDirectory) IsHandleStale(e *HandleEntry) bool {
50
+
func (d *CacheDirectory) isHandleStale(e *handleEntry) bool {
51
51
if e.Err != nil && time.Since(e.Updated) > d.ErrTTL {
52
52
return true
53
53
}
54
54
return false
55
55
}
56
56
57
-
func (d *CacheDirectory) IsIdentityStale(e *IdentityEntry) bool {
57
+
func (d *CacheDirectory) isIdentityStale(e *identityEntry) bool {
58
58
if e.Err != nil && time.Since(e.Updated) > d.ErrTTL {
59
59
return true
60
60
}
···
64
64
return false
65
65
}
66
66
67
-
func (d *CacheDirectory) updateHandle(ctx context.Context, h syntax.Handle) HandleEntry {
67
+
func (d *CacheDirectory) updateHandle(ctx context.Context, h syntax.Handle) handleEntry {
68
68
ident, err := d.Inner.LookupHandle(ctx, h)
69
69
if err != nil {
70
-
he := HandleEntry{
70
+
he := handleEntry{
71
71
Updated: time.Now(),
72
72
DID: "",
73
73
Err: err,
···
76
76
return he
77
77
}
78
78
79
-
entry := IdentityEntry{
79
+
entry := identityEntry{
80
80
Updated: time.Now(),
81
81
Identity: ident,
82
82
Err: nil,
83
83
}
84
-
he := HandleEntry{
84
+
he := handleEntry{
85
85
Updated: time.Now(),
86
86
DID: ident.DID,
87
87
Err: nil,
···
98
98
}
99
99
start := time.Now()
100
100
entry, ok := d.handleCache.Get(h)
101
-
if ok && !d.IsHandleStale(&entry) {
101
+
if ok && !d.isHandleStale(&entry) {
102
102
handleCacheHits.Inc()
103
103
handleResolution.WithLabelValues("lru", "cached").Inc()
104
104
handleResolutionDuration.WithLabelValues("lru", "cached").Observe(time.Since(start).Seconds())
···
118
118
case <-val.(chan struct{}):
119
119
// The result should now be in the cache
120
120
entry, ok := d.handleCache.Get(h)
121
-
if ok && !d.IsHandleStale(&entry) {
121
+
if ok && !d.isHandleStale(&entry) {
122
122
return entry.DID, entry.Err
123
123
}
124
124
return "", fmt.Errorf("identity not found in cache after coalesce returned")
···
148
148
return "", fmt.Errorf("unexpected control-flow error")
149
149
}
150
150
151
-
func (d *CacheDirectory) updateDID(ctx context.Context, did syntax.DID) IdentityEntry {
151
+
func (d *CacheDirectory) updateDID(ctx context.Context, did syntax.DID) identityEntry {
152
152
ident, err := d.Inner.LookupDID(ctx, did)
153
153
// persist the identity lookup error, instead of processing it immediately
154
-
entry := IdentityEntry{
154
+
entry := identityEntry{
155
155
Updated: time.Now(),
156
156
Identity: ident,
157
157
Err: err,
158
158
}
159
-
var he *HandleEntry
159
+
var he *handleEntry
160
160
// if *not* an error, then also update the handle cache
161
161
if nil == err && !ident.Handle.IsInvalidHandle() {
162
-
he = &HandleEntry{
162
+
he = &handleEntry{
163
163
Updated: time.Now(),
164
164
DID: did,
165
165
Err: nil,
···
174
174
}
175
175
176
176
func (d *CacheDirectory) LookupDID(ctx context.Context, did syntax.DID) (*Identity, error) {
177
-
id, _, err := d.LookupDIDWithCacheState(ctx, did)
177
+
id, _, err := d.lookupDIDWithCacheState(ctx, did)
178
178
return id, err
179
179
}
180
180
181
-
func (d *CacheDirectory) LookupDIDWithCacheState(ctx context.Context, did syntax.DID) (*Identity, bool, error) {
181
+
func (d *CacheDirectory) lookupDIDWithCacheState(ctx context.Context, did syntax.DID) (*Identity, bool, error) {
182
182
start := time.Now()
183
183
entry, ok := d.identityCache.Get(did)
184
-
if ok && !d.IsIdentityStale(&entry) {
184
+
if ok && !d.isIdentityStale(&entry) {
185
185
identityCacheHits.Inc()
186
186
didResolution.WithLabelValues("lru", "cached").Inc()
187
187
didResolutionDuration.WithLabelValues("lru", "cached").Observe(time.Since(start).Seconds())
···
201
201
case <-val.(chan struct{}):
202
202
// The result should now be in the cache
203
203
entry, ok := d.identityCache.Get(did)
204
-
if ok && !d.IsIdentityStale(&entry) {
204
+
if ok && !d.isIdentityStale(&entry) {
205
205
return entry.Identity, false, entry.Err
206
206
}
207
207
return nil, false, fmt.Errorf("identity not found in cache after coalesce returned")
···
232
232
}
233
233
234
234
func (d *CacheDirectory) LookupHandle(ctx context.Context, h syntax.Handle) (*Identity, error) {
235
-
ident, _, err := d.LookupHandleWithCacheState(ctx, h)
235
+
ident, _, err := d.lookupHandleWithCacheState(ctx, h)
236
236
return ident, err
237
237
}
238
238
239
-
func (d *CacheDirectory) LookupHandleWithCacheState(ctx context.Context, h syntax.Handle) (*Identity, bool, error) {
239
+
func (d *CacheDirectory) lookupHandleWithCacheState(ctx context.Context, h syntax.Handle) (*Identity, bool, error) {
240
240
h = h.Normalize()
241
241
did, err := d.ResolveHandle(ctx, h)
242
242
if err != nil {
243
243
return nil, false, err
244
244
}
245
-
ident, hit, err := d.LookupDIDWithCacheState(ctx, did)
245
+
ident, hit, err := d.lookupDIDWithCacheState(ctx, did)
246
246
if err != nil {
247
247
return nil, hit, err
248
248
}
+5
-5
atproto/identity/redisdir/redis_directory.go
+5
-5
atproto/identity/redisdir/redis_directory.go
···
273
273
}
274
274
275
275
func (d *RedisDirectory) LookupDID(ctx context.Context, did syntax.DID) (*identity.Identity, error) {
276
-
id, _, err := d.LookupDIDWithCacheState(ctx, did)
276
+
id, _, err := d.lookupDIDWithCacheState(ctx, did)
277
277
return id, err
278
278
}
279
279
280
-
func (d *RedisDirectory) LookupDIDWithCacheState(ctx context.Context, did syntax.DID) (*identity.Identity, bool, error) {
280
+
func (d *RedisDirectory) lookupDIDWithCacheState(ctx context.Context, did syntax.DID) (*identity.Identity, bool, error) {
281
281
start := time.Now()
282
282
var entry identityEntry
283
283
err := d.identityCache.Get(ctx, redisDirPrefix+did.String(), &entry)
···
340
340
}
341
341
342
342
func (d *RedisDirectory) LookupHandle(ctx context.Context, h syntax.Handle) (*identity.Identity, error) {
343
-
ident, _, err := d.LookupHandleWithCacheState(ctx, h)
343
+
ident, _, err := d.lookupHandleWithCacheState(ctx, h)
344
344
return ident, err
345
345
}
346
346
347
-
func (d *RedisDirectory) LookupHandleWithCacheState(ctx context.Context, h syntax.Handle) (*identity.Identity, bool, error) {
347
+
func (d *RedisDirectory) lookupHandleWithCacheState(ctx context.Context, h syntax.Handle) (*identity.Identity, bool, error) {
348
348
h = h.Normalize()
349
349
did, err := d.ResolveHandle(ctx, h)
350
350
if err != nil {
351
351
return nil, false, err
352
352
}
353
-
ident, hit, err := d.LookupDIDWithCacheState(ctx, did)
353
+
ident, hit, err := d.lookupDIDWithCacheState(ctx, did)
354
354
if err != nil {
355
355
return nil, hit, err
356
356
}