From 7f1eb53d34ea6dda02cae1934b5011e38de248a6 Mon Sep 17 00:00:00 2001 From: joe Date: Thu, 20 Jul 2017 05:18:31 -0400 Subject: comments --- src/Network/BitTorrent/DHT/Search.hs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'src/Network/BitTorrent/DHT') diff --git a/src/Network/BitTorrent/DHT/Search.hs b/src/Network/BitTorrent/DHT/Search.hs index 91a1079b..c562b988 100644 --- a/src/Network/BitTorrent/DHT/Search.hs +++ b/src/Network/BitTorrent/DHT/Search.hs @@ -33,8 +33,12 @@ data IterativeSearch nid addr ni r = IterativeSearch , searchSpace :: KademliaSpace nid ni , searchNodeAddress :: ni -> addr , searchQuery :: ni -> IO ([ni], [r]) + -- | The number of pending queries. Incremented before any query is sent + -- and decremented when we get a reply. , searchPendingCount :: TVar Int + -- | Nodes scheduled to be queried. , searchQueued :: TVar (MinMaxPSQ ni nid) + -- | The nearest K nodes that issued a reply. , searchInformant :: TVar (MinMaxPSQ ni nid) , searchVisited :: TVar (Set addr) , searchResults :: TVar (Set r) @@ -127,11 +131,17 @@ search s@IterativeSearch{..} = withTaskGroup searchAlpha $ \g -> do found <- MM.minView <$> readTVar searchQueued case found of Just (ni :-> d, q) - | (MM.size informants < searchK) && (cnt > 0 || not (MM.null q)) + | -- If there's fewer than /k/ informants and there's any + -- node we haven't yet got a response from. + (MM.size informants < searchK) && (cnt > 0 || not (MM.null q)) + -- Or if the closest scheduled node is nearer than the + -- nearest /k/ informants. || (PSQ.prio (fromJust $ MM.findMax informants) > d) - -> do writeTVar searchQueued q + -> -- Then the search continues, send a query. + do writeTVar searchQueued q modifyTVar searchVisited $ Set.insert (searchNodeAddress ni) modifyTVar searchPendingCount succ return $ withAsync g (sendQuery s (ni :-> d)) (const again) - _ -> do check (cnt == 0) + _ -> -- Otherwise, we are finished. + do check (cnt == 0) return $ return () -- cgit v1.2.3