From b4ecf8b0aab12f43eb6e886a6e927f36d6df39a0 Mon Sep 17 00:00:00 2001 From: Trolli Schmittlauch Date: Wed, 27 May 2020 19:10:45 +0200 Subject: [PATCH] catch and handle more join errors --- src/Hash2Pub/DHTProtocol.hs | 9 +++++---- src/Hash2Pub/FediChord.hs | 4 ++-- src/Hash2Pub/Main.hs | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Hash2Pub/DHTProtocol.hs b/src/Hash2Pub/DHTProtocol.hs index 1adcdc1..34423a4 100644 --- a/src/Hash2Pub/DHTProtocol.hs +++ b/src/Hash2Pub/DHTProtocol.hs @@ -23,10 +23,10 @@ module Hash2Pub.DHTProtocol where import Control.Concurrent.Async -import Control.Exception import Control.Concurrent.STM import Control.Concurrent.STM.TBQueue import Control.Concurrent.STM.TQueue +import Control.Exception import Control.Monad (foldM, forM, forM_) import qualified Data.ByteString as BS import Data.Either (rights) @@ -146,7 +146,7 @@ markCacheEntryAsVerified timestamp = Map.adjust adjustFunc -- | send a join request and return the joined 'LocalNodeState' including neighbours requestJoin :: NodeState a => a -- ^ currently responsible node to be contacted -> LocalNodeState -- ^ joining NodeState - -> IO (Maybe LocalNodeState) -- ^ node after join with all its new information + -> IO (Either String LocalNodeState) -- ^ node after join with all its new information requestJoin toJoinOn ownState = bracket (mkSendSocket (getDomain toJoinOn) (getDhtPort toJoinOn)) close (\sock -> do responses <- sendRequestTo 5000 3 (\rid -> Request rid (toRemoteNodeState ownState) 1 1 Join (Just JoinRequestPayload)) sock @@ -166,10 +166,11 @@ requestJoin toJoinOn ownState = (setPredecessors [] . setSuccessors [] $ ownState) responses if responses == Set.empty - then pure Nothing + then pure . Left $ "join error: got no response from " <> show (getNid toJoinOn) -- sort successors and predecessors - else pure . Just . setSuccessors (sortBy localCompare $ successors joinedStateUnsorted) . setPredecessors (sortBy localCompare $ predecessors joinedStateUnsorted) $ joinedStateUnsorted + else pure . Right . setSuccessors (sortBy localCompare $ successors joinedStateUnsorted) . setPredecessors (sortBy localCompare $ predecessors joinedStateUnsorted) $ joinedStateUnsorted ) + `catch` (\e -> pure . Left $ displayException (e :: IOException)) -- | Send a 'QueryID' 'Request' for getting the node that handles a certain key ID. diff --git a/src/Hash2Pub/FediChord.hs b/src/Hash2Pub/FediChord.hs index d159009..76ab1a2 100644 --- a/src/Hash2Pub/FediChord.hs +++ b/src/Hash2Pub/FediChord.hs @@ -154,8 +154,8 @@ fediChordJoin cacheSnapshot ns = do -- 2. then send a join to the currently responsible node joinResult <- requestJoin currentlyResponsible ns case joinResult of - Nothing -> pure . Left $ "Error joining on " <> show currentlyResponsible - Just joinedNS -> pure . Right $ joinedNS + Left err -> pure . Left $ "Error joining on " <> err + Right joinedNS -> pure . Right $ joinedNS -- | cache updater thread that waits for incoming NodeCache update instructions on diff --git a/src/Hash2Pub/Main.hs b/src/Hash2Pub/Main.hs index fb58ad5..4ab3a48 100644 --- a/src/Hash2Pub/Main.hs +++ b/src/Hash2Pub/Main.hs @@ -2,9 +2,9 @@ module Main where import Control.Concurrent import Control.Exception +import Data.Either import Data.IP (IPv6, toHostAddress6) import System.Environment -import Data.Either import Hash2Pub.FediChord