generated from ipfs/ipfs-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bitswap: make ProviderFinder a param for client.New(). Fix tests.
- Loading branch information
Showing
6 changed files
with
39 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ import ( | |
bsmq "github.com/ipfs/boxo/bitswap/client/internal/messagequeue" | ||
"github.com/ipfs/boxo/bitswap/client/internal/notifications" | ||
bspm "github.com/ipfs/boxo/bitswap/client/internal/peermanager" | ||
"github.com/ipfs/boxo/bitswap/client/internal/session" | ||
bssession "github.com/ipfs/boxo/bitswap/client/internal/session" | ||
bssim "github.com/ipfs/boxo/bitswap/client/internal/sessioninterestmanager" | ||
bssm "github.com/ipfs/boxo/bitswap/client/internal/sessionmanager" | ||
|
@@ -32,7 +33,6 @@ import ( | |
logging "github.com/ipfs/go-log/v2" | ||
"github.com/ipfs/go-metrics-interface" | ||
"github.com/libp2p/go-libp2p/core/peer" | ||
"github.com/libp2p/go-libp2p/core/routing" | ||
"go.opentelemetry.io/otel/attribute" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
@@ -118,10 +118,16 @@ type BlockReceivedNotifier interface { | |
ReceivedBlocks(peer.ID, []blocks.Block) | ||
} | ||
|
||
// ProviderFinder is a subset of | ||
// https://pkg.go.dev/github.com/libp2p/[email protected]/core/routing#ContentRouting | ||
type ProviderFinder interface { | ||
FindProvidersAsync(context.Context, cid.Cid, int) <-chan peer.AddrInfo | ||
} | ||
|
||
// New initializes a Bitswap client that runs until client.Close is called. | ||
// The Content router paramteter can be nil to disable content-routing | ||
// The Content providerFinder paramteter can be nil to disable content-routing | ||
// lookups for content (rely only on bitswap for discovery). | ||
func New(parent context.Context, network bsnet.BitSwapNetwork, router routing.Routing, bstore blockstore.Blockstore, options ...Option) *Client { | ||
func New(parent context.Context, network bsnet.BitSwapNetwork, providerFinder ProviderFinder, bstore blockstore.Blockstore, options ...Option) *Client { | ||
// important to use provided parent context (since it may include important | ||
// loggable data). It's probably not a good idea to allow bitswap to be | ||
// coupled to the concerns of the ipfs daemon in this way. | ||
|
@@ -133,7 +139,7 @@ func New(parent context.Context, network bsnet.BitSwapNetwork, router routing.Ro | |
|
||
bs := &Client{ | ||
network: network, | ||
router: router, | ||
providerFinder: providerFinder, | ||
blockstore: bstore, | ||
cancel: cancelFunc, | ||
closing: make(chan struct{}), | ||
|
@@ -168,9 +174,10 @@ func New(parent context.Context, network bsnet.BitSwapNetwork, router routing.Ro | |
sim := bssim.New() | ||
bpm := bsbpm.New() | ||
pm := bspm.New(ctx, peerQueueFactory, network.Self()) | ||
if bs.router != nil && bs.defaultProviderQueryManager { | ||
|
||
if bs.providerFinder != nil && bs.defaultProviderQueryManager { | ||
// network can do dialing. | ||
pqm, err := rpqm.New(ctx, network, bs.router, rpqm.WithMaxProviders(10)) | ||
pqm, err := rpqm.New(ctx, network, bs.providerFinder, rpqm.WithMaxProviders(10)) | ||
if err != nil { | ||
// Should not be possible to hit this | ||
panic(err) | ||
|
@@ -192,7 +199,15 @@ func New(parent context.Context, network bsnet.BitSwapNetwork, router routing.Ro | |
rebroadcastDelay delay.D, | ||
self peer.ID, | ||
) bssm.Session { | ||
return bssession.New(sessctx, sessmgr, id, spm, bs.pqm, sim, pm, bpm, notif, provSearchDelay, rebroadcastDelay, self) | ||
// careful when bs.pqm is nil. Since we are type-casting it | ||
// into session.ProviderFinder when passing it, it will become | ||
// not nil. Related: | ||
// https://groups.google.com/g/golang-nuts/c/wnH302gBa4I?pli=1 | ||
var pqm session.ProviderFinder | ||
if bs.pqm != nil { | ||
pqm = bs.pqm | ||
} | ||
return bssession.New(sessctx, sessmgr, id, spm, pqm, sim, pm, bpm, notif, provSearchDelay, rebroadcastDelay, self) | ||
} | ||
sessionPeerManagerFactory := func(ctx context.Context, id uint64) bssession.SessionPeerManager { | ||
return bsspm.New(id, network.ConnectionManager()) | ||
|
@@ -212,8 +227,7 @@ func New(parent context.Context, network bsnet.BitSwapNetwork, router routing.Ro | |
type Client struct { | ||
pm *bspm.PeerManager | ||
|
||
// content router | ||
router routing.Routing | ||
providerFinder ProviderFinder | ||
|
||
// the provider query manager manages requests to find providers | ||
pqm *rpqm.ProviderQueryManager | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters