提交 286941d4 作者: Raúl Kripalani

support experimental datastore-backed peerstore.

License: MIT
Signed-off-by: 's avatarRaúl Kripalani <raul@protocol.ai>
上级 495f12c0
...@@ -5,7 +5,8 @@ import ( ...@@ -5,7 +5,8 @@ import (
"crypto/rand" "crypto/rand"
"encoding/base64" "encoding/base64"
"errors" "errors"
"github.com/ipfs/go-ipfs/provider" "fmt"
"os" "os"
"syscall" "syscall"
"time" "time"
...@@ -15,7 +16,7 @@ import ( ...@@ -15,7 +16,7 @@ import (
pin "github.com/ipfs/go-ipfs/pin" pin "github.com/ipfs/go-ipfs/pin"
repo "github.com/ipfs/go-ipfs/repo" repo "github.com/ipfs/go-ipfs/repo"
cidv0v1 "github.com/ipfs/go-ipfs/thirdparty/cidv0v1" cidv0v1 "github.com/ipfs/go-ipfs/thirdparty/cidv0v1"
"github.com/ipfs/go-ipfs/thirdparty/verifbs" verifbs "github.com/ipfs/go-ipfs/thirdparty/verifbs"
bserv "github.com/ipfs/go-blockservice" bserv "github.com/ipfs/go-blockservice"
ds "github.com/ipfs/go-datastore" ds "github.com/ipfs/go-datastore"
...@@ -25,6 +26,7 @@ import ( ...@@ -25,6 +26,7 @@ import (
cfg "github.com/ipfs/go-ipfs-config" cfg "github.com/ipfs/go-ipfs-config"
offline "github.com/ipfs/go-ipfs-exchange-offline" offline "github.com/ipfs/go-ipfs-exchange-offline"
offroute "github.com/ipfs/go-ipfs-routing/offline" offroute "github.com/ipfs/go-ipfs-routing/offline"
provider "github.com/ipfs/go-ipfs/provider"
ipns "github.com/ipfs/go-ipns" ipns "github.com/ipfs/go-ipns"
dag "github.com/ipfs/go-merkledag" dag "github.com/ipfs/go-merkledag"
metrics "github.com/ipfs/go-metrics-interface" metrics "github.com/ipfs/go-metrics-interface"
...@@ -36,6 +38,7 @@ import ( ...@@ -36,6 +38,7 @@ import (
p2phost "github.com/libp2p/go-libp2p-host" p2phost "github.com/libp2p/go-libp2p-host"
peer "github.com/libp2p/go-libp2p-peer" peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore" pstore "github.com/libp2p/go-libp2p-peerstore"
pstoreds "github.com/libp2p/go-libp2p-peerstore/pstoreds"
pstoremem "github.com/libp2p/go-libp2p-peerstore/pstoremem" pstoremem "github.com/libp2p/go-libp2p-peerstore/pstoremem"
record "github.com/libp2p/go-libp2p-record" record "github.com/libp2p/go-libp2p-record"
) )
...@@ -130,23 +133,33 @@ func defaultRepo(dstore repo.Datastore) (repo.Repo, error) { ...@@ -130,23 +133,33 @@ func defaultRepo(dstore repo.Datastore) (repo.Repo, error) {
} }
// NewNode constructs and returns an IpfsNode using the given cfg. // NewNode constructs and returns an IpfsNode using the given cfg.
func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) { func NewNode(ctx context.Context, bcfg *BuildCfg) (*IpfsNode, error) {
if cfg == nil { if bcfg == nil {
cfg = new(BuildCfg) bcfg = new(BuildCfg)
} }
err := cfg.fillDefaults() err := bcfg.fillDefaults()
if err != nil { if err != nil {
return nil, err return nil, err
} }
ctx = metrics.CtxScope(ctx, "ipfs") ctx = metrics.CtxScope(ctx, "ipfs")
config, err := bcfg.Repo.Config()
if err != nil {
return nil, err
}
ps, err := setupPeerstore(ctx, bcfg.Repo.Datastore(), &config.Experimental.Peerstore)
if err != nil {
return nil, fmt.Errorf("failed while creating peerstore: %s", err)
}
n := &IpfsNode{ n := &IpfsNode{
IsOnline: cfg.Online, IsOnline: bcfg.Online,
Repo: cfg.Repo, Repo: bcfg.Repo,
ctx: ctx, ctx: ctx,
Peerstore: pstoremem.NewPeerstore(), Peerstore: ps,
} }
n.RecordValidator = record.NamespacedValidator{ n.RecordValidator = record.NamespacedValidator{
...@@ -157,7 +170,7 @@ func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) { ...@@ -157,7 +170,7 @@ func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
// TODO: this is a weird circular-ish dependency, rework it // TODO: this is a weird circular-ish dependency, rework it
n.proc = goprocessctx.WithContextAndTeardown(ctx, n.teardown) n.proc = goprocessctx.WithContextAndTeardown(ctx, n.teardown)
if err := setupNode(ctx, n, cfg); err != nil { if err := setupNode(ctx, n, bcfg); err != nil {
n.Close() n.Close()
return nil, err return nil, err
} }
...@@ -291,3 +304,39 @@ func setupNode(ctx context.Context, n *IpfsNode, cfg *BuildCfg) error { ...@@ -291,3 +304,39 @@ func setupNode(ctx context.Context, n *IpfsNode, cfg *BuildCfg) error {
return n.loadFilesRoot() return n.loadFilesRoot()
} }
func setupPeerstore(ctx context.Context, dstore repo.Datastore, pcfg *cfg.Peerstore) (pstore.Peerstore, error) {
switch pcfg.Type {
case "", cfg.PeerstoreMemory:
return pstoremem.NewPeerstore(), nil
case cfg.PeerstoreDatastore:
dcfg := pcfg.Datastore
opts := pstoreds.DefaultOpts()
opts.GCLookaheadInterval = 0 // ensure lookahead is disabled.
if dcfg.Cache.Disable {
opts.CacheSize = 0
} else if dcfg.Cache.Size > 0 {
opts.CacheSize = uint(dcfg.Cache.Size)
}
if dcfg.GC.Disable {
opts.GCPurgeInterval = 0
} else if dcfg.GC.PurgeIntervalMillis > 0 {
opts.GCPurgeInterval = time.Duration(dcfg.GC.PurgeIntervalMillis) * time.Millisecond
}
if dcfg.GC.LookaheadIntervalMillis > 0 {
opts.GCLookaheadInterval = time.Duration(dcfg.GC.LookaheadIntervalMillis) * time.Millisecond
}
if dcfg.GC.InitDelayMillis != nil {
opts.GCInitialDelay = time.Duration(*dcfg.GC.InitDelayMillis) * time.Millisecond
}
return pstoreds.NewPeerstore(ctx, dstore, opts)
}
return nil, fmt.Errorf("unsupported peerstore type: %s", pcfg.Type)
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论