diff --git a/src/Hash2Pub/FediChord.hs b/src/Hash2Pub/FediChord.hs index 27d662f..4936e7c 100644 --- a/src/Hash2Pub/FediChord.hs +++ b/src/Hash2Pub/FediChord.hs @@ -322,6 +322,34 @@ kChoicesDepartureCost remainingOwnLoad ownCap thisSegmentLoad segment = + abs (remainingOwnLoad + thisSegmentLoad) / ownCap - abs (segmentOwnerRemainingLoadTarget segment) / segmentOwnerCapacity segment + +kChoicesRebalanceThread :: (Service s (RealNodeSTM s)) => RealNodeSTM s -> IO () +kChoicesRebalanceThread nodeSTM = (confKChoicesRebalanceInterval . nodeConfig <$> readTVarIO nodeSTM) >>= rebalanceVS 0 + where + rebalanceVS :: NodeID -> Int -> IO () + rebalanceVS startAt interval = do + threadDelay interval + (vsToRebalance', serv) <- atomically $ do + node <- readTVar nodeSTM + pure (rMapLookupPred 0 (vservers node), nodeService node) + maybe + -- wait and retry if no active VS available + (rebalanceVS startAt interval) + (\(vsNid, vsSTM) -> do + vs <- readTVarIO vsSTM + -- query neighbour node(s) load + -- query own load + -- calculate departure cost + -- if deciding to re-balance, first leave and then join + -- loop + rebalanceVS vsNid interval + ) + vsToRebalance' + + + -- placeholder + pure () + -- | Join a new node into the DHT, using a provided bootstrap node as initial cache seed -- for resolving the new node's position. fediChordBootstrapJoin :: Service s (RealNodeSTM s)