Skip to content

Commit

Permalink
cache ipns entries to speed things up a little
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jeromy <[email protected]>
  • Loading branch information
whyrusleeping committed Oct 22, 2015
1 parent 795e242 commit b0e2090
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 16 deletions.
11 changes: 8 additions & 3 deletions core/commands/ipns.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ Resolve the value of another name:
}
}

router := n.Routing
var resolver namesys.Resolver
resolver = n.Namesys
if local, _, _ := req.Option("local").Bool(); local {
router = offline.NewOfflineRouter(n.Repo.Datastore(), n.PrivateKey)
offroute := offline.NewOfflineRouter(n.Repo.Datastore(), n.PrivateKey)
resolver = namesys.NewRoutingResolver(offroute)
}

var name string
Expand All @@ -86,7 +88,10 @@ Resolve the value of another name:
depth = namesys.DefaultDepthLimit
}

resolver := namesys.NewRoutingResolver(router)
if !strings.HasPrefix(name, "/ipns/") {
name = "/ipns/" + name
}

output, err := resolver.ResolveN(req.Context(), name, depth)
if err != nil {
res.SetError(err, cmds.ErrNormal)
Expand Down
2 changes: 1 addition & 1 deletion namesys/namesys.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewNameSystem(r routing.IpfsRouting, ds ds.Datastore) NameSystem {
resolvers: map[string]resolver{
"dns": newDNSResolver(),
"proquint": new(ProquintResolver),
"dht": newRoutingResolver(r),
"dht": NewRoutingResolver(r),
},
publishers: map[string]Publisher{
"/ipns/": NewRoutingPublisher(r, ds),
Expand Down
3 changes: 3 additions & 0 deletions namesys/republisher/repub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import (
)

func TestRepublish(t *testing.T) {
// set cache life to zero for testing low-period repubs
namesys.IpnsCacheLife = 0

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand Down
60 changes: 49 additions & 11 deletions namesys/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package namesys

import (
"fmt"
"sync"
"time"

proto "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/gogo/protobuf/proto"
mh "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
Expand All @@ -19,25 +21,49 @@ var log = logging.Logger("namesys")
// routingResolver implements NSResolver for the main IPFS SFS-like naming
type routingResolver struct {
routing routing.IpfsRouting

cache map[string]cacheEntry
cachelock sync.Mutex
}

// NewRoutingResolver constructs a name resolver using the IPFS Routing system
// to implement SFS-like naming on top.
func NewRoutingResolver(route routing.IpfsRouting) Resolver {
if route == nil {
panic("attempt to create resolver with nil routing system")
func (r *routingResolver) cacheGet(name string) (path.Path, bool) {
r.cachelock.Lock()
entry, ok := r.cache[name]
r.cachelock.Unlock()
if ok && time.Now().Sub(entry.recvd) < IpnsCacheLife {
return entry.val, true
}

return "", false
}

func (r *routingResolver) cacheSet(name string, val path.Path) {
r.cachelock.Lock()
r.cache[name] = cacheEntry{
val: val,
recvd: time.Now(),
}
r.cachelock.Unlock()
}

return &routingResolver{routing: route}
var IpnsCacheLife = time.Minute

type cacheEntry struct {
val path.Path
recvd time.Time
}

// newRoutingResolver returns a resolver instead of a Resolver.
func newRoutingResolver(route routing.IpfsRouting) resolver {
// NewRoutingResolver constructs a name resolver using the IPFS Routing system
// to implement SFS-like naming on top.
func NewRoutingResolver(route routing.IpfsRouting) *routingResolver {
if route == nil {
panic("attempt to create resolver with nil routing system")
}

return &routingResolver{routing: route}
return &routingResolver{
routing: route,
cache: make(map[string]cacheEntry),
}
}

// Resolve implements Resolver.
Expand All @@ -54,6 +80,11 @@ func (r *routingResolver) ResolveN(ctx context.Context, name string, depth int)
// resolve SFS-like names.
func (r *routingResolver) resolveOnce(ctx context.Context, name string) (path.Path, error) {
log.Debugf("RoutingResolve: '%s'", name)
cached, ok := r.cacheGet(name)
if ok {
return cached, nil
}

hash, err := mh.FromB58String(name)
if err != nil {
log.Warning("RoutingResolve: bad input hash: [%s]\n", name)
Expand Down Expand Up @@ -98,10 +129,17 @@ func (r *routingResolver) resolveOnce(ctx context.Context, name string) (path.Pa
valh, err := mh.Cast(entry.GetValue())
if err != nil {
// Not a multihash, probably a new record
return path.ParsePath(string(entry.GetValue()))
p, err := path.ParsePath(string(entry.GetValue()))
if err != nil {
return "", err
}
r.cacheSet(name, p)
return p, nil
} else {
// Its an old style multihash record
log.Warning("Detected old style multihash record")
return path.FromKey(key.Key(valh)), nil
p := path.FromKey(key.Key(valh))
r.cacheSet(name, p)
return p, nil
}
}
2 changes: 1 addition & 1 deletion routing/dht/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (dht *IpfsDHT) GetValue(ctx context.Context, key key.Key) ([]byte, error) {
ctx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()

vals, err := dht.GetValues(ctx, key, 16)
vals, err := dht.GetValues(ctx, key, (KValue/2)+1)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit b0e2090

Please sign in to comment.