this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add methods for querying a caching directory and getting back hit status (#508)

authored by

Jaz and committed by
GitHub
33491ca7 83b8f8c7

+23 -13
+23 -13
atproto/identity/cache_directory.go
··· 188 188 } 189 189 190 190 func (d *CacheDirectory) LookupDID(ctx context.Context, did syntax.DID) (*Identity, error) { 191 + id, _, err := d.LookupDIDWithCacheState(ctx, did) 192 + return id, err 193 + } 194 + 195 + func (d *CacheDirectory) LookupDIDWithCacheState(ctx context.Context, did syntax.DID) (*Identity, bool, error) { 191 196 entry, ok := d.identityCache.Get(did) 192 197 if ok && !d.IsIdentityStale(&entry) { 193 198 identityCacheHits.Inc() 194 - return entry.Identity, entry.Err 199 + return entry.Identity, true, entry.Err 195 200 } 196 201 identityCacheMisses.Inc() 197 202 ··· 206 211 // The result should now be in the cache 207 212 entry, ok := d.identityCache.Get(did) 208 213 if ok && !d.IsIdentityStale(&entry) { 209 - return entry.Identity, entry.Err 214 + return entry.Identity, false, entry.Err 210 215 } 211 - return nil, fmt.Errorf("identity not found in cache after coalesce returned") 216 + return nil, false, fmt.Errorf("identity not found in cache after coalesce returned") 212 217 case <-ctx.Done(): 213 - return nil, ctx.Err() 218 + return nil, false, ctx.Err() 214 219 } 215 220 } 216 221 ··· 223 228 close(res) 224 229 225 230 if newEntry.Err != nil { 226 - return nil, newEntry.Err 231 + return nil, false, newEntry.Err 227 232 } 228 233 if newEntry.Identity != nil { 229 - return newEntry.Identity, nil 234 + return newEntry.Identity, false, nil 230 235 } 231 - return nil, fmt.Errorf("unexpected control-flow error") 236 + return nil, false, fmt.Errorf("unexpected control-flow error") 232 237 } 233 238 234 239 func (d *CacheDirectory) LookupHandle(ctx context.Context, h syntax.Handle) (*Identity, error) { 240 + ident, _, err := d.LookupHandleWithCacheState(ctx, h) 241 + return ident, err 242 + } 243 + 244 + func (d *CacheDirectory) LookupHandleWithCacheState(ctx context.Context, h syntax.Handle) (*Identity, bool, error) { 235 245 h = h.Normalize() 236 246 did, err := d.ResolveHandle(ctx, h) 237 247 if err != nil { 238 - return nil, err 248 + return nil, false, err 239 249 } 240 - ident, err := d.LookupDID(ctx, did) 250 + ident, hit, err := d.LookupDIDWithCacheState(ctx, did) 241 251 if err != nil { 242 - return nil, err 252 + return nil, hit, err 243 253 } 244 254 245 255 declared, err := ident.DeclaredHandle() 246 256 if err != nil { 247 - return nil, err 257 + return nil, hit, err 248 258 } 249 259 if declared != h { 250 - return nil, ErrHandleMismatch 260 + return nil, hit, ErrHandleMismatch 251 261 } 252 - return ident, nil 262 + return ident, hit, nil 253 263 } 254 264 255 265 func (d *CacheDirectory) Lookup(ctx context.Context, a syntax.AtIdentifier) (*Identity, error) {