Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 4 commits into from
Nov 18, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions core/coreapi/unixfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package coreapi
import (
"context"
"fmt"
"sync"

"github.com/ipfs/go-ipfs/core"

Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down