Skip to content

Commit

Permalink
make cache time configurable
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 28, 2015
1 parent 8c5bbd7 commit e371661
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 15 deletions.
2 changes: 1 addition & 1 deletion core/commands/ipns.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ Resolve the value of another name:
resolver = n.Namesys
if local, _, _ := req.Option("local").Bool(); local {
offroute := offline.NewOfflineRouter(n.Repo.Datastore(), n.PrivateKey)
resolver = namesys.NewRoutingResolver(offroute)
resolver = namesys.NewRoutingResolver(offroute, 0)
}

var name string
Expand Down
31 changes: 29 additions & 2 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,13 @@ func (n *IpfsNode) startOnlineServicesWithHost(ctx context.Context, host p2phost
bitswapNetwork := bsnet.NewFromIpfsHost(n.PeerHost, n.Routing)
n.Exchange = bitswap.New(ctx, n.Identity, bitswapNetwork, n.Blockstore, alwaysSendToPeer)

cachelife, err := n.getCacheLifetime()
if err != nil {
return err
}

// setup name system
n.Namesys = namesys.NewNameSystem(n.Routing, n.Repo.Datastore())
n.Namesys = namesys.NewNameSystem(n.Routing, n.Repo.Datastore(), cachelife)

// setup ipns republishing
err = n.setupIpnsRepublisher()
Expand All @@ -238,6 +243,23 @@ func (n *IpfsNode) startOnlineServicesWithHost(ctx context.Context, host p2phost
return nil
}

func (n *IpfsNode) getCacheLifetime() (time.Duration, error) {
cfg, err := n.Repo.Config()
if err != nil {
return 0, err
}

ct := cfg.Ipns.ResolveCacheTime
if ct == "" {
return namesys.DefaultResolverCacheLife, nil
}
d, err := time.ParseDuration(ct)
if err != nil {
return 0, fmt.Errorf("error parsing cache life from Ipns.ResolveCacheTime: %s", err)
}
return d, nil
}

func (n *IpfsNode) setupIpnsRepublisher() error {
cfg, err := n.Repo.Config()
if err != nil {
Expand Down Expand Up @@ -456,7 +478,12 @@ func (n *IpfsNode) SetupOfflineRouting() error {

n.Routing = offroute.NewOfflineRouter(n.Repo.Datastore(), n.PrivateKey)

n.Namesys = namesys.NewNameSystem(n.Routing, n.Repo.Datastore())
cachelife, err := n.getCacheLifetime()
if err != nil {
return err
}

n.Namesys = namesys.NewNameSystem(n.Routing, n.Repo.Datastore(), cachelife)

return nil
}
Expand Down
6 changes: 4 additions & 2 deletions namesys/namesys.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,21 @@ type mpns struct {
}

// NewNameSystem will construct the IPFS naming system based on Routing
func NewNameSystem(r routing.IpfsRouting, ds ds.Datastore) NameSystem {
func NewNameSystem(r routing.IpfsRouting, ds ds.Datastore, cachelife time.Duration) NameSystem {
return &mpns{
resolvers: map[string]resolver{
"dns": newDNSResolver(),
"proquint": new(ProquintResolver),
"dht": NewRoutingResolver(r),
"dht": NewRoutingResolver(r, cachelife),
},
publishers: map[string]Publisher{
"/ipns/": NewRoutingPublisher(r, ds),
},
}
}

const DefaultResolverCacheLife = time.Minute

// Resolve implements Resolver.
func (ns *mpns) Resolve(ctx context.Context, name string) (path.Path, error) {
return ns.ResolveN(ctx, name, DefaultDepthLimit)
Expand Down
3 changes: 2 additions & 1 deletion namesys/republisher/repub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ 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 All @@ -37,6 +36,8 @@ func TestRepublish(t *testing.T) {
t.Fatal(err)
}

nd.Namesys = namesys.NewNameSystem(nd.Routing, nd.Repo.Datastore(), 0)

nodes = append(nodes, nd)
}

Expand Down
6 changes: 3 additions & 3 deletions namesys/resolve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestRoutingResolve(t *testing.T) {
d := mockrouting.NewServer().Client(testutil.RandIdentityOrFatal(t))
dstore := ds.NewMapDatastore()

resolver := NewRoutingResolver(d)
resolver := NewRoutingResolver(d, 0)
publisher := NewRoutingPublisher(d, dstore)

privk, pubk, err := testutil.RandTestKeyPair(512)
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestPrexistingExpiredRecord(t *testing.T) {
dstore := ds.NewMapDatastore()
d := mockrouting.NewServer().ClientWithDatastore(context.Background(), testutil.RandIdentityOrFatal(t), dstore)

resolver := NewRoutingResolver(d)
resolver := NewRoutingResolver(d, 0)
publisher := NewRoutingPublisher(d, dstore)

privk, pubk, err := testutil.RandTestKeyPair(512)
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestPrexistingRecord(t *testing.T) {
dstore := ds.NewMapDatastore()
d := mockrouting.NewServer().ClientWithDatastore(context.Background(), testutil.RandIdentityOrFatal(t), dstore)

resolver := NewRoutingResolver(d)
resolver := NewRoutingResolver(d, 0)
publisher := NewRoutingPublisher(d, dstore)

privk, pubk, err := testutil.RandTestKeyPair(512)
Expand Down
12 changes: 6 additions & 6 deletions namesys/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ type routingResolver struct {

cache map[string]cacheEntry
cachelock sync.Mutex
cachelife time.Duration
}

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 {
if ok && time.Now().Sub(entry.recvd) < r.cachelife {
return entry.val, true
}

Expand All @@ -46,23 +47,22 @@ func (r *routingResolver) cacheSet(name string, val path.Path) {
r.cachelock.Unlock()
}

var IpnsCacheLife = time.Minute

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

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

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

Expand Down
2 changes: 2 additions & 0 deletions repo/config/ipns.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ package config
type Ipns struct {
RepublishPeriod string
RecordLifetime string

ResolveCacheTime string
}
1 change: 1 addition & 0 deletions test/sharness/t0240-republisher.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ setup_iptb() {
for i in $(test_seq 0 3)
do
ipfsi $i config Ipns.RepublishPeriod 20s
ipfsi $i config Ipns.ResolveCacheTime 0s
done
'

Expand Down

0 comments on commit e371661

Please sign in to comment.