Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

namesys: add entry to DHT cache after publish #3501

Merged
merged 4 commits into from
Dec 14, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions namesys/namesys.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package namesys

import (
"context"
"strings"
"time"

context "context"
path "github.com/ipfs/go-ipfs/path"

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ds "gx/ipfs/QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364/go-datastore"
routing "gx/ipfs/QmbkGVaN9W6RYJK4Ws5FvMKXKDqdRQ5snhtaa92qP6L8eU/go-libp2p-routing"
peer "gx/ipfs/QmfMmLGoKzCHDN7cGgk64PJr4iipzidDRME8HABSJqvmhC/go-libp2p-peer"
ci "gx/ipfs/QmfWDLQjGjVe4fr5CoztYW2DYYjRysMJrFe1RCsXLPTf46/go-libp2p-crypto"
)

Expand Down Expand Up @@ -87,9 +89,44 @@ func (ns *mpns) resolveOnce(ctx context.Context, name string) (path.Path, error)

// Publish implements Publisher
func (ns *mpns) Publish(ctx context.Context, name ci.PrivKey, value path.Path) error {
return ns.publishers["/ipns/"].Publish(ctx, name, value)
err := ns.publishers["/ipns/"].Publish(ctx, name, value)
if err != nil {
return err
}
ns.addToDHTCache(name, value, time.Now().Add(time.Hour*24))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets pull this magic number out and justify it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is the same as in publisher.go, should I add comment for it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or extract it from publisher?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd extract it from both, and give it a small comment

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets just call it DefaultRecordTTL (i had to go look at it again to remember what exactly this was)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hah, gave it the same name without reading this. <3

return nil
}

func (ns *mpns) PublishWithEOL(ctx context.Context, name ci.PrivKey, value path.Path, eol time.Time) error {
err := ns.publishers["/ipns/"].PublishWithEOL(ctx, name, value, eol)
if err != nil {
return err
}
ns.addToDHTCache(name, value, eol)
return nil
}

func (ns *mpns) PublishWithEOL(ctx context.Context, name ci.PrivKey, val path.Path, eol time.Time) error {
return ns.publishers["/ipns/"].PublishWithEOL(ctx, name, val, eol)
func (ns *mpns) addToDHTCache(key ci.PrivKey, value path.Path, eol time.Time) {
var err error
value, err = path.ParsePath(value.String())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait, why are we converting the thing to a string, then back to a path?

Copy link
Member Author

@Kubuxu Kubuxu Dec 13, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes as the publisher can publish hash only but resolver will sanitize this value to be /ipfs/CID which is what ParsePath does.

if err != nil {
log.Error("could not parse path")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add the actual error to this? might aid in debugging random error messages

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only cache, I didn't want it to fail the publish (as it was already published and we can't revert it).

return
}

name, err := peer.IDFromPrivateKey(key)
if err != nil {
log.Error("while adding to cache, could not get peerid from private key")
return
}

rr, ok := ns.resolvers["dht"].(*routingResolver)
if !ok {
// should never happen, purely for sanity
log.Panicf("unexpected type %T as DHT resolver.", ns.resolvers["dht"])
}
rr.cache.Add(name.Pretty(), cacheEntry{
val: value,
eol: eol,
})
}