Skip to content

Commit

Permalink
refactor: core/node/bitswap.go (#8349)
Browse files Browse the repository at this point in the history
- extracts bitswap init to separate file and defines implicit defaults as consts
- fixes a typo where BitswapTaskWorkerCount was 128 (instead of 8)
  and BitswapEngineBlockstoreWorkerCount was 8 (instead of 128)
  • Loading branch information
lidel authored Aug 17, 2021
1 parent 50f2681 commit 37e430d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 33 deletions.
52 changes: 52 additions & 0 deletions core/node/bitswap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package node

import (
"context"

"github.com/ipfs/go-bitswap"
"github.com/ipfs/go-bitswap/network"
blockstore "github.com/ipfs/go-ipfs-blockstore"
config "github.com/ipfs/go-ipfs-config"
exchange "github.com/ipfs/go-ipfs-exchange-interface"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/routing"
"go.uber.org/fx"

"github.com/ipfs/go-ipfs/core/node/helpers"
)

const (
// Docs: https://github.com/ipfs/go-ipfs/blob/master/docs/config.md#internalbitswap
DefaultEngineBlockstoreWorkerCount = 128
DefaultTaskWorkerCount = 8
DefaultEngineTaskWorkerCount = 8
DefaultMaxOutstandingBytesPerPeer = 1 << 20
)

// OnlineExchange creates new LibP2P backed block exchange (BitSwap)
func OnlineExchange(cfg *config.Config, provide bool) interface{} {
return func(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host, rt routing.Routing, bs blockstore.GCBlockstore) exchange.Interface {
bitswapNetwork := network.NewFromIpfsHost(host, rt)

var internalBsCfg config.InternalBitswap
if cfg.Internal.Bitswap != nil {
internalBsCfg = *cfg.Internal.Bitswap
}

opts := []bitswap.Option{
bitswap.ProvideEnabled(provide),
bitswap.EngineBlockstoreWorkerCount(int(internalBsCfg.EngineBlockstoreWorkerCount.WithDefault(DefaultEngineBlockstoreWorkerCount))),
bitswap.TaskWorkerCount(int(internalBsCfg.TaskWorkerCount.WithDefault(DefaultTaskWorkerCount))),
bitswap.EngineTaskWorkerCount(int(internalBsCfg.EngineTaskWorkerCount.WithDefault(DefaultEngineTaskWorkerCount))),
bitswap.MaxOutstandingBytesPerPeer(int(internalBsCfg.MaxOutstandingBytesPerPeer.WithDefault(DefaultMaxOutstandingBytesPerPeer))),
}
exch := bitswap.New(helpers.LifecycleCtx(mctx, lc), bitswapNetwork, bs, opts...)
lc.Append(fx.Hook{
OnStop: func(ctx context.Context) error {
return exch.Close()
},
})
return exch

}
}
33 changes: 0 additions & 33 deletions core/node/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,18 @@ import (
"context"
"fmt"

"github.com/ipfs/go-bitswap"
"github.com/ipfs/go-bitswap/network"
"github.com/ipfs/go-blockservice"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-filestore"
blockstore "github.com/ipfs/go-ipfs-blockstore"
config "github.com/ipfs/go-ipfs-config"
exchange "github.com/ipfs/go-ipfs-exchange-interface"
pin "github.com/ipfs/go-ipfs-pinner"
"github.com/ipfs/go-ipfs-pinner/dspinner"
format "github.com/ipfs/go-ipld-format"
"github.com/ipfs/go-merkledag"
"github.com/ipfs/go-mfs"
"github.com/ipfs/go-unixfs"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/routing"
"go.uber.org/fx"

"github.com/ipfs/go-ipfs/core/node/helpers"
Expand Down Expand Up @@ -86,34 +81,6 @@ func Dag(bs blockservice.BlockService) format.DAGService {
return merkledag.NewDAGService(bs)
}

// OnlineExchange creates new LibP2P backed block exchange (BitSwap)
func OnlineExchange(cfg *config.Config, provide bool) interface{} {
return func(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host, rt routing.Routing, bs blockstore.GCBlockstore) exchange.Interface {
bitswapNetwork := network.NewFromIpfsHost(host, rt)

var internalBsCfg config.InternalBitswap
if cfg.Internal.Bitswap != nil {
internalBsCfg = *cfg.Internal.Bitswap
}

opts := []bitswap.Option{
bitswap.ProvideEnabled(provide),
bitswap.EngineBlockstoreWorkerCount(int(internalBsCfg.EngineBlockstoreWorkerCount.WithDefault(8))),
bitswap.TaskWorkerCount(int(internalBsCfg.TaskWorkerCount.WithDefault(128))),
bitswap.EngineTaskWorkerCount(int(internalBsCfg.EngineTaskWorkerCount.WithDefault(8))),
bitswap.MaxOutstandingBytesPerPeer(int(internalBsCfg.MaxOutstandingBytesPerPeer.WithDefault(1 << 20))),
}
exch := bitswap.New(helpers.LifecycleCtx(mctx, lc), bitswapNetwork, bs, opts...)
lc.Append(fx.Hook{
OnStop: func(ctx context.Context) error {
return exch.Close()
},
})
return exch

}
}

// Files loads persisted MFS root
func Files(mctx helpers.MetricsCtx, lc fx.Lifecycle, repo repo.Repo, dag format.DAGService) (*mfs.Root, error) {
dsk := datastore.NewKey("/local/filesroot")
Expand Down

0 comments on commit 37e430d

Please sign in to comment.