A peer-to-peer stack for code collaboration
Use MonadUnliftIO for Daemon
Thomas Scholtesff55a502e1d4
diff --git a/exe/Daemon.hs b/exe/Daemon.hsindex 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 amodifyMachine id f = do- env <- ask- res <- liftIO $ CMap.modifyExistingValue id (getMachines (machines env)) (modCached env)+ CachedMachines machineMap <- asks machines+ res <- CMap.modifyExistingValue id machineMap modCachedcase 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 awhere- 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 MachineemptyMachine id mode sub = dodiff --git a/package.yaml b/package.yamlindex 899e1a6..b2f641b 100644--- a/package.yaml+++ b/package.yaml@@ -68,6 +68,7 @@ library:- template-haskell- text- time+ - unliftio-core- unordered-containers- uuid- wreqdiff --git a/src/Radicle/Daemon/Monad.hs b/src/Radicle/Daemon/Monad.hsindex 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.Unliftimport Radicle (LangError, Value, renderCompactPretty)import Radicle.Daemon.Common@@ -27,6 +31,16 @@ import Radicle.Ipfsnewtype 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 ErrordisplayError :: Error -> (Text, [(Text,Text)])displayError = \casediff --git a/src/Radicle/Internal/ConcurrentMap.hs b/src/Radicle/Internal/ConcurrentMap.hsindex 6ef5c28..64f6928 100644--- a/src/Radicle/Internal/ConcurrentMap.hs+++ b/src/Radicle/Internal/ConcurrentMap.hs@@ -11,6 +11,7 @@ module Radicle.Internal.ConcurrentMapimport Protolude hiding (empty)+import Control.Monad.IO.Unliftimport 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 ofNothing -> pure NothingJust 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 ofNothing -> do- (maybev, x) <- f Nothing+ (maybev, x) <- runInIO $ f Nothingcase maybev ofJust v -> dov_ <- newMVar vpure (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' ofJust v' -> pure (v', (m, x))Nothing -> pure (v, (Map.delete k m, x))