Skip to content

Commit 69798ef

Browse files
committed
shut down dht instead of removing
1 parent 2738661 commit 69798ef

File tree

1 file changed

+32
-15
lines changed

1 file changed

+32
-15
lines changed

p2p/communication.go

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ import (
1010
"time"
1111

1212
libp2p "github.com/libp2p/go-libp2p"
13+
dht "github.com/libp2p/go-libp2p-kad-dht"
1314
"github.com/libp2p/go-libp2p/core/crypto"
1415
"github.com/libp2p/go-libp2p/core/host"
1516
"github.com/libp2p/go-libp2p/core/network"
1617
"github.com/libp2p/go-libp2p/core/peer"
1718
"github.com/libp2p/go-libp2p/core/protocol"
19+
discovery_routing "github.com/libp2p/go-libp2p/p2p/discovery/routing"
20+
discovery_util "github.com/libp2p/go-libp2p/p2p/discovery/util"
1821
rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
1922
"github.com/libp2p/go-libp2p/p2p/net/connmgr"
2023
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
@@ -252,7 +255,7 @@ func (c *Communication) bootStrapConnectivityCheck() error {
252255
}
253256

254257
func (c *Communication) startChannel(privKeyBytes []byte) error {
255-
// ctx := context.Background()
258+
ctx := context.Background()
256259
p2pPriKey, err := crypto.UnmarshalSecp256k1PrivateKey(privKeyBytes)
257260
if err != nil {
258261
c.logger.Error().Msgf("error is %f", err)
@@ -317,18 +320,18 @@ func (c *Communication) startChannel(privKeyBytes []byte) error {
317320
c.host = h
318321
c.logger.Info().Msgf("Host created, we are: %s, at: %s", h.ID(), h.Addrs())
319322
h.SetStreamHandler(TSSProtocolID, c.handleStream)
320-
// // Start a DHT, for use in peer discovery. We can't just make a new DHT
321-
// // client because we want each peer to maintain its own local copy of the
322-
// // DHT, so that the bootstrapping node of the DHT can go down without
323-
// // inhibiting future peer discovery.
324-
// kademliaDHT, err := dht.New(ctx, h, dht.Mode(dht.ModeServer))
325-
// if err != nil {
326-
// return fmt.Errorf("fail to create DHT: %w", err)
327-
// }
328-
// c.logger.Debug().Msg("Bootstrapping the DHT")
329-
// if err = kademliaDHT.Bootstrap(ctx); err != nil {
330-
// return fmt.Errorf("fail to bootstrap DHT: %w", err)
331-
// }
323+
// Start a DHT, for use in peer discovery. We can't just make a new DHT
324+
// client because we want each peer to maintain its own local copy of the
325+
// DHT, so that the bootstrapping node of the DHT can go down without
326+
// inhibiting future peer discovery.
327+
kademliaDHT, err := dht.New(ctx, h, dht.Mode(dht.ModeServer))
328+
if err != nil {
329+
return fmt.Errorf("fail to create DHT: %w", err)
330+
}
331+
c.logger.Debug().Msg("Bootstrapping the DHT")
332+
if err = kademliaDHT.Bootstrap(ctx); err != nil {
333+
return fmt.Errorf("fail to bootstrap DHT: %w", err)
334+
}
332335

333336
var connectionErr error
334337
for i := 0; i < 5; i++ {
@@ -345,8 +348,22 @@ func (c *Communication) startChannel(privKeyBytes []byte) error {
345348

346349
// We use a rendezvous point "meet me here" to announce our location.
347350
// This is like telling your friends to meet you at the Eiffel Tower.
348-
// routingDiscovery := discovery_routing.NewRoutingDiscovery(kademliaDHT)
349-
// discovery_util.Advertise(ctx, routingDiscovery, c.rendezvous)
351+
routingDiscovery := discovery_routing.NewRoutingDiscovery(kademliaDHT)
352+
discovery_util.Advertise(ctx, routingDiscovery, c.rendezvous)
353+
354+
// Create a goroutine to shut down the DHT after 5 minutes
355+
go func() {
356+
select {
357+
case <-time.After(5 * time.Minute):
358+
c.logger.Info().Msg("Closing Kademlia DHT after 5 minutes")
359+
if err := kademliaDHT.Close(); err != nil {
360+
c.logger.Error().Err(err).Msg("Failed to close Kademlia DHT")
361+
}
362+
case <-ctx.Done():
363+
c.logger.Info().Msg("Context done, not waiting for 5 minutes to close DHT")
364+
}
365+
}()
366+
350367
err = c.bootStrapConnectivityCheck()
351368
if err != nil {
352369
return err

0 commit comments

Comments
 (0)