Skip to content

Commit

Permalink
Merge pull request #991 from cortze/feat/add-message-sender-opt
Browse files Browse the repository at this point in the history
Add new DHT option to provide custom pb.MessageSender
  • Loading branch information
guillaumemichel authored Oct 4, 2024
2 parents 3989875 + 794bfc7 commit 0d075d2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
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 @@ import (
"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 @@ func AddressFilter(f func([]ma.Multiaddr) []ma.Multiaddr) Option {
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
}
}
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

0 comments on commit 0d075d2

Please sign in to comment.