Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1a6c45e

Browse files
committedOct 26, 2015
Add nocache option
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
1 parent 0c59c63 commit 1a6c45e

File tree

2 files changed

+44
-12
lines changed

2 files changed

+44
-12
lines changed
 

‎core/commands/ipns.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Resolve the value of another name:
4545
},
4646
Options: []cmds.Option{
4747
cmds.BoolOption("recursive", "r", "Resolve until the result is not an IPNS name"),
48+
cmds.BoolOption("nocache", "n", "Do not used cached entries"),
4849
},
4950
Run: func(req cmds.Request, res cmds.Response) {
5051

@@ -62,15 +63,27 @@ Resolve the value of another name:
6263
}
6364
}
6465

65-
var resolver namesys.Resolver
66-
resolver = n.Namesys
67-
if local, _, _ := req.Option("local").Bool(); local {
66+
nocache, _, _ := req.Option("nocache").Bool()
67+
local, _, _ := req.Option("local").Bool()
68+
69+
// default to nodes namesys resolver
70+
var resolver namesys.Resolver = n.Namesys
71+
72+
if local && nocache {
73+
res.SetError(errors.New("cannot specify both local and nocache"), cmds.ErrNormal)
74+
return
75+
}
76+
77+
if local {
6878
offroute := offline.NewOfflineRouter(n.Repo.Datastore(), n.PrivateKey)
6979
resolver = namesys.NewRoutingResolver(offroute, 0)
7080
}
7181

72-
var name string
82+
if nocache {
83+
resolver = namesys.NewNameSystem(n.Routing, n.Repo.Datastore(), 0)
84+
}
7385

86+
var name string
7487
if len(req.Arguments()) == 0 {
7588
if n.Identity == "" {
7689
res.SetError(errors.New("Identity not loaded!"), cmds.ErrNormal)

‎namesys/routing.go

+27-8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
pb "github.com/ipfs/go-ipfs/namesys/pb"
1414
path "github.com/ipfs/go-ipfs/path"
1515
routing "github.com/ipfs/go-ipfs/routing"
16+
u "github.com/ipfs/go-ipfs/util"
1617
logging "github.com/ipfs/go-ipfs/vendor/QmXJkcEXB6C9h6Ytb6rrUTFU56Ro62zxgrbxTT3dgjQGA8/go-log"
1718
)
1819

@@ -31,25 +32,25 @@ func (r *routingResolver) cacheGet(name string) (path.Path, bool) {
3132
r.cachelock.Lock()
3233
entry, ok := r.cache[name]
3334
r.cachelock.Unlock()
34-
if ok && time.Now().Sub(entry.recvd) < r.cachelife {
35+
if ok && time.Now().Before(entry.eol) {
3536
return entry.val, true
3637
}
3738

3839
return "", false
3940
}
4041

41-
func (r *routingResolver) cacheSet(name string, val path.Path) {
42+
func (r *routingResolver) cacheSet(name string, val path.Path, eol time.Time) {
4243
r.cachelock.Lock()
4344
r.cache[name] = cacheEntry{
44-
val: val,
45-
recvd: time.Now(),
45+
val: val,
46+
eol: eol,
4647
}
4748
r.cachelock.Unlock()
4849
}
4950

5051
type cacheEntry struct {
51-
val path.Path
52-
recvd time.Time
52+
val path.Path
53+
eol time.Time
5354
}
5455

5556
// NewRoutingResolver constructs a name resolver using the IPFS Routing system
@@ -133,13 +134,31 @@ func (r *routingResolver) resolveOnce(ctx context.Context, name string) (path.Pa
133134
if err != nil {
134135
return "", err
135136
}
136-
r.cacheSet(name, p)
137+
138+
cacheTil := time.Now().Add(r.cachelife)
139+
eol, ok := checkEOL(entry)
140+
if ok && eol.Before(cacheTil) {
141+
cacheTil = eol
142+
}
143+
144+
r.cacheSet(name, p, cacheTil)
137145
return p, nil
138146
} else {
139147
// Its an old style multihash record
140148
log.Warning("Detected old style multihash record")
141149
p := path.FromKey(key.Key(valh))
142-
r.cacheSet(name, p)
150+
r.cacheSet(name, p, time.Now().Add(r.cachelife))
143151
return p, nil
144152
}
145153
}
154+
155+
func checkEOL(e *pb.IpnsEntry) (time.Time, bool) {
156+
if e.GetValidityType() == pb.IpnsEntry_EOL {
157+
eol, err := u.ParseRFC3339(string(e.GetValidity()))
158+
if err != nil {
159+
return time.Time{}, false
160+
}
161+
return eol, true
162+
}
163+
return time.Time{}, false
164+
}

0 commit comments

Comments
 (0)
Please sign in to comment.