Skip to content

Commit

Permalink
Add possibility of providing DHT instance to PeerDiscovery
Browse files Browse the repository at this point in the history
  • Loading branch information
bahner committed Nov 26, 2023
1 parent 31e1b68 commit 4d60f5f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D
git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/bahner/go-ma v0.0.5 h1:RgQrINqn6zU4avHDwoYfPCtMnpU9OpTkOUaFcTsS0MM=
github.com/bahner/go-ma v0.0.5/go.mod h1:l9ty9QbOZfRUtovECXES7a01Su7xBYIBphFak1TOjVs=
github.com/bahner/go-ma v0.0.6-0.20231125235358-e5f8a113aef4 h1:/YfOUHvRcDYO3nAYOXzwwosZzSmOgbEQVhMM6m2N2Ew=
github.com/bahner/go-ma v0.0.6-0.20231125235358-e5f8a113aef4/go.mod h1:l9ty9QbOZfRUtovECXES7a01Su7xBYIBphFak1TOjVs=
github.com/bahner/go-ma v0.0.6-0.20231126000006-ea6d3230b7c5 h1:Olf3MlIvYYfgdDQnofqcKSQOdO7hcGleD68ofrptxsA=
github.com/bahner/go-ma v0.0.6-0.20231126000006-ea6d3230b7c5/go.mod h1:l9ty9QbOZfRUtovECXES7a01Su7xBYIBphFak1TOjVs=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
Expand Down
26 changes: 22 additions & 4 deletions p2p/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,42 @@ import (

"github.com/bahner/go-ma-actor/p2p/dht"
"github.com/bahner/go-ma-actor/p2p/mdns"
p2pdht "github.com/libp2p/go-libp2p-kad-dht"
"github.com/libp2p/go-libp2p/core/host"
log "github.com/sirupsen/logrus"
)

func StartPeerDiscovery(ctx context.Context, h host.Host) error {
// StartPeerDiscovery starts the peer discovery process.
// The context should be cancelled to stop the discovery process, and
// should probably a timeout context.
// Host is just a libp2p host.
//
// DHT is a Kademlia DHT instance.
// If nil, a new DHT instance will be created.
// You might want to pass a DHT instance in Server mode here, for long running processes.
func StartPeerDiscovery(ctx context.Context, h host.Host, dhtInstance *p2pdht.IpfsDHT) error {
log.Debug("Starting peer discovery...")

var err error
done := make(chan struct{}, 2) // Buffered channel to avoid blocking

if dhtInstance == nil {
dhtInstance, err = dht.Init(ctx, h)
if err != nil {
log.Errorf("Failed to initialise DHT. Peer discovery unsuccessful. %v ", err)
done <- struct{}{} // Signal completion
return err
}
}

// Start DHT discovery in a new goroutine
go func() {
dhtINstance, err := dht.Init(ctx, h)

if err != nil {
log.Errorf("Failed to initialise DHT. Peer discovery unsuccessful. %v ", err)
done <- struct{}{} // Signal completion
return
}
dht.DiscoverPeers(ctx, dhtINstance, h)
dht.DiscoverPeers(ctx, dhtInstance, h)
done <- struct{}{} // Signal completion
}()

Expand Down
2 changes: 1 addition & 1 deletion p2p/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func Init(ctx context.Context, i *ipns.Key, discoveryTimeout time.Duration, p2pO
ctxDiscovery, cancel = context.WithTimeout(ctx, discoveryTimeout)
defer cancel()

err = StartPeerDiscovery(ctxDiscovery, n)
err = StartPeerDiscovery(ctxDiscovery, n, nil)
if err != nil {
return nil, nil, fmt.Errorf("p2p.Init: failed to start peer discovery: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion ui/p2p.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import "github.com/bahner/go-ma-actor/p2p"
func (ui *ChatUI) triggerDiscovery() {

// go ui.n.StartPeerDiscovery(ui.ctx, config.GetRendezvous())
p2p.StartPeerDiscovery(ui.ctx, ui.n)
p2p.StartPeerDiscovery(ui.ctx, ui.n, nil)
ui.displaySystemMessage("Discovery process started...")
}

0 comments on commit 4d60f5f

Please sign in to comment.