提交 6a171a0b 作者: hucg

fix issue 6760, adding with hash-only, high CPU usage.

上级 410e7e87
...@@ -354,6 +354,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment ...@@ -354,6 +354,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
// We wait for the node to close first, as the node has children // We wait for the node to close first, as the node has children
// that it will wait for before closing, such as the API server. // that it will wait for before closing, such as the API server.
node.Close() node.Close()
coreapi.CloseFakeRepo()
select { select {
case <-req.Context.Done(): case <-req.Context.Done():
......
...@@ -3,6 +3,7 @@ package coreapi ...@@ -3,6 +3,7 @@ package coreapi
import ( import (
"context" "context"
"fmt" "fmt"
"sync"
"github.com/ipfs/go-ipfs/core" "github.com/ipfs/go-ipfs/core"
...@@ -27,6 +28,43 @@ import ( ...@@ -27,6 +28,43 @@ import (
) )
type UnixfsAPI CoreAPI type UnixfsAPI CoreAPI
var nilNode *core.IpfsNode
var lock sync.Mutex
func getOrCreateNilNode() (*core.IpfsNode,error) {
lock.Lock()
defer lock.Unlock()
if nilNode != nil {
return nilNode,nil
}
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 {
return nil, err
}
nilNode = node
return nilNode,nil
}
func CloseFakeRepo() error {
lock.Lock()
defer lock.Unlock()
if nilNode != nil {
if err := nilNode.Close(); err != nil {
return err
}
nilNode = nil
}
return nil
}
// Add builds a merkledag node from a reader, adds it to the blockstore, // Add builds a merkledag node from a reader, adds it to the blockstore,
// and returns the key representing that node. // and returns the key representing that node.
...@@ -61,18 +99,13 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.Node, opts ...options ...@@ -61,18 +99,13 @@ func (api *UnixfsAPI) Add(ctx context.Context, files files.Node, opts ...options
pinning := api.pinning pinning := api.pinning
if settings.OnlyHash { if settings.OnlyHash {
nilnode, err := core.NewNode(ctx, &core.BuildCfg{ node, err := getOrCreateNilNode()
//TODO: need this to be true or all files
// hashed will be stored in memory!
NilRepo: true,
})
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer nilnode.Close() addblockstore = node.Blockstore
addblockstore = nilnode.Blockstore exch = node.Exchange
exch = nilnode.Exchange pinning = node.Pinning
pinning = nilnode.Pinning
} }
bserv := blockservice.New(addblockstore, exch) // hash security 001 bserv := blockservice.New(addblockstore, exch) // hash security 001
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论