37 lines
845 B
Go
37 lines
845 B
Go
package p2p
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/libp2p/go-libp2p-core/host"
|
|
"github.com/libp2p/go-libp2p-core/network"
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
dht "github.com/libp2p/go-libp2p-kad-dht"
|
|
)
|
|
|
|
// Discover starts up a DHT based discovery system finding and adding nodes with the same rendezvous string.
|
|
func Discover(ctx context.Context, h host.Host, dht *dht.IpfsDHT, peerTable map[string]peer.ID) {
|
|
ticker := time.NewTicker(time.Second * 1)
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-ticker.C:
|
|
for _, id := range peerTable {
|
|
if h.Network().Connectedness(id) != network.Connected {
|
|
addrs, err := dht.FindPeer(ctx, id)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
_, err = h.Network().DialPeer(ctx, addrs.ID)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|