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

Add new DHT option to provide custom pb.MessageSender #991

Merged
merged 3 commits into from
Oct 4, 2024
Merged
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
3 changes: 1 addition & 2 deletions dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

"github.com/libp2p/go-libp2p-kad-dht/internal"
dhtcfg "github.com/libp2p/go-libp2p-kad-dht/internal/config"
"github.com/libp2p/go-libp2p-kad-dht/internal/net"
"github.com/libp2p/go-libp2p-kad-dht/metrics"
"github.com/libp2p/go-libp2p-kad-dht/netsize"
pb "github.com/libp2p/go-libp2p-kad-dht/pb"
Expand Down Expand Up @@ -206,7 +205,7 @@ func New(ctx context.Context, h host.Host, options ...Option) (*IpfsDHT, error)
dht.disableFixLowPeers = cfg.DisableFixLowPeers

dht.Validator = cfg.Validator
dht.msgSender = net.NewMessageSenderImpl(h, dht.protocols)
dht.msgSender = cfg.MsgSenderBuilder(h, dht.protocols)
dht.protoMessenger, err = pb.NewProtocolMessenger(dht.msgSender)
if err != nil {
return nil, err
Expand Down
11 changes: 11 additions & 0 deletions dht_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
"time"

dhtcfg "github.com/libp2p/go-libp2p-kad-dht/internal/config"
pb "github.com/libp2p/go-libp2p-kad-dht/pb"
"github.com/libp2p/go-libp2p-kad-dht/providers"
"github.com/libp2p/go-libp2p-kbucket/peerdiversity"
record "github.com/libp2p/go-libp2p-record"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"

Expand Down Expand Up @@ -356,3 +358,12 @@
return nil
}
}

// WithCustomMessageSender configures the pb.MessageSender of the IpfsDHT to use the
// custom implementation of the pb.MessageSender
func WithCustomMessageSender(messageSenderBuilder func(h host.Host, protos []protocol.ID) pb.MessageSenderWithDisconnect) Option {
return func(c *dhtcfg.Config) error {
c.MsgSenderBuilder = messageSenderBuilder
return nil

Check warning on line 367 in dht_options.go

View check run for this annotation

Codecov / codecov/patch

dht_options.go#L364-L367

Added lines #L364 - L367 were not covered by tests
}
}
4 changes: 4 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/ipfs/boxo/ipns"
ds "github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
"github.com/libp2p/go-libp2p-kad-dht/internal/net"
pb "github.com/libp2p/go-libp2p-kad-dht/pb"
"github.com/libp2p/go-libp2p-kad-dht/providers"
"github.com/libp2p/go-libp2p-kbucket/peerdiversity"
record "github.com/libp2p/go-libp2p-record"
Expand Down Expand Up @@ -48,6 +50,7 @@ type Config struct {
ProviderStore providers.ProviderStore
QueryPeerFilter QueryFilterFunc
LookupCheckConcurrency int
MsgSenderBuilder func(h host.Host, protos []protocol.ID) pb.MessageSenderWithDisconnect

RoutingTable struct {
RefreshQueryTimeout time.Duration
Expand Down Expand Up @@ -114,6 +117,7 @@ var Defaults = func(o *Config) error {
o.EnableProviders = true
o.EnableValues = true
o.QueryPeerFilter = EmptyQueryFilter
o.MsgSenderBuilder = net.NewMessageSenderImpl

o.RoutingTable.LatencyTolerance = 10 * time.Second
o.RoutingTable.RefreshQueryTimeout = 10 * time.Second
Expand Down