Skip to content
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

how to wait until DHT bootstrap ready? #989

Open
mostcute opened this issue Sep 3, 2024 · 3 comments
Open

how to wait until DHT bootstrap ready? #989

mostcute opened this issue Sep 3, 2024 · 3 comments

Comments

@mostcute
Copy link

mostcute commented Sep 3, 2024

the chat with rendezvous example use time.sleep(1 * time.Second) instead of block until bootstrap ready

how to wait until bootstrap ready in product env?

i see wait 1sec for bootstrap can not find peers stable, but 10 sec will be more stable,what is the correct way to block it ?

@mostcute
Copy link
Author

mostcute commented Sep 3, 2024

	kademliaDHT, err := dht.New(ctx, h, dht.BootstrapPeers(bootstrapPeers...))
	if err != nil {
		panic(err)
	}

	// Bootstrap the DHT. In the default configuration, this spawns a Background
	// thread that will refresh the peer table every five minutes.
	fmt.Println("Bootstrapping the DHT")
	if err = kademliaDHT.Bootstrap(ctx); err != nil {
		panic(err)
	}
	// Wait a bit to let bootstrapping finish (really bootstrap should block until it's ready, but that isn't the case yet.)
	time.Sleep(15 * time.Second)


	fmt.Println("Announcing ourselves...")
	routingDiscovery := drouting.NewRoutingDiscovery(kademliaDHT)
	dutil.Advertise(ctx, routingDiscovery, "find me here hahaha")
	fmt.Println("Successfully announced!")

	// Now, look for others who have announced
	// This is like your friend telling you the location to meet you.
	fmt.Println("Searching for other peers...")
	peerChan, err := routingDiscovery.FindPeers(ctx, "find me here hahaha")
	if err != nil {
		panic(err)
	}

if use time.sleep(1 * time.Second) ,peerChan might receive no peers sometimes (8/10 chance failed get peers),if increase to 10 *second , will get peer success everytime

@burdiyan
Copy link

If you have access to the instance of the DHT object, one way to do it is probably calling Bootstrap(), and then calling RefreshRoutingTable() waiting on the returned channel.

@sukunrt sukunrt transferred this issue from libp2p/go-libp2p Sep 19, 2024
@mostcute
Copy link
Author

just like this right?

		if err = kademliaDHT.Bootstrap(ctx); err != nil {
			panic(err)
		}
		errchan := kademliaDHT.RefreshRoutingTable()
		<-errchan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants