@cryptotaxi247 / kubo / commits / cd7c83a1d

pin: fix a too aggressive refactor and connect some contexts

Michael Muré committed Nov 18, 2019 at 18:26 UTC cd7c83a1d65760161bd6805d5e4ce9cde73178b0
1 file changed +10 -8
pin/pin.go
+10 -8
@@ -281,7 +281,9 @@ func (p *pinner) isInternalPin(c cid.Cid) bool {
281 // IsPinned returns whether or not the given key is pinned
282 // and an explanation of why its pinned
283 func (p *pinner) IsPinned(ctx context.Context, c cid.Cid) (string, bool, error) {
284 - return p.isPinnedWithType(c, Any)
284 + p.lock.RLock()
285 + defer p.lock.RUnlock()
286 + return p.isPinnedWithType(ctx, c, Any)
287 }
288
289 // IsPinnedWithType returns whether or not the given cid is pinned with the
@@ -289,12 +291,12 @@ func (p *pinner) IsPinned(ctx context.Context, c cid.Cid) (string, bool, error)
291 func (p *pinner) IsPinnedWithType(ctx context.Context, c cid.Cid, mode Mode) (string, bool, error) {
292 p.lock.RLock()
293 defer p.lock.RUnlock()
292 - return p.isPinnedWithType(c, mode)
294 + return p.isPinnedWithType(ctx, c, mode)
295 }
296
297 // isPinnedWithType is the implementation of IsPinnedWithType that does not lock.
298 // intended for use by other pinned methods that already take locks
297 -func (p *pinner) isPinnedWithType(c cid.Cid, mode Mode) (string, bool, error) {
299 +func (p *pinner) isPinnedWithType(ctx context.Context, c cid.Cid, mode Mode) (string, bool, error) {
300 switch mode {
301 case Any, Direct, Indirect, Recursive, Internal:
302 default:
@@ -326,7 +328,7 @@ func (p *pinner) isPinnedWithType(c cid.Cid, mode Mode) (string, bool, error) {
328 // Default is Indirect
329 visitedSet := cid.NewSet()
330 for _, rc := range p.recursePin.Keys() {
329 - has, err := hasChild(p.dserv, rc, c, visitedSet.Visit)
331 + has, err := hasChild(ctx, p.dserv, rc, c, visitedSet.Visit)
332 if err != nil {
333 return "", false, err
334 }
@@ -361,7 +363,7 @@ func (p *pinner) CheckIfPinned(ctx context.Context, cids ...cid.Cid) ([]Pinned,
363 // Now walk all recursive pins to check for indirect pins
364 var checkChildren func(cid.Cid, cid.Cid) error
365 checkChildren = func(rk, parentKey cid.Cid) error {
364 - links, err := ipld.GetLinks(context.TODO(), p.dserv, parentKey)
366 + links, err := ipld.GetLinks(ctx, p.dserv, parentKey)
367 if err != nil {
368 return err
369 }
@@ -607,8 +609,8 @@ func (p *pinner) PinWithMode(c cid.Cid, mode Mode) {
609
610 // hasChild recursively looks for a Cid among the children of a root Cid.
611 // The visit function can be used to shortcut already-visited branches.
610 -func hasChild(ng ipld.NodeGetter, root cid.Cid, child cid.Cid, visit func(cid.Cid) bool) (bool, error) {
611 - links, err := ipld.GetLinks(context.TODO(), ng, root)
612 +func hasChild(ctx context.Context, ng ipld.NodeGetter, root cid.Cid, child cid.Cid, visit func(cid.Cid) bool) (bool, error) {
613 + links, err := ipld.GetLinks(ctx, ng, root)
614 if err != nil {
615 return false, err
616 }
@@ -618,7 +620,7 @@ func hasChild(ng ipld.NodeGetter, root cid.Cid, child cid.Cid, visit func(cid.Ci
620 return true, nil
621 }
622 if visit(c) {
621 - has, err := hasChild(ng, c, child, visit)
623 + has, err := hasChild(ctx, ng, c, child, visit)
624 if err != nil {
625 return false, err
626 }