Skip to content

A peer-to-peer stack for code collaboration

Simplify handling of pubsub message parse errors

Thomas Scholtes106eed667d9e
diff --git a/exe/Daemon.hs b/exe/Daemon.hs
index 9f09dc5..1f529c0 100644
--- a/exe/Daemon.hs
+++ b/exe/Daemon.hs
@@ -154,7 +154,7 @@ init follows = traverse_ initMachine (Map.toList follows)
newMachine :: Daemon Api.NewResponse
newMachine = do
id <- ipfs createMachine CouldNotCreateMachine
- sub <- machineIpfs id $ initSubscription id
+ sub <- initDaemonSubscription id
logInfo Normal "Created new IPFS machine" [("machine-id", getMachineId id)]
m <- liftIO $ emptyMachine id Writer sub
insertNewMachine id (Cached m)
@@ -205,12 +205,12 @@ send id (Api.SendRequest expressions) = do
results <- writeInputs id expressions Nothing
pure Api.SendResponse{..}
where
+ matchMessage nonce = \case
+ (New InputsApplied{results, nonce = Just nonce'}) | nonce' == nonce -> Just results
+ _ -> Nothing
requestInput sub = do
nonce <- liftIO $ UUID.uuid
- let isResponse = \case
- New InputsApplied{ nonce = Just nonce' } | nonce' == nonce -> True
- _ -> False
- asyncMsg <- liftIO $ async $ subscribeOne sub ackWaitTime isResponse (logNonDecodableMsg id)
+ asyncMsg <- liftIO $ async $ subscribeOne sub ackWaitTime (matchMessage nonce)
machineIpfs id $ publish id (Submit SubmitInputs{..})
logInfo Debug
"Sent input request to writer"
@@ -218,7 +218,7 @@ send id (Api.SendRequest expressions) = do
, ("expressions", prValues expressions) ]
msg_ <- machineIpfs id $ wait asyncMsg
case msg_ of
- Just (New InputsApplied{results}) -> do
+ Just results -> do
logInfo Normal
"Writer accepted input request"
[ ("machine-id", getMachineId id)
@@ -226,7 +226,6 @@ send id (Api.SendRequest expressions) = do
]
bumpPolling id
pure Api.SendResponse{..}
- Just _ -> throwError $ MachineError id (DaemonError "Didn't filter machine topic messages correctly.")
Nothing -> throwError $ MachineError id AckTimeout
@@ -274,15 +273,20 @@ checkMachineLoaded id = do
-- then pure m
-- else refreshAsReader id
-logNonDecodableMsg :: MachineId -> Text -> IO ()
-logNonDecodableMsg (MachineId id) bad =
- Common.logInfo "Non-decodable message on machine's pubsub topic" [("machine-id", id), ("message", bad)]
+-- | Creates a IPFS pusbsub subscription for the given machine. This
+-- wraps 'initSubscription' and logs all message parsing errors.
+initDaemonSubscription :: MachineId -> Daemon TopicSubscription
+initDaemonSubscription id =
+ machineIpfs id $ initSubscription id logNonDecodableMsg
+ where
+ logNonDecodableMsg bad =
+ Common.logInfo "Non-decodable message on machine's pubsub topic"
+ [("machine-id", getMachineId id), ("message", bad)]
-- | Turns a 'Daemon' subscription handler into an IO one. Errors which are
-- encountered in a subscription handler are just logged.
-daemonHandler :: Env -> MachineId -> (Message -> Daemon ()) -> Either Text Message -> IO ()
-daemonHandler _ id _ (Left bad) = logNonDecodableMsg id bad
-daemonHandler env _ h (Right msg) = do
+daemonHandler :: Env -> (Message -> Daemon ()) -> Message -> IO ()
+daemonHandler env h msg = do
x_ <- runDaemon env (h msg)
case x_ of
Left err -> logDaemonError err
@@ -292,7 +296,7 @@ daemonHandler env _ h (Right msg) = do
loadMachine :: ReaderOrWriter -> MachineId -> Daemon Machine
loadMachine mode id = do
(idx, is) <- machineIpfs id $ machineInputsFrom id Nothing
- sub <- machineIpfs id $ initSubscription id
+ sub <- initDaemonSubscription id
m <- liftIO $ emptyMachine id mode sub
(m', _) <- addInputs is (pure idx) (const (pure ())) m
insertNewMachine id (Cached m')
@@ -389,7 +393,7 @@ initAsReader :: MachineId -> Daemon Machine
initAsReader id = do
m <- loadMachine Reader id
env <- ask
- _ <- liftIO $ addHandler (machineSubscription m) (daemonHandler env id onMsg)
+ _ <- liftIO $ addHandler (machineSubscription m) (daemonHandler env onMsg)
logInfo Normal "Following as reader" [ ("machine-id", getMachineId id)
, ("current-input-index", show (machineLastIndex m)) ]
pure m
@@ -457,7 +461,7 @@ initAsWriter id idx = do
actAsWriter :: Machine -> Daemon ()
actAsWriter m = do
env <- ask
- _ <- liftIO $ addHandler (machineSubscription m) (daemonHandler env id onMsg)
+ _ <- liftIO $ addHandler (machineSubscription m) (daemonHandler env onMsg)
logInfo Normal "Acting as writer" [("machine-id", getMachineId id)]
where
id = machineId m
diff --git a/src/Radicle/Daemon/Ipfs.hs b/src/Radicle/Daemon/Ipfs.hs
index 39e3e9c..3fd724b 100644
--- a/src/Radicle/Daemon/Ipfs.hs
+++ b/src/Radicle/Daemon/Ipfs.hs
@@ -28,6 +28,7 @@ import qualified Data.Aeson.Types as Aeson
import qualified Data.Map.Strict as Map
import qualified Data.Sequence as Seq
import qualified Data.Unique as Unique
+import System.Timeout
import Radicle.Internal.Core
import qualified Radicle.Internal.MachineBackend.Ipfs as Ipfs
@@ -121,7 +122,7 @@ publish :: MachineId -> Message -> IO ()
publish id msg = Ipfs.publish (getTopic (machineTopic id)) (Aeson.encode msg)
-- | Subscribe to messages on a machine's IPFS pubsub topic.
-subscribeForever :: MachineId -> MsgHandler -> IO ()
+subscribeForever :: MachineId -> (Either Text Message -> IO ()) -> IO ()
subscribeForever id messageHandler = Ipfs.subscribe topic pubsubHandler
where
Topic topic = machineTopic id
@@ -148,7 +149,7 @@ ipnsPublish id (Ipfs.MachineEntryIndex cid) = Ipfs.namePublish (getMachineId id)
-- * Topic subscriptions
-type MsgHandler = Either Text Message -> IO ()
+type MsgHandler = Message -> IO ()
type RegisteredHandler = Unique.Unique
@@ -159,15 +160,20 @@ newtype TopicSubscription = TopicSubscription
-- | Given a machine ID, creates a subscription for that machine's
-- pubsub topic. This allows adding and removing handlers for messages
-- on that topic.
-initSubscription :: MachineId -> IO TopicSubscription
-initSubscription id = do
+--
+-- @logParseError@ is called with the raw message content if a message
+-- cannot be parsed.
+initSubscription :: MachineId -> (Text -> IO ()) -> IO TopicSubscription
+initSubscription id logParseError = do
hdlrs <- newMVar Map.empty
_ <- async $ subscribeForever id (mainHdlr hdlrs)
pure $ TopicSubscription hdlrs
where
- mainHdlr hdlrs msg = do
- hs <- readMVar hdlrs
- traverse_ ($ msg) (Map.elems hs)
+ mainHdlr hdlrs = \case
+ Left rawMessage -> logParseError rawMessage
+ Right msg -> do
+ hs <- readMVar hdlrs
+ traverse_ ($ msg) (Map.elems hs)
-- | Add a message handler to a topic subscription.
addHandler :: TopicSubscription -> MsgHandler -> IO RegisteredHandler
@@ -181,21 +187,20 @@ removeHandler :: TopicSubscription -> RegisteredHandler -> IO ()
removeHandler ts u =
modifyMVar (handlers ts) (pure . (,()) . Map.delete u)
--- | Block while waiting for a message which matches a
--- predicate. Returns @Just msg@ where @msg@ is the first message
--- passing the predicate if such a message arrives before the
--- specified amount of milliseconds. Otherwise, returns @Nothing@.
-subscribeOne :: TopicSubscription -> Int64 -> (Message -> Bool) -> (Text -> IO ()) -> IO (Maybe Message)
-subscribeOne sub timeout pr badMsg = do
- var <- newEmptyMVar
- let onMsg = \case
- Right msg | pr msg -> putMVar var (Just msg)
- Right _ -> pure ()
- Left bad -> badMsg bad
- bracket
- (addHandler sub onMsg)
- (removeHandler sub)
- (const $ forkIO (threadDelay (fromIntegral timeout * 1000) >> putMVar var Nothing) *> readMVar var)
+-- | Block while waiting for a matching message.
+-- If no matching message arrives within the given timeout (in
+-- milliseconds) then @Nothing@ is retuend.
+subscribeOne :: TopicSubscription -> Int64 -> (Message -> Maybe a) -> IO (Maybe a)
+subscribeOne subscription t matchMessage = do
+ timeout (fromIntegral t * 1000) $ do
+ msgVar <- newEmptyMVar
+ let onMsg msg = case matchMessage msg of
+ Just a -> putMVar msgVar a
+ Nothing -> pure ()
+ bracket
+ (addHandler subscription onMsg)
+ (removeHandler subscription)
+ (\_ -> readMVar msgVar)
emptyMachineEntryIndex :: Ipfs.MachineEntryIndex
emptyMachineEntryIndex = Ipfs.MachineEntryIndex Ipfs.emptyMachineCid