Skip to content

feat: add request callback config option #1011

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

Merged
merged 2 commits into from
Jan 15, 2025
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: 3 additions & 0 deletions dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ type IpfsDHT struct {
// addrFilter is used to filter the addresses we put into the peer store.
// 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)
}

// Assert that IPFS assumptions about interfaces aren't broken. These aren't a
Expand Down Expand Up @@ -306,6 +308,7 @@ func makeDHT(h host.Host, cfg dhtcfg.Config) (*IpfsDHT, error) {
routingTablePeerFilter: cfg.RoutingTable.PeerFilter,
rtPeerDiversityFilter: cfg.RoutingTable.DiversityFilter,
addrFilter: cfg.AddressFilter,
onRequestHook: cfg.OnRequestHook,

fixLowPeersChan: make(chan struct{}, 1),

Expand Down
4 changes: 4 additions & 0 deletions dht_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@
metrics.ReceivedBytes.M(int64(msgLen)),
)

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

Check warning on line 105 in dht_net.go

View check run for this annotation

Codecov / codecov/patch

dht_net.go#L104-L105

Added lines #L104 - L105 were not covered by tests

handler := dht.handlerForMsgType(req.GetType())
if handler == nil {
stats.Record(ctx, metrics.ReceivedMessageErrors.M(1))
Expand Down
13 changes: 13 additions & 0 deletions dht_options.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dht

import (
"context"
"fmt"
"testing"
"time"
Expand All @@ -12,6 +13,7 @@
"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/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"

Expand Down Expand Up @@ -368,3 +370,14 @@
return nil
}
}

// OnRequestHook registers a callback function that will be invoked for every
// 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 {
return func(c *dhtcfg.Config) error {
c.OnRequestHook = f
return nil
}

Check warning on line 382 in dht_options.go

View check run for this annotation

Codecov / codecov/patch

dht_options.go#L378-L382

Added lines #L378 - L382 were not covered by tests
}
3 changes: 3 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"context"
"fmt"
"time"

Expand All @@ -14,6 +15,7 @@ import (
"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/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
ma "github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -63,6 +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)

// test specific Config options
DisableFixLowPeers bool
Expand Down
Loading