Mainline DHT is an implementation of kademlia, see the document for more information.
From the 8 nodes you received, sort them by node -id proximity to your own identifier, and then send find_node to the top 3 (3 closest to you). Then you get another 8 x more nodes, paste them into your node list, still sorted by how narrow the nodes are to you. Keep sending find_node messages to the top 3 nodes (ignoring the messages you have already sent) until the nodes you return are already in your list. that is, the termination condition is that you sent a message to all 8 nodes closest to you (at the top of the list).
As explained in the article, the XOR distance metric. To calculate how far your node-ID is from another node, you XOR node-IDs. The lower the result, the closer the nodes are to each other.
In real life, you can make this a little more complicated by storing 3 outstanding queries at any given time and temporarily opening more outstanding queries halfway through timeouts.
source share