-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathresponsecache.go
107 lines (89 loc) · 3.44 KB
/
responsecache.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
package responsecache
import (
"context"
"sync"
blocks "github.com/ipfs/go-block-format"
logging "github.com/ipfs/go-log/v2"
"github.com/ipld/go-ipld-prime"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"github.com/ipfs/go-graphsync"
"github.com/ipfs/go-graphsync/linktracker"
"github.com/ipfs/go-graphsync/metadata"
)
var log = logging.Logger("graphsync")
// UnverifiedBlockStore is an interface for storing blocks
// as they come in and removing them as they are verified
type UnverifiedBlockStore interface {
PruneBlocks(func(ipld.Link, uint64) bool)
PruneBlock(ipld.Link)
VerifyBlock(ipld.Link, ipld.LinkContext) ([]byte, error)
AddUnverifiedBlock(trace.Link, ipld.Link, []byte)
}
// ResponseCache maintains a store of unverified blocks and response
// data about links for loading, and prunes blocks as needed.
type ResponseCache struct {
responseCacheLk sync.RWMutex
linkTracker *linktracker.LinkTracker
unverifiedBlockStore UnverifiedBlockStore
}
// New initializes a new ResponseCache using the given unverified block store.
func New(unverifiedBlockStore UnverifiedBlockStore) *ResponseCache {
return &ResponseCache{
linkTracker: linktracker.New(),
unverifiedBlockStore: unverifiedBlockStore,
}
}
// FinishRequest indicate there is no more need to track blocks tied to this
// response. It returns the total number of bytes in blocks that were being
// tracked but are no longer in memory
func (rc *ResponseCache) FinishRequest(requestID graphsync.RequestID) {
rc.responseCacheLk.Lock()
rc.linkTracker.FinishRequest(requestID)
rc.unverifiedBlockStore.PruneBlocks(func(link ipld.Link, amt uint64) bool {
return rc.linkTracker.BlockRefCount(link) == 0
})
rc.responseCacheLk.Unlock()
}
// AttemptLoad attempts to laod the given block from the cache
func (rc *ResponseCache) AttemptLoad(requestID graphsync.RequestID, link ipld.Link, linkContext ipld.LinkContext) ([]byte, error) {
rc.responseCacheLk.Lock()
defer rc.responseCacheLk.Unlock()
if rc.linkTracker.IsKnownMissingLink(requestID, link) {
return nil, graphsync.RemoteMissingBlockErr{Link: link}
}
data, _ := rc.unverifiedBlockStore.VerifyBlock(link, linkContext)
return data, nil
}
// ProcessResponse processes incoming response data, adding unverified blocks,
// and tracking link metadata from a remote peer
func (rc *ResponseCache) ProcessResponse(
ctx context.Context,
responses map[graphsync.RequestID]metadata.Metadata,
blks []blocks.Block) {
ctx, span := otel.Tracer("graphsync").Start(ctx, "cacheProcess", trace.WithAttributes(
attribute.Int("blockCount", len(blks)),
))
traceLink := trace.LinkFromContext(ctx)
defer span.End()
rc.responseCacheLk.Lock()
for _, block := range blks {
log.Debugf("Received block from network: %s", block.Cid().String())
rc.unverifiedBlockStore.AddUnverifiedBlock(traceLink, cidlink.Link{Cid: block.Cid()}, block.RawData())
}
for requestID, md := range responses {
for _, item := range md {
log.Debugf("Traverse link %s on request ID %d", item.Link.String(), requestID)
rc.linkTracker.RecordLinkTraversal(requestID, cidlink.Link{Cid: item.Link}, item.BlockPresent)
}
}
// prune unused blocks right away
for _, block := range blks {
if rc.linkTracker.BlockRefCount(cidlink.Link{Cid: block.Cid()}) == 0 {
rc.unverifiedBlockStore.PruneBlock(cidlink.Link{Cid: block.Cid()})
}
}
rc.responseCacheLk.Unlock()
}