提交 143ef0dc 作者: vyzo 提交者: Łukasz Magiera

namecache: make followInterval a follow parameter

Also adds --refresh-interval to follow command.

License: MIT
Signed-off-by: 's avatarvyzo <vyzo@hackzen.org>
上级 ec9b120a
......@@ -4,9 +4,11 @@ import (
"errors"
"io"
"strings"
"time"
cmds "github.com/ipfs/go-ipfs/commands"
e "github.com/ipfs/go-ipfs/core/commands/e"
nc "github.com/ipfs/go-ipfs/namecache"
"gx/ipfs/Qmde5VP1qUkyQXKCfmEUA7bP64V2HAptbJ7phuPp7jXWwg/go-ipfs-cmdkit"
)
......@@ -40,7 +42,8 @@ Follows an IPNS name by periodically resolving in the backround.
cmdkit.StringArg("name", true, true, "IPNS Name to follow."),
},
Options: []cmdkit.Option{
cmdkit.BoolOption("pin", "recursively pin the resolved pointer"),
cmdkit.BoolOption("pin", "Recursively pin the resolved pointer"),
cmdkit.StringOption("refresh-interval", "Follow refresh interval."),
},
Run: func(req cmds.Request, res cmds.Response) {
......@@ -57,8 +60,25 @@ Follows an IPNS name by periodically resolving in the backround.
pin, _, _ := req.Option("pin").Bool()
refrS, _, err := req.Option("refresh-interval").String()
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
var refr time.Duration
if refrS == "" {
refr = nc.DefaultFollowInterval
} else {
refr, err = time.ParseDuration(refrS)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}
}
for _, name := range req.Arguments() {
err = n.Namecache.Follow(name, pin)
err = n.Namecache.Follow(name, pin, refr)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
......
......@@ -21,8 +21,8 @@ import (
)
const (
followInterval = 60 * time.Minute
resolveTimeout = 60 * time.Second
DefaultFollowInterval = 1 * time.Hour
resolveTimeout = 1 * time.Minute
)
var log = logging.Logger("namecache")
......@@ -30,7 +30,7 @@ var log = logging.Logger("namecache")
// NameCache represents a following cache of names
type NameCache interface {
// Follow starts following name, pinning it if pinit is true
Follow(name string, pinit bool) error
Follow(name string, pinit bool, followInterval time.Duration) error
// Unofollow cancels a follow
Unfollow(name string) error
// ListFollows returns a list of followed names
......@@ -61,7 +61,7 @@ func NewNameCache(ctx context.Context, nsys namesys.NameSystem, pinning pin.Pinn
// Follow spawns a goroutine that periodically resolves a name
// and (when pinit is true) pins it in the background
func (nc *nameCache) Follow(name string, pinit bool) error {
func (nc *nameCache) Follow(name string, pinit bool, followInterval time.Duration) error {
nc.mx.Lock()
defer nc.mx.Unlock()
......@@ -70,7 +70,7 @@ func (nc *nameCache) Follow(name string, pinit bool) error {
}
ctx, cancel := context.WithCancel(nc.ctx)
go nc.followName(ctx, name, pinit)
go nc.followName(ctx, name, pinit, followInterval)
nc.follows[name] = cancel
return nil
......@@ -104,7 +104,7 @@ func (nc *nameCache) ListFollows() []string {
return follows
}
func (nc *nameCache) followName(ctx context.Context, name string, pinit bool) {
func (nc *nameCache) followName(ctx context.Context, name string, pinit bool, followInterval time.Duration) {
// if cid != nil, we have created a new pin that is updated on changes and
// unpinned on cancel
c, err := nc.resolveAndPin(ctx, name, pinit)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论