Skip to content

Commit

Permalink
add in ttl option to ipfs name publish
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 29, 2015
1 parent d79e3b6 commit 9577fab
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
14 changes: 13 additions & 1 deletion core/commands/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Publish an <ipfs-path> to another public key (not implemented):
Options: []cmds.Option{
cmds.BoolOption("resolve", "resolve given path before publishing (default=true)"),
cmds.StringOption("lifetime", "t", "time duration that the record will be valid for (default: 24hrs)"),
cmds.StringOption("ttl", "time duration this record should be cached for (caution: experimental)"),
},
Run: func(req cmds.Request, res cmds.Response) {
log.Debug("Begin Publish")
Expand Down Expand Up @@ -96,7 +97,18 @@ Publish an <ipfs-path> to another public key (not implemented):
popts.pubValidTime = d
}

output, err := publish(req.Context(), n, n.PrivateKey, path.Path(pstr), popts)
ctx := req.Context()
if ttl, found, _ := req.Option("ttl").String(); found {
d, err := time.ParseDuration(ttl)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
}

ctx = context.WithValue(ctx, "ipns-publish-ttl", d)
}

output, err := publish(ctx, n, n.PrivateKey, path.Path(pstr), popts)
if err != nil {
res.SetError(err, cmds.ErrNormal)
return
Expand Down
8 changes: 8 additions & 0 deletions namesys/pb/namesys.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions namesys/pb/namesys.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ message IpnsEntry {
optional bytes validity = 4;

optional uint64 sequence = 5;

optional uint64 ttl = 6;
}
18 changes: 18 additions & 0 deletions namesys/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,31 @@ func (p *ipnsPublisher) getPreviousSeqNo(ctx context.Context, ipnskey key.Key) (
return e.GetSequence(), nil
}

// setting the TTL on published records is an experimental feature.
// as such, i'm using the context to wire it through to avoid changing too
// much code along the way.
func checkCtxTTL(ctx context.Context) (time.Duration, bool) {
v := ctx.Value("ipns-publish-ttl")
if v == nil {
return 0, false
}

d, ok := v.(time.Duration)
return d, ok
}

func PutRecordToRouting(ctx context.Context, k ci.PrivKey, value path.Path, seqnum uint64, eol time.Time, r routing.IpfsRouting, id peer.ID) error {
namekey, ipnskey := IpnsKeysForID(id)
entry, err := CreateRoutingEntryData(k, value, seqnum, eol)
if err != nil {
return err
}

ttl, ok := checkCtxTTL(ctx)
if ok {
entry.Ttl = proto.Uint64(uint64(ttl.Nanoseconds()))
}

err = PublishEntry(ctx, r, ipnskey, entry)
if err != nil {
return err
Expand Down
11 changes: 10 additions & 1 deletion namesys/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,16 @@ func (r *routingResolver) cacheGet(name string) (path.Path, bool) {
}

func (r *routingResolver) cacheSet(name string, val path.Path, rec *pb.IpnsEntry) {
cacheTil := time.Now().Add(r.cachelife)
ttl := r.cachelife
if rec.Ttl != nil {
// if the record has a ttl set, and its less than ours, use it instead
recttl := time.Duration(rec.GetTtl())
if recttl < ttl {
ttl = recttl
}
}

cacheTil := time.Now().Add(ttl)
eol, ok := checkEOL(rec)
if ok && eol.Before(cacheTil) {
cacheTil = eol
Expand Down

0 comments on commit 9577fab

Please sign in to comment.