Disconnecting / removing peers form the DHT and peerstore

Hi I have been banging my head against a wall with this now for a couple of days.

  • I have created a bootstrap node with DHT and peer discovery.

  • My peers are able to connect to the bootstrap node and discover more peers that they can connect to.

  • When I close / kill a peer I am unable to remove it form the peerStore or DHT.

  • From my other nodes and bootstrap node I am able to see that I am not connected to this peer anymore with

    myHost.Network().Connectedness(peer.ID)
    
  • I I have tried to use the following to remove the peer

						myKdht.RoutingTable().RemovePeer(peer.ID)
						err := myHost.Network().ClosePeer(peer.ID)
						if err != nil {
							fmt.Println("please handle me", err)
						}
						myHost.Peerstore().RemovePeer(peer.ID)

After this i am still able to see the peer in my peerStore and DHT/routing discovery

I have no idea what I am missing.

Remote peers stay in the routing table even if they go offline. The node doesn’t need to keep an open connection to all the remote peers that are in its routing table (it would be too much). It “refreshes” its routing table every 10 minutes.

A routing table “refresh” consists in probing all remote peers that are in the routing table, and verify that they are still online. If a remote peer fails to answer a probe during a refresh, it is evicted from the routing table.

So it is expected that a remote peer doesn’t immediately disappear from the routing table as soon as it goes offline, it should get flush within 10 minutes. However, myKdht.RoutingTable().RemovePeer(peer.ID) should remove the peer from the routing table (but not the peerstore though).

Thank you for the response.
What I am finding strange is that it is not getting removed routing table if I wait for 10+ minutes or try and remove it manually.
I am still able to pick it up via this here:

	peerChan, err := routingDiscovery.FindPeers(w.ctx, w.conf.Rendezvous)

When I have a bit of time I will post my code (a reduced version of it), this should be by tomorrow.

Why is this important to you? libp2p is designed to “loose” the peer information over time, gradually. It takes time for one peer to be “forgotten”.

In my case, I want to only allow peers to connect which are known to me.

Therefore, I am adding a custom handshake, a bit following Implement custom handshake - #7 by MOHANKUMAR-IT

However, if my custom handshake fails, the DHT module still reports being connected to the peer. if I check via host.Peerstore().Peers().

My disconnect code in case of a failed handshake does

	host.Network().ClosePeer(p)
	host.Network().Peerstore().RemovePeer(p)
	dht.RoutingTable().RemovePeer(p)

But still via the PeerStore the peer is there…

Am I thinking about this in a wrong way? I understand that the DHT is managing the mesh underneath, but I really want only my valid peers to connect…