Skip to content

Commit ed624c4

Browse files
committed
feat: add delay and improve error handling for DHT peer discovery
1 parent 2032917 commit ed624c4

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

main.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ import (
2222
"github.com/libp2p/go-libp2p/core/peer"
2323
"github.com/libp2p/go-libp2p/core/pnet"
2424
"github.com/libp2p/go-libp2p/p2p/discovery/mdns"
25-
"github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/proto"
2625
drouting "github.com/libp2p/go-libp2p/p2p/discovery/routing"
26+
"github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/proto"
2727
"github.com/multiformats/go-multiaddr"
2828
)
2929

@@ -35,10 +35,10 @@ type Message struct {
3535
}
3636

3737
type PeerTracker struct {
38-
mu sync.RWMutex
39-
names map[peer.ID]string
40-
relayCount int
41-
isRelaying map[string]bool
38+
mu sync.RWMutex
39+
names map[peer.ID]string
40+
relayCount int
41+
isRelaying map[string]bool
4242
}
4343

4444
func NewPeerTracker() *PeerTracker {
@@ -176,9 +176,15 @@ func main() {
176176

177177
routingDiscovery := drouting.NewRoutingDiscovery(kadDHT)
178178
go func() {
179+
time.Sleep(5 * time.Second)
180+
179181
_, err := routingDiscovery.Advertise(ctx, topicName)
180182
if err != nil && ctx.Err() == nil {
181-
log.Printf("Failed to advertise: %v", err)
183+
if strings.Contains(err.Error(), "failed to find any peer in table") {
184+
log.Printf("DHT routing table empty - peer discovery via mDNS or bootstrap nodes")
185+
} else {
186+
log.Printf("Failed to advertise: %v", err)
187+
}
182188
} else if err == nil {
183189
fmt.Println("Announcing presence on DHT")
184190
}

0 commit comments

Comments
 (0)