Unverified 提交 8c358629 作者: Steven Allen 提交者: GitHub

Merge pull request #6764 from hcg1314/fix/6760

fix issue 6760, adding with hash-only, high CPU usage.
......@@ -3,6 +3,7 @@ package coreapi
import (
"context"
"fmt"
"sync"
"github.com/ipfs/go-ipfs/core"
......@@ -28,6 +29,28 @@ import (
type UnixfsAPI CoreAPI
var nilNode *core.IpfsNode
var once sync.Once
func getOrCreateNilNode() (*core.IpfsNode,error) {
once.Do(func() {
if nilNode != nil {
return
}
node, err := core.NewNode(context.Background(), &core.BuildCfg{
//TODO: need this to be true or all files
// hashed will be stored in memory!
NilRepo: true,
})
if err != nil {
panic(err)
}
nilNode = node
})
return nilNode, nil
}
// Add builds a merkledag node from a reader, adds it to the blockstore,
// and returns the key representing that node.
func (api *UnixfsAPI) Add(ctx context.Context, files files.Node, opts ...options.UnixfsAddOption) (path.Resolved, error) {
......@@ -61,18 +84,13 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.Node, opts ...options
pinning := api.pinning
if settings.OnlyHash {
nilnode, err := core.NewNode(ctx, &core.BuildCfg{
//TODO: need this to be true or all files
// hashed will be stored in memory!
NilRepo: true,
})
node, err := getOrCreateNilNode()
if err != nil {
return nil, err
}
defer nilnode.Close()
addblockstore = nilnode.Blockstore
exch = nilnode.Exchange
pinning = nilnode.Pinning
addblockstore = node.Blockstore
exch = node.Exchange
pinning = node.Pinning
}
bserv := blockservice.New(addblockstore, exch) // hash security 001
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论