Skip to content

Commit

Permalink
remote: use DList Logger
Browse files Browse the repository at this point in the history
instead of slow-to-append-to list. Thanks for the suggestion!

Closes #63.

Co-Authored-By: Travis Whitaker <pi.boy.travis@gmail.com>
  • Loading branch information
sorki and TravisWhitaker committed Dec 3, 2023
1 parent 58011db commit 2832079
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 8 deletions.
3 changes: 2 additions & 1 deletion docs/01-Contributors.org
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions hnix-store-remote/hnix-store-remote.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion hnix-store-remote/src/System/Nix/Store/Remote.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion hnix-store-remote/src/System/Nix/Store/Remote/Client.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions hnix-store-remote/src/System/Nix/Store/Remote/MonadStore.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 }
Expand Down
5 changes: 3 additions & 2 deletions hnix-store-remote/tests-io/NixDaemon.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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" []
Expand All @@ -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

Expand Down

0 comments on commit 2832079

Please sign in to comment.