Skip to content

Commit

Permalink
Merge pull request #1012 from libp2p/fix/onRequestHook
Browse files Browse the repository at this point in the history
fix: don't copy message to OnRequestHook
  • Loading branch information
guillaumemichel authored Jan 28, 2025
2 parents 08ec10d + 44e76f5 commit 811669a
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
9 changes: 5 additions & 4 deletions dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ import (
"go.uber.org/zap"
)

const tracer = tracing.Tracer("go-libp2p-kad-dht")
const dhtName = "IpfsDHT"
const (
tracer = tracing.Tracer("go-libp2p-kad-dht")
dhtName = "IpfsDHT"
)

var (
logger = logging.Logger("dht")
Expand Down Expand Up @@ -164,7 +166,7 @@ type IpfsDHT struct {
// Mostly used to filter out localhost and local addresses.
addrFilter func([]ma.Multiaddr) []ma.Multiaddr

onRequestHook func(ctx context.Context, s network.Stream, req pb.Message)
onRequestHook func(ctx context.Context, s network.Stream, req *pb.Message)
}

// Assert that IPFS assumptions about interfaces aren't broken. These aren't a
Expand Down Expand Up @@ -420,7 +422,6 @@ func makeRoutingTable(dht *IpfsDHT, cfg dhtcfg.Config, maxLastSuccessfulOutbound
df, err := peerdiversity.NewFilter(dht.rtPeerDiversityFilter, "rt/diversity", func(p peer.ID) int {
return kb.CommonPrefixLen(dht.selfKey, kb.ConvertPeerID(p))
})

if err != nil {
return nil, fmt.Errorf("failed to construct peer diversity filter: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion dht_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (dht *IpfsDHT) handleNewMessage(s network.Stream) bool {
)

if dht.onRequestHook != nil {
dht.onRequestHook(ctx, s, req)
dht.onRequestHook(ctx, s, &req)
}

handler := dht.handlerForMsgType(req.GetType())
Expand Down
2 changes: 1 addition & 1 deletion dht_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ func WithCustomMessageSender(messageSenderBuilder func(h host.Host, protos []pro
// incoming DHT protocol message.
// Note: Ensure that the callback executes efficiently, as it will block the
// entire message handler.
func OnRequestHook(f func(ctx context.Context, s network.Stream, req pb.Message)) Option {
func OnRequestHook(f func(ctx context.Context, s network.Stream, req *pb.Message)) Option {
return func(c *dhtcfg.Config) error {
c.OnRequestHook = f
return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type Config struct {

BootstrapPeers func() []peer.AddrInfo
AddressFilter func([]ma.Multiaddr) []ma.Multiaddr
OnRequestHook func(ctx context.Context, s network.Stream, req pb.Message)
OnRequestHook func(ctx context.Context, s network.Stream, req *pb.Message)

// test specific Config options
DisableFixLowPeers bool
Expand Down

0 comments on commit 811669a

Please sign in to comment.