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

HTTP retrieval proposal #747

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion bitswap/benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"time"

"github.com/ipfs/boxo/bitswap"
bsnet "github.com/ipfs/boxo/bitswap/network"
bsnet "github.com/ipfs/boxo/bitswap/network/bsnet"
testinstance "github.com/ipfs/boxo/bitswap/testinstance"
tn "github.com/ipfs/boxo/bitswap/testnet"
mockrouting "github.com/ipfs/boxo/routing/mock"
Expand Down
6 changes: 3 additions & 3 deletions bitswap/client/client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package bitswap implements the IPFS exchange interface with the BitSwap
// Package client implements the IPFS exchange interface with the BitSwap
// bilateral exchange protocol.
package client

Expand Down Expand Up @@ -191,7 +191,7 @@ func New(parent context.Context, network bsnet.BitSwapNetwork, providerFinder Pr

sim := bssim.New()
bpm := bsbpm.New()
pm := bspm.New(ctx, peerQueueFactory, network.Self())
pm := bspm.New(ctx, peerQueueFactory)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out, peer ID is not used in the peermanager.

if bs.providerFinder != nil && bs.defaultProviderQueryManager {
// network can do dialing.
Expand Down Expand Up @@ -232,7 +232,7 @@ func New(parent context.Context, network bsnet.BitSwapNetwork, providerFinder Pr
return bssession.New(sessctx, sessmgr, id, spm, sessionProvFinder, sim, pm, bpm, notif, provSearchDelay, rebroadcastDelay, self)
}
sessionPeerManagerFactory := func(ctx context.Context, id uint64) bssession.SessionPeerManager {
return bsspm.New(id, network.ConnectionManager())
return bsspm.New(id, network)
}
notif := notifications.New()
sm = bssm.New(ctx, sessionFactory, sim, sessionPeerManagerFactory, bpm, pm, notif, network.Self())
Expand Down
5 changes: 1 addition & 4 deletions bitswap/client/internal/peermanager/peermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,17 @@ type PeerManager struct {
psLk sync.RWMutex
sessions map[uint64]Session
peerSessions map[peer.ID]map[uint64]struct{}

self peer.ID
}

// New creates a new PeerManager, given a context and a peerQueueFactory.
func New(ctx context.Context, createPeerQueue PeerQueueFactory, self peer.ID) *PeerManager {
func New(ctx context.Context, createPeerQueue PeerQueueFactory) *PeerManager {
wantGauge := metrics.NewCtx(ctx, "wantlist_total", "Number of items in wantlist.").Gauge()
wantBlockGauge := metrics.NewCtx(ctx, "want_blocks_total", "Number of want-blocks in wantlist.").Gauge()
return &PeerManager{
peerQueues: make(map[peer.ID]PeerQueue),
pwm: newPeerWantManager(wantGauge, wantBlockGauge),
createPeerQueue: createPeerQueue,
ctx: ctx,
self: self,

sessions: make(map[uint64]Session),
peerSessions: make(map[peer.ID]map[uint64]struct{}),
Expand Down
31 changes: 15 additions & 16 deletions bitswap/client/internal/peermanager/peermanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ func TestAddingAndRemovingPeers(t *testing.T) {
peerQueueFactory := makePeerQueueFactory(msgs)

tp := random.Peers(6)
self, peer1, peer2, peer3, peer4, peer5 := tp[0], tp[1], tp[2], tp[3], tp[4], tp[5]
peerManager := New(ctx, peerQueueFactory, self)
peer1, peer2, peer3, peer4, peer5 := tp[0], tp[1], tp[2], tp[3], tp[4]
peerManager := New(ctx, peerQueueFactory)

peerManager.Connected(peer1)
peerManager.Connected(peer2)
Expand Down Expand Up @@ -128,8 +128,8 @@ func TestBroadcastOnConnect(t *testing.T) {
msgs := make(chan msg, 16)
peerQueueFactory := makePeerQueueFactory(msgs)
tp := random.Peers(2)
self, peer1 := tp[0], tp[1]
peerManager := New(ctx, peerQueueFactory, self)
peer1 := tp[0]
peerManager := New(ctx, peerQueueFactory)

cids := random.Cids(2)
peerManager.BroadcastWantHaves(ctx, cids)
Expand All @@ -149,8 +149,8 @@ func TestBroadcastWantHaves(t *testing.T) {
msgs := make(chan msg, 16)
peerQueueFactory := makePeerQueueFactory(msgs)
tp := random.Peers(3)
self, peer1, peer2 := tp[0], tp[1], tp[2]
peerManager := New(ctx, peerQueueFactory, self)
peer1, peer2 := tp[0], tp[1]
peerManager := New(ctx, peerQueueFactory)

cids := random.Cids(3)

Expand Down Expand Up @@ -190,8 +190,8 @@ func TestSendWants(t *testing.T) {
msgs := make(chan msg, 16)
peerQueueFactory := makePeerQueueFactory(msgs)
tp := random.Peers(2)
self, peer1 := tp[0], tp[1]
peerManager := New(ctx, peerQueueFactory, self)
peer1 := tp[0]
peerManager := New(ctx, peerQueueFactory)
cids := random.Cids(4)

peerManager.Connected(peer1)
Expand Down Expand Up @@ -224,8 +224,8 @@ func TestSendCancels(t *testing.T) {
msgs := make(chan msg, 16)
peerQueueFactory := makePeerQueueFactory(msgs)
tp := random.Peers(3)
self, peer1, peer2 := tp[0], tp[1], tp[2]
peerManager := New(ctx, peerQueueFactory, self)
peer1, peer2 := tp[0], tp[1]
peerManager := New(ctx, peerQueueFactory)
cids := random.Cids(4)

// Connect to peer1 and peer2
Expand Down Expand Up @@ -267,8 +267,8 @@ func TestSendCancelsExclude(t *testing.T) {
msgs := make(chan msg, 16)
peerQueueFactory := makePeerQueueFactory(msgs)
tp := random.Peers(3)
self, peer1, peer2 := tp[0], tp[1], tp[2]
peerManager := New(ctx, peerQueueFactory, self)
peer1, peer2 := tp[0], tp[1]
peerManager := New(ctx, peerQueueFactory)
cids := random.Cids(4)

// Connect to peer1 and peer2
Expand Down Expand Up @@ -328,8 +328,8 @@ func TestSessionRegistration(t *testing.T) {
peerQueueFactory := makePeerQueueFactory(msgs)

tp := random.Peers(3)
self, p1, p2 := tp[0], tp[1], tp[2]
peerManager := New(ctx, peerQueueFactory, self)
p1, p2 := tp[0], tp[1]
peerManager := New(ctx, peerQueueFactory)

id := uint64(1)
s := newSess(id)
Expand Down Expand Up @@ -387,9 +387,8 @@ func BenchmarkPeerManager(b *testing.B) {
return &benchPeerQueue{}
}

self := random.Peers(1)[0]
peers := random.Peers(500)
peerManager := New(ctx, peerQueueFactory, self)
peerManager := New(ctx, peerQueueFactory)

// Create a bunch of connections
connected := 0
Expand Down
14 changes: 14 additions & 0 deletions bitswap/network/bsnet/bsnet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package bsnet

import "github.com/ipfs/boxo/bitswap/network/bsnet/internal"

var (
// ProtocolBitswapNoVers is equivalent to the legacy bitswap protocol
ProtocolBitswapNoVers = internal.ProtocolBitswapNoVers
// ProtocolBitswapOneZero is the prefix for the legacy bitswap protocol
ProtocolBitswapOneZero = internal.ProtocolBitswapOneZero
// ProtocolBitswapOneOne is the prefix for version 1.1.0
ProtocolBitswapOneOne = internal.ProtocolBitswapOneOne
// ProtocolBitswap is the current version of the bitswap protocol: 1.2.0
ProtocolBitswap = internal.ProtocolBitswap
)
Comment on lines +1 to +14
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved from network, which now is more an "exchange" network with interfaces and common utils.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package network
package bsnet

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes to this file and others in the module are cosmetic, renames...

import (
"context"
Expand All @@ -9,10 +9,10 @@ import (
"time"

bsmsg "github.com/ipfs/boxo/bitswap/message"
"github.com/ipfs/boxo/bitswap/network/internal"
iface "github.com/ipfs/boxo/bitswap/network"
"github.com/ipfs/boxo/bitswap/network/bsnet/internal"

logging "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p/core/connmgr"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
Expand All @@ -23,7 +23,7 @@ import (
"github.com/multiformats/go-multistream"
)

var log = logging.Logger("bitswap/network")
var log = logging.Logger("bitswap/bsnet")

var (
maxSendTimeout = 2 * time.Minute
Expand All @@ -33,7 +33,7 @@ var (
)

// NewFromIpfsHost returns a BitSwapNetwork supported by underlying IPFS host.
func NewFromIpfsHost(host host.Host, opts ...NetOpt) BitSwapNetwork {
func NewFromIpfsHost(host host.Host, opts ...NetOpt) iface.BitSwapNetwork {
s := processSettings(opts...)

bitswapNetwork := impl{
Expand Down Expand Up @@ -66,10 +66,10 @@ func processSettings(opts ...NetOpt) Settings {
type impl struct {
// NOTE: Stats must be at the top of the heap allocation to ensure 64bit
// alignment.
stats Stats
stats iface.Stats

host host.Host
connectEvtMgr *connectEventManager
connectEvtMgr *iface.ConnectEventManager

protocolBitswapNoVers protocol.ID
protocolBitswapOneZero protocol.ID
Expand All @@ -79,7 +79,7 @@ type impl struct {
supportedProtocols []protocol.ID

// inbound messages from the network are forwarded to the receiver
receivers []Receiver
receivers []iface.Receiver
}

// interfaceWrapper is concrete type that wraps an interface. Necessary because
Expand Down Expand Up @@ -109,7 +109,7 @@ type streamMessageSender struct {
to peer.ID
stream atomicInterface[network.Stream]
bsnet *impl
opts *MessageSenderOpts
opts *iface.MessageSenderOpts
}

type HasContext interface {
Expand Down Expand Up @@ -317,7 +317,7 @@ func (bsnet *impl) msgToStream(ctx context.Context, s network.Stream, msg bsmsg.
return nil
}

func (bsnet *impl) NewMessageSender(ctx context.Context, p peer.ID, opts *MessageSenderOpts) (MessageSender, error) {
func (bsnet *impl) NewMessageSender(ctx context.Context, p peer.ID, opts *iface.MessageSenderOpts) (iface.MessageSender, error) {
opts = setDefaultOpts(opts)

sender := &streamMessageSender{
Expand All @@ -337,7 +337,7 @@ func (bsnet *impl) NewMessageSender(ctx context.Context, p peer.ID, opts *Messag
return sender, nil
}

func setDefaultOpts(opts *MessageSenderOpts) *MessageSenderOpts {
func setDefaultOpts(opts *iface.MessageSenderOpts) *iface.MessageSenderOpts {
copy := *opts
if opts.MaxRetries == 0 {
copy.MaxRetries = 3
Expand Down Expand Up @@ -385,14 +385,14 @@ func (bsnet *impl) newStreamToPeer(ctx context.Context, p peer.ID) (network.Stre
return bsnet.host.NewStream(ctx, p, bsnet.supportedProtocols...)
}

func (bsnet *impl) Start(r ...Receiver) {
func (bsnet *impl) Start(r ...iface.Receiver) {
bsnet.receivers = r
{
connectionListeners := make([]ConnectionListener, len(r))
connectionListeners := make([]iface.ConnectionListener, len(r))
for i, v := range r {
connectionListeners[i] = v
}
bsnet.connectEvtMgr = newConnectEventManager(connectionListeners...)
bsnet.connectEvtMgr = iface.NewConnectEventManager(connectionListeners...)
}
for _, proto := range bsnet.supportedProtocols {
bsnet.host.SetStreamHandler(proto, bsnet.handleNewStream)
Expand Down Expand Up @@ -451,12 +451,36 @@ func (bsnet *impl) handleNewStream(s network.Stream) {
}
}

func (bsnet *impl) ConnectionManager() connmgr.ConnManager {
return bsnet.host.ConnManager()
func (bsnet *impl) TagPeer(p peer.ID, tag string, w int) {
if bsnet.host == nil {
return
}
bsnet.host.ConnManager().TagPeer(p, tag, w)
}

func (bsnet *impl) UntagPeer(p peer.ID, tag string) {
if bsnet.host == nil {
return
}
bsnet.host.ConnManager().UntagPeer(p, tag)
}

func (bsnet *impl) Protect(p peer.ID, tag string) {
if bsnet.host == nil {
return
}
bsnet.host.ConnManager().Protect(p, tag)
}

func (bsnet *impl) Unprotect(p peer.ID, tag string) bool {
if bsnet.host == nil {
return false
}
return bsnet.host.ConnManager().Unprotect(p, tag)
}

func (bsnet *impl) Stats() Stats {
return Stats{
func (bsnet *impl) Stats() iface.Stats {
return iface.Stats{
MessagesRecvd: atomic.LoadUint64(&bsnet.stats.MessagesRecvd),
MessagesSent: atomic.LoadUint64(&bsnet.stats.MessagesSent),
}
Expand Down
Loading
Loading