Skip to content

Commit

Permalink
Implement a selective cache flush interface for handle updates (#313)
Browse files Browse the repository at this point in the history
When a handle update happens, we check the PLC entry to get the new
handle, but if we've cached the did document for a user, we will have
the old doc and fail to process the handle change.
  • Loading branch information
ericvolp12 authored Sep 15, 2023
2 parents 4f61094 + ae911fd commit b1efee8
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 1 deletion.
4 changes: 4 additions & 0 deletions api/plc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
2 changes: 2 additions & 0 deletions bgs/bgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions did/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

type Resolver interface {
GetDocument(ctx context.Context, didstr string) (*did.Document, error)
FlushCacheFor(did string)
}

type MultiResolver struct {
Expand All @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions did/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ":") {
Expand Down
6 changes: 5 additions & 1 deletion plc/caching.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions plc/fakedid.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b1efee8

Please sign in to comment.