Skip to content

A peer-to-peer stack for code collaboration

Use MonadUnliftIO for Daemon

Thomas Scholtesff55a502e1d4
diff --git a/exe/Daemon.hs b/exe/Daemon.hs
index 6c93b0e..078803a 100644
--- a/exe/Daemon.hs
+++ b/exe/Daemon.hs
@@ -369,20 +369,19 @@ insertNewMachine id m = do
-- machine isn't in the cache already.
modifyMachine :: forall a. MachineId -> (Machine -> Daemon (Machine, a)) -> Daemon a
modifyMachine id f = do
- env <- ask
- res <- liftIO $ CMap.modifyExistingValue id (getMachines (machines env)) (modCached env)
+ CachedMachines machineMap <- asks machines
+ res <- CMap.modifyExistingValue id machineMap modCached
case res of
- Nothing -> throwError (MachineError id MachineNotCached)
- Just (Left err) -> throwError err
- Just (Right y) -> pure y
+ Nothing -> throwError (MachineError id MachineNotCached)
+ Just a -> pure a
where
- modCached :: Env -> CachedMachine -> IO (CachedMachine, Either Error a)
- modCached _ u@(UninitialisedReader _) = pure (u, Left (MachineError id MachineNotCached))
- modCached env (Cached m) = do
- x <- runDaemon env (f m)
- pure $ case x of
- Left err -> (Cached m, Left err)
- Right (m', y) -> (Cached m', Right y)
+ modCached :: CachedMachine -> Daemon (CachedMachine, a)
+ modCached = \case
+ UninitialisedReader _ -> throwError $ MachineError id MachineNotCached
+ Cached m -> do
+ (m', a) <- f m
+ pure (Cached m', a)
+
emptyMachine :: MachineId -> ReaderOrWriter -> TopicSubscription -> IO Machine
emptyMachine id mode sub = do
diff --git a/package.yaml b/package.yaml
index 899e1a6..b2f641b 100644
--- a/package.yaml
+++ b/package.yaml
@@ -68,6 +68,7 @@ library:
- template-haskell
- text
- time
+ - unliftio-core
- unordered-containers
- uuid
- wreq
diff --git a/src/Radicle/Daemon/Monad.hs b/src/Radicle/Daemon/Monad.hs
index 3523d27..0b9dc12 100644
--- a/src/Radicle/Daemon/Monad.hs
+++ b/src/Radicle/Daemon/Monad.hs
@@ -15,7 +15,11 @@ module Radicle.Daemon.Monad
, displayError
) where
-import Protolude
+import Protolude hiding (try)
+
+import Control.Exception.Safe
+import Control.Monad.Except
+import Control.Monad.IO.Unlift
import Radicle (LangError, Value, renderCompactPretty)
import Radicle.Daemon.Common
@@ -27,6 +31,16 @@ import Radicle.Ipfs
newtype Daemon a = Daemon (ExceptT Error (ReaderT Env IO) a)
deriving (Functor, Applicative, Monad, MonadError Error, MonadIO, MonadReader Env)
+instance MonadUnliftIO Daemon where
+ withRunInIO inner = do
+ env <- ask
+ result <- liftIO $ try $ inner $ \d -> do
+ result <- runDaemon env d
+ case result of
+ Left err -> throw err
+ Right a -> pure a
+ liftEither result
+
runDaemon :: Env -> Daemon a -> IO (Either Error a)
runDaemon env (Daemon x) = runReaderT (runExceptT x) env
@@ -56,12 +70,18 @@ data MachineError
| AckTimeout
| DaemonError Text
| MachineNotCached
+ deriving (Show)
+
+instance Exception MachineError
-- | An error that the daemon can encounter.
data Error
= MachineError MachineId MachineError
| CouldNotCreateMachine IpfsException
| IpfsDaemonNotReachable
+ deriving (Show)
+
+instance Exception Error
displayError :: Error -> (Text, [(Text,Text)])
displayError = \case
diff --git a/src/Radicle/Internal/ConcurrentMap.hs b/src/Radicle/Internal/ConcurrentMap.hs
index 6ef5c28..64f6928 100644
--- a/src/Radicle/Internal/ConcurrentMap.hs
+++ b/src/Radicle/Internal/ConcurrentMap.hs
@@ -11,6 +11,7 @@ module Radicle.Internal.ConcurrentMap
import Protolude hiding (empty)
+import Control.Monad.IO.Unlift
import qualified Data.Map.Strict as Map
-- | A Map which offers atomic operations on the values associated to
@@ -52,28 +53,28 @@ insert k v (CMap m_) = modifyMVar m_ $ \m -> do
-- | Atomically modifies a value associated to a key but only if it
-- exists. There is no guarantee the value is still associated with
-- the same key, or any key, by the time the operation completes.
-modifyExistingValue :: Ord k => k -> CMap k v -> (v -> IO (v, a)) -> IO (Maybe a)
-modifyExistingValue k (CMap m_) f = do
- m <- readMVar m_
+modifyExistingValue :: (MonadUnliftIO m, Ord k) => k -> CMap k v -> (v -> m (v, a)) -> m (Maybe a)
+modifyExistingValue k (CMap m_) f = withRunInIO $ \runInIO -> do
+ m <- liftIO $ readMVar m_
case Map.lookup k m of
Nothing -> pure Nothing
Just v_ -> do
- x <- modifyMVar v_ f
+ x <- modifyMVar v_ (runInIO . f)
pure (Just x)
-- | Atomically modifies the value associated to a key.
-modifyValue :: Ord k => k -> CMap k v -> (Maybe v -> IO (Maybe v, a)) -> IO a
-modifyValue k (CMap m_) f = modifyMVar m_ $ \m ->
+modifyValue :: (MonadUnliftIO m, Ord k) => k -> CMap k v -> (Maybe v -> m (Maybe v, a)) -> m a
+modifyValue k (CMap m_) f = withRunInIO $ \runInIO -> modifyMVar m_ $ \m ->
case Map.lookup k m of
Nothing -> do
- (maybev, x) <- f Nothing
+ (maybev, x) <- runInIO $ f Nothing
case maybev of
Just v -> do
v_ <- newMVar v
pure (Map.insert k v_ m, x)
Nothing -> pure (m, x)
Just v_ -> modifyMVar v_ $ \v -> do
- (maybev', x) <- f (Just v)
+ (maybev', x) <- runInIO $ f (Just v)
case maybev' of
Just v' -> pure (v', (m, x))
Nothing -> pure (v, (Map.delete k m, x))