From ae911fd13c0effab15c304855b94d374096c2cac Mon Sep 17 00:00:00 2001 From: Jaz Volpert Date: Fri, 15 Sep 2023 01:05:34 +0000 Subject: [PATCH] Implement a selective cache flush interface for handle updates --- api/plc.go | 4 ++++ bgs/bgs.go | 2 ++ did/multi.go | 17 +++++++++++++++++ did/web.go | 4 ++++ plc/caching.go | 6 +++++- plc/fakedid.go | 4 ++++ 6 files changed, 36 insertions(+), 1 deletion(-) diff --git a/api/plc.go b/api/plc.go index 11beaee27..ac058ec2e 100644 --- a/api/plc.go +++ b/api/plc.go @@ -54,6 +54,10 @@ func (s *PLCServer) GetDocument(ctx context.Context, didstr string) (*did.Docume return &doc, nil } +func (s *PLCServer) FlushCacheFor(did string) { + return +} + type CreateOp struct { Type string `json:"type" cborgen:"type"` SigningKey string `json:"signingKey" cborgen:"signingKey"` diff --git a/bgs/bgs.go b/bgs/bgs.go index 5a2f622f0..4659d0afa 100644 --- a/bgs/bgs.go +++ b/bgs/bgs.go @@ -785,6 +785,8 @@ func (bgs *BGS) handleFedEvent(ctx context.Context, host *models.PDS, env *event return nil case env.RepoHandle != nil: + // Flush any cached DID documents for this user + bgs.didr.FlushCacheFor(env.RepoHandle.Did) // TODO: ignoring the data in the message and just going out to the DID doc act, err := bgs.createExternalUser(ctx, env.RepoHandle.Did) diff --git a/did/multi.go b/did/multi.go index 8d2c1a765..e871ff454 100644 --- a/did/multi.go +++ b/did/multi.go @@ -9,6 +9,7 @@ import ( type Resolver interface { GetDocument(ctx context.Context, didstr string) (*did.Document, error) + FlushCacheFor(did string) } type MultiResolver struct { @@ -25,6 +26,22 @@ func (mr *MultiResolver) AddHandler(method string, res Resolver) { mr.handlers[method] = res } +func (mr *MultiResolver) FlushCacheFor(didstr string) { + pdid, err := did.ParseDID(didstr) + if err != nil { + return + } + + method := pdid.Protocol() + + res, ok := mr.handlers[method] + if !ok { + return + } + + res.FlushCacheFor(didstr) +} + func (mr *MultiResolver) GetDocument(ctx context.Context, didstr string) (*did.Document, error) { pdid, err := did.ParseDID(didstr) if err != nil { diff --git a/did/web.go b/did/web.go index ee3e8cc69..322520fdc 100644 --- a/did/web.go +++ b/did/web.go @@ -63,6 +63,10 @@ var disallowedTlds = map[string]bool{ "internal": true, } +func (wr *WebResolver) FlushCacheFor(did string) { + return +} + func checkValidDidWeb(val string) error { // no ports or ipv6 if strings.Contains(val, ":") { diff --git a/plc/caching.go b/plc/caching.go index 00ec26ed8..e8ccd3b39 100644 --- a/plc/caching.go +++ b/plc/caching.go @@ -4,7 +4,7 @@ import ( "context" "time" - did "github.com/bluesky-social/indigo/did" + "github.com/bluesky-social/indigo/did" lru "github.com/hashicorp/golang-lru" "go.opentelemetry.io/otel" "go.opentelemetry.io/otel/attribute" @@ -34,6 +34,10 @@ func NewCachingDidResolver(res did.Resolver, maxAge time.Duration, size int) *Ca } } +func (r *CachingDidResolver) FlushCacheFor(didstr string) { + r.cache.Remove(didstr) +} + func (r *CachingDidResolver) tryCache(did string) (*did.Document, bool) { v, ok := r.cache.Get(did) if !ok { diff --git a/plc/fakedid.go b/plc/fakedid.go index 221a07cf0..5162ca8a5 100644 --- a/plc/fakedid.go +++ b/plc/fakedid.go @@ -66,6 +66,10 @@ func (fd *FakeDid) GetDocument(ctx context.Context, udid string) (*did.Document, }, nil } +func (fd *FakeDid) FlushCacheFor(did string) { + return +} + func (fd *FakeDid) CreateDID(ctx context.Context, sigkey *did.PrivKey, recovery string, handle string, service string) (string, error) { buf := make([]byte, 8) rand.Read(buf)