-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" | ||
|
||
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" | ||
) | ||
|
||
|
@@ -87,9 +89,47 @@ 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(DefaultRecordTTL)) | ||
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()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
if err != nil { | ||
log.Error("could not parse path") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"]) | ||
} | ||
if time.Now().Add(DefaultResolverCacheTTL).Before(eol) { | ||
eol = time.Now().Add(DefaultResolverCacheTTL) | ||
} | ||
rr.cache.Add(name.Pretty(), cacheEntry{ | ||
val: value, | ||
eol: eol, | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/ipfs/community/blob/master/go-contribution-guidelines.md#imports