diff options
Diffstat (limited to 'Kademlia.hs')
-rw-r--r-- | Kademlia.hs | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/Kademlia.hs b/Kademlia.hs index fdc1fdd2..4920467a 100644 --- a/Kademlia.hs +++ b/Kademlia.hs | |||
@@ -270,10 +270,11 @@ refreshBucket :: forall nid ni addr. | |||
270 | Search nid addr ni ni -> TVar (BucketList ni) -> nid -> Int -> IO () | 270 | Search nid addr ni ni -> TVar (BucketList ni) -> nid -> Int -> IO () |
271 | refreshBucket sch var nid n = do | 271 | refreshBucket sch var nid n = do |
272 | tbl <- atomically (readTVar var) | 272 | tbl <- atomically (readTVar var) |
273 | sample <- if n+1 == R.defaultBucketCount -- Is this the last bucket? | 273 | let count = bktCount tbl |
274 | then return nid -- Yes? Search our own id. | 274 | sample <- if n+1 >= count -- Is this the last bucket? |
275 | else genBucketSample nid -- No? Generate a random id. | 275 | then return nid -- Yes? Search our own id. |
276 | (bucketRange n (n + 1 < bktCount tbl)) | 276 | else genBucketSample nid -- No? Generate a random id. |
277 | (bucketRange n (n + 1 < count)) | ||
277 | resultCounter <- atomically $ newTVar Set.empty | 278 | resultCounter <- atomically $ newTVar Set.empty |
278 | let fullcount = R.defaultBucketSize | 279 | let fullcount = R.defaultBucketSize |
279 | let checkBucketFull :: ni -> STM Bool | 280 | let checkBucketFull :: ni -> STM Bool |
@@ -315,7 +316,7 @@ bootstrap :: | |||
315 | bootstrap sch var ping ns ns0 = do | 316 | bootstrap sch var ping ns ns0 = do |
316 | -- First, ping the given nodes so that they are added to | 317 | -- First, ping the given nodes so that they are added to |
317 | -- our routing table. | 318 | -- our routing table. |
318 | withTaskGroup 20 $ \g -> do | 319 | withTaskGroup 50 $ \g -> do |
319 | got_response <- or <$> mapConcurrently g ping ns | 320 | got_response <- or <$> mapConcurrently g ping ns |
320 | -- We resort to the hardcoded fallback nodes only when we got no | 321 | -- We resort to the hardcoded fallback nodes only when we got no |
321 | -- responses. This is to lesson the burden on well-known boostrap | 322 | -- responses. This is to lesson the burden on well-known boostrap |
@@ -323,13 +324,20 @@ bootstrap sch var ping ns ns0 = do | |||
323 | when (not got_response) $ do | 324 | when (not got_response) $ do |
324 | _ <- mapConcurrently g ping ns0 | 325 | _ <- mapConcurrently g ping ns0 |
325 | return () | 326 | return () |
327 | |||
328 | mvar <- newMVar () | ||
326 | -- Now run searches until all the buckets are full. On a small network, | 329 | -- Now run searches until all the buckets are full. On a small network, |
327 | -- this may never quit. | 330 | -- this may never quit. |
328 | -- | 331 | -- |
329 | -- TODO: For small networks, we should give up on filling a nearby bucket | 332 | -- TODO: For small networks, we should give up on filling a nearby bucket |
330 | -- at some point and move on to one farther away. | 333 | -- at some point and move on to one farther away. |
331 | fix $ \again -> do | 334 | fix $ \again -> do |
335 | takeMVar mvar | ||
332 | tbl <- atomically $ readTVar var | 336 | tbl <- atomically $ readTVar var |
337 | fork $ do | ||
338 | -- Force a delay in case the search returns too quickly | ||
339 | threadDelay (60 * 1000000) | ||
340 | putMVar mvar () | ||
333 | let shp = reverse $ zip (R.shape tbl) [0 .. ] | 341 | let shp = reverse $ zip (R.shape tbl) [0 .. ] |
334 | unfull = filter ( (< R.defaultBucketSize) . fst ) shp | 342 | unfull = filter ( (< R.defaultBucketSize) . fst ) shp |
335 | case dropWhile ((> R.defaultBucketCount - 1) . snd) unfull of | 343 | case dropWhile ((> R.defaultBucketCount - 1) . snd) unfull of |