Skip to content

Commit

Permalink
Add option to disable fallback on Want-Blocks
Browse files Browse the repository at this point in the history
Add an option to disable fallback on `Want-Blocks` when a peer does not
support `Want-Haves`.

Assure connection to peer before checking whether a peer supports
`Want-Haves` to reduce false negatives.

Refactor default connection manger settings to constants.

Fixes #3
  • Loading branch information
masih committed Mar 20, 2023
1 parent 3bbce4d commit 87a22ed
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
17 changes: 14 additions & 3 deletions broadcaster.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,14 @@ func (cs *channeledSender) start() {
}

func (cs *channeledSender) supportsHaves() bool {
// TODO open stream to detect this as that is more reliable
// TODO cache supported protocol IDs for the recipient
// Assure connection to peer before checking protocols list. Otherwise, GetProtocols
// silently returns empty protocols list.
if addrs := cs.c.h.Peerstore().Addrs(cs.id); len(addrs) == 0 {
return false
} else if err := cs.c.h.Connect(cs.ctx, peer.AddrInfo{ID: cs.id, Addrs: addrs}); err != nil {
logger.Errorw("Failed to connect to peer in order to determine Want-Haves support", "peer", cs.id, "err", err)
return false
}
protocols, err := cs.c.h.Peerstore().GetProtocols(cs.id)
if err != nil {
return false
Expand All @@ -174,8 +180,13 @@ func (cs *channeledSender) sendUnsent() {
var wlt bitswap_message_pb.Message_Wantlist_WantType
if cs.supportsHaves() {
wlt = bitswap_message_pb.Message_Wantlist_Have
} else {
} else if cs.c.fallbackOnWantBlock {
wlt = bitswap_message_pb.Message_Wantlist_Block
} else {
logger.Warnw("Peer does not support Want-Haves and fallback on Want-Blocks is disabled. Skipping broadcast.", "peer", cs.id, "skipped", len(cs.unsentCids))
// Clear unsent CIDs.
cs.unsentCids = make(map[cid.Cid]struct{})
return
}
msg := message.New(false)
for c := range cs.unsentCids {
Expand Down
9 changes: 8 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ var kuboBootstrapPeers = []string{
"/ip4/104.131.131.82/udp/4001/quic/p2p/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ", // mars.i.ipfs.io
}

const (
defaultConnMngrLowWaterMark = 0xff
defaultConnMngrHighWaterMark = 0xfff
)

type (
Option func(*options) error
options struct {
Expand All @@ -34,6 +39,7 @@ type (
findByMultihash bool
messageSenderBuffer int
recipientsRefreshInterval time.Duration
fallbackOnWantBlock bool

maxBroadcastBatchSize int
maxBroadcastBatchWait time.Duration
Expand All @@ -49,6 +55,7 @@ func newOptions(o ...Option) (*options, error) {
messageSenderBuffer: 100,
findByMultihash: true,
recipientsRefreshInterval: 10 * time.Second,
fallbackOnWantBlock: true,
maxBroadcastBatchSize: 100,
maxBroadcastBatchWait: 100 * time.Millisecond,
}
Expand All @@ -59,7 +66,7 @@ func newOptions(o ...Option) (*options, error) {
}

if opts.h == nil {
manager, err := connmgr.NewConnManager(500, 5000)
manager, err := connmgr.NewConnManager(defaultConnMngrLowWaterMark, defaultConnMngrHighWaterMark)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 87a22ed

Please sign in to comment.