From 28320794f044e3c89484abd0a30e2dad967e4000 Mon Sep 17 00:00:00 2001 From: sorki Date: Sun, 3 Dec 2023 16:51:55 +0100 Subject: [PATCH] remote: use DList Logger instead of slow-to-append-to list. Thanks for the suggestion! Closes #63. Co-Authored-By: Travis Whitaker --- docs/01-Contributors.org | 3 ++- hnix-store-remote/hnix-store-remote.cabal | 1 + hnix-store-remote/src/System/Nix/Store/Remote.hs | 2 +- hnix-store-remote/src/System/Nix/Store/Remote/Client.hs | 3 ++- .../src/System/Nix/Store/Remote/MonadStore.hs | 9 ++++++--- hnix-store-remote/tests-io/NixDaemon.hs | 5 +++-- 6 files changed, 15 insertions(+), 8 deletions(-) diff --git a/docs/01-Contributors.org b/docs/01-Contributors.org index 48d86fac..c3266f12 100644 --- a/docs/01-Contributors.org +++ b/docs/01-Contributors.org @@ -28,4 +28,5 @@ in order of appearance: + Luigy Leon @luigy + squalus @squalus + Vaibhav Sagar @vaibhavsagar -* Ryan Trinkle @ryantrinkle ++ Ryan Trinkle @ryantrinkle ++ Travis Whitaker @TravisWhitaker diff --git a/hnix-store-remote/hnix-store-remote.cabal b/hnix-store-remote/hnix-store-remote.cabal index e41b03ad..e07b2e0d 100644 --- a/hnix-store-remote/hnix-store-remote.cabal +++ b/hnix-store-remote/hnix-store-remote.cabal @@ -117,6 +117,7 @@ library , data-default-class , dependent-sum > 0.7 , dependent-sum-template > 0.1.1 && < 0.3 + , dlist >= 1.0 , generic-arbitrary < 1.1 , hashable , text diff --git a/hnix-store-remote/src/System/Nix/Store/Remote.hs b/hnix-store-remote/src/System/Nix/Store/Remote.hs index 0cd57e30..838f7186 100644 --- a/hnix-store-remote/src/System/Nix/Store/Remote.hs +++ b/hnix-store-remote/src/System/Nix/Store/Remote.hs @@ -121,7 +121,7 @@ runStoreOptsTCP host port sd code = do (Network.Socket.addrAddress sockAddr) sd code - _ -> pure (Left RemoteStoreError_GetAddrInfoFailed, []) + _ -> pure (Left RemoteStoreError_GetAddrInfoFailed, mempty) runStoreOpts' :: Family diff --git a/hnix-store-remote/src/System/Nix/Store/Remote/Client.hs b/hnix-store-remote/src/System/Nix/Store/Remote/Client.hs index 8df46a8b..7cdb5394 100644 --- a/hnix-store-remote/src/System/Nix/Store/Remote/Client.hs +++ b/hnix-store-remote/src/System/Nix/Store/Remote/Client.hs @@ -17,6 +17,7 @@ module System.Nix.Store.Remote.Client import Control.Monad (unless, when) import Control.Monad.Except (throwError) import Control.Monad.IO.Class (MonadIO, liftIO) +import Data.DList (DList) import Data.Serialize.Put (Put, runPut) import Data.Some (Some(Some)) @@ -157,7 +158,7 @@ addToStore name source method hashAlgo repair = do isValidPath :: MonadRemoteStore m => StorePath -> m Bool isValidPath = doReq . IsValidPath -type Run m a = m (Either RemoteStoreError a, [Logger]) +type Run m a = m (Either RemoteStoreError a, DList Logger) runStoreSocket :: ( Monad m diff --git a/hnix-store-remote/src/System/Nix/Store/Remote/MonadStore.hs b/hnix-store-remote/src/System/Nix/Store/Remote/MonadStore.hs index ef4662f7..2b51f96f 100644 --- a/hnix-store-remote/src/System/Nix/Store/Remote/MonadStore.hs +++ b/hnix-store-remote/src/System/Nix/Store/Remote/MonadStore.hs @@ -24,6 +24,7 @@ import Control.Monad.Trans.State.Strict (StateT, runStateT, mapStateT) import Control.Monad.Trans.Except (ExceptT, runExceptT, mapExceptT) import Control.Monad.Trans.Reader (ReaderT, runReaderT, withReaderT) import Data.ByteString (ByteString) +import Data.DList (DList) import Data.Word (Word64) import Network.Socket (Socket) import System.Nix.Nar (NarSource) @@ -33,8 +34,10 @@ import System.Nix.Store.Remote.Types.Logger (Logger) import System.Nix.Store.Remote.Types.ProtoVersion (HasProtoVersion(..), ProtoVersion) import System.Nix.Store.Remote.Types.StoreConfig (HasStoreSocket(..), StoreConfig) +import qualified Data.DList + data RemoteStoreState = RemoteStoreState { - remoteStoreState_logs :: [Logger] + remoteStoreState_logs :: DList Logger , remoteStoreState_gotError :: Bool , remoteStoreState_mDataSource :: Maybe (Word64 -> IO (Maybe ByteString)) -- ^ Source for @Logger_Read@, this will be called repeatedly @@ -121,7 +124,7 @@ runRemoteStoreT ) => r -> RemoteStoreT r m a - -> m (Either RemoteStoreError a, [Logger]) + -> m (Either RemoteStoreError a, DList Logger) runRemoteStoreT r = fmap (\(res, RemoteStoreState{..}) -> (res, remoteStoreState_logs)) . (`runReaderT` r) @@ -304,7 +307,7 @@ instance ( MonadIO m appendLog x = RemoteStoreT $ modify - $ \s -> s { remoteStoreState_logs = remoteStoreState_logs s ++ [x] } + $ \s -> s { remoteStoreState_logs = remoteStoreState_logs s `Data.DList.snoc` x } setError = RemoteStoreT $ modify $ \s -> s { remoteStoreState_gotError = True } clearError = RemoteStoreT $ modify $ \s -> s { remoteStoreState_gotError = False } diff --git a/hnix-store-remote/tests-io/NixDaemon.hs b/hnix-store-remote/tests-io/NixDaemon.hs index 1088cc02..9762e244 100644 --- a/hnix-store-remote/tests-io/NixDaemon.hs +++ b/hnix-store-remote/tests-io/NixDaemon.hs @@ -29,6 +29,7 @@ import System.Nix.Build import System.Nix.StorePath import System.Nix.StorePath.Metadata import System.Nix.Store.Remote +import System.Nix.Store.Remote.Client (Run) import System.Nix.Store.Remote.MonadStore (mapStoreConfig) import Crypto.Hash (SHA256) @@ -89,7 +90,7 @@ error: changing ownership of path '/run/user/1000/test-nix-store-06b0d249e561612 startDaemon :: FilePath - -> IO (P.ProcessHandle, MonadStore a -> IO (Either RemoteStoreError a, [Logger])) + -> IO (P.ProcessHandle, MonadStore a -> Run IO a) startDaemon fp = do writeConf (fp "etc" "nix.conf") p <- createProcessEnv fp "nix-daemon" [] @@ -110,7 +111,7 @@ enterNamespaces = do writeGroupMappings Nothing [GroupMapping 0 gid 1] True withNixDaemon - :: ((MonadStore a -> IO (Either RemoteStoreError a, [Logger])) -> IO a) -> IO a + :: ((MonadStore a -> Run IO a) -> IO a) -> IO a withNixDaemon action = withSystemTempDirectory "test-nix-store" $ \path -> do