From f8b548cd8020d7bf0000f0a1fffeed80b2317467 Mon Sep 17 00:00:00 2001 From: skosito Date: Tue, 22 Oct 2024 21:37:50 +0100 Subject: [PATCH] shut down dht after 5 mins (#32) --- p2p/communication.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/p2p/communication.go b/p2p/communication.go index 81ab547..1fb7640 100644 --- a/p2p/communication.go +++ b/p2p/communication.go @@ -338,6 +338,20 @@ func (c *Communication) startChannel(privKeyBytes []byte) error { // This is like telling your friends to meet you at the Eiffel Tower. routingDiscovery := discovery_routing.NewRoutingDiscovery(kademliaDHT) discovery_util.Advertise(ctx, routingDiscovery, c.rendezvous) + + // Create a goroutine to shut down the DHT after 5 minutes + go func() { + select { + case <-time.After(5 * time.Minute): + c.logger.Info().Msg("Closing Kademlia DHT after 5 minutes") + if err := kademliaDHT.Close(); err != nil { + c.logger.Error().Err(err).Msg("Failed to close Kademlia DHT") + } + case <-ctx.Done(): + c.logger.Info().Msg("Context done, not waiting for 5 minutes to close DHT") + } + }() + err = c.bootStrapConnectivityCheck() if err != nil { return err