@cryptotaxi247 / kubo / commits / 1f820dc23

path/resolver.go: Handle timeout here

License: MIT Signed-off-by: Mildred Ki'Lya <mildred-pub.git@mildred.fr>

Mildred Ki'Lya committed Oct 26, 2015 at 22:47 UTC 1f820dc2356275b521561622bc649e541a53a532
2 files changed +13 -8
merkledag/node.go
+6 -7
@@ -2,7 +2,6 @@ package merkledag
2
3 import (
4 "fmt"
5 - "time"
5
6 "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
7
@@ -77,6 +76,11 @@ func MakeLink(n *Node) (*Link, error) {
76 }, nil
77 }
78
79 +// GetCachedNode returns the MDAG Node that was cached, or nil
80 +func (l *Link) GetCachedNode() *Node {
81 + return l.Node
82 +}
83 +
84 // GetNode returns the MDAG Node that this link points to
85 func (l *Link) GetNode(ctx context.Context, serv DAGService) (*Node, error) {
86 if l.Node != nil {
@@ -89,13 +93,8 @@ func (l *Link) GetNode(ctx context.Context, serv DAGService) (*Node, error) {
93 // GetNodeAndCache return the MDAG Node that the link points to and store a
94 // pointer to that node along with the link to speed up further retrivals. A
95 // timeout is to be specified to avoid taking too much time.
92 -func (l *Link) GetNodeAndCache(ctx context.Context, serv DAGService, timeout time.Duration) (*Node, error) {
96 +func (l *Link) GetNodeAndCache(ctx context.Context, serv DAGService) (*Node, error) {
97 if l.Node == nil {
94 - if timeout != 0 {
95 - var cancel context.CancelFunc
96 - ctx, cancel = context.WithTimeout(ctx, time.Minute)
97 - defer cancel()
98 - }
98 nd, err := serv.Get(ctx, key.Key(l.Hash))
99 if err != nil {
100 return nil, err
path/resolver.go
+7 -1
@@ -125,8 +125,14 @@ func (s *Resolver) ResolveLinks(ctx context.Context, ndd *merkledag.Node, names
125 return result, ErrNoLink{Name: name, Node: n}
126 }
127
128 + if nlink.GetCachedNode() == nil {
129 + var cancel context.CancelFunc
130 + ctx, cancel = context.WithTimeout(ctx, time.Minute)
131 + defer cancel()
132 + }
133 +
134 var err error
129 - nd, err = nlink.GetNodeAndCache(ctx, s.DAG, time.Minute)
135 + nd, err = nlink.GetNodeAndCache(ctx, s.DAG)
136 if err != nil {
137 return append(result, nd), err
138 }