theatre-dev 0.2 → 0.3
raw patch · 4 files changed
+21/−21 lines, 4 files
Files
- hspec/TheatreDevSpec.hs +4/−4
- library/TheatreDev.hs +14/−14
- library/TheatreDev/Tell.hs +2/−2
- theatre-dev.cabal +1/−1
hspec/TheatreDevSpec.hs view
@@ -116,10 +116,10 @@ results === sort (concat (replicate (size * Preferences.concurrency) messages)) ] - describe "oneOf" . modifyMaxSuccess (max 1000) $ do+ describe "firstAvailableOneOf" . modifyMaxSuccess (max 1000) $ do prop "Dispatches correctly" $ forAll (chooseInt (0, 99)) $ \size -> forAll arbitrary $ \(messages :: [Int]) -> idempotentIOProperty do- results <- sort . concat <$> IO.simulateReduction Preferences.concurrency size Actor.oneOf messages+ results <- sort . concat <$> IO.simulateReduction Preferences.concurrency size Actor.firstAvailableOneOf messages return $ conjoin [ length results === length messages * size,@@ -127,11 +127,11 @@ === sort (concat (replicate (size) messages)) ] - describe "byKeyHash" . modifyMaxSuccess (max Preferences.largePropertyMaxSuccess) $ do+ describe "byKeyHashOneOf" . modifyMaxSuccess (max Preferences.largePropertyMaxSuccess) $ do prop "Dispatches individually" $ forAll (chooseInt (0, 99)) $ \size -> forAll arbitrary $ \(messages :: [Int]) -> idempotentIOProperty $ do resultsVar <- newTVarIO [] actor <-- fmap (Actor.byKeyHash id)+ fmap (Actor.byKeyHashOneOf id) $ replicateM size $ Actor.spawnStatefulIndividual IntMap.empty
library/TheatreDev.hs view
@@ -13,9 +13,9 @@ wait, -- * Composition- oneOf,+ firstAvailableOneOf,+ byKeyHashOneOf, allOf,- byKeyHash, ) where @@ -33,7 +33,7 @@ -- Provides abstraction over the message channel, thread-forking and killing. -- -- Monoid instance is not provided for the same reason it is not provided for numbers.--- This type supports both sum and product composition. See 'allOf' and 'oneOf'.+-- This type supports both sum and product composition. See 'allOf', 'firstAvailableOneOf' and 'byKeyHashOneOf'. data Actor message = Actor { -- | Send a message to the actor. tell :: message -> STM (),@@ -91,17 +91,11 @@ -- -- > spawnPool :: Int -> IO (Actor message) -> IO (Actor message) -- > spawnPool size spawn =--- > oneOf <$> replicateM size spawn+-- > firstAvailableOneOf <$> replicateM size spawn -- -- You can consider this being an interface to the Sum monoid.-oneOf :: [Actor message] -> Actor message-oneOf = tellComposition Tell.one---- | Distribute the message stream to all provided actors.------ You can consider this being an interface to the Product monoid.-allOf :: [Actor message] -> Actor message-allOf = tellComposition Tell.all+firstAvailableOneOf :: [Actor message] -> Actor message+firstAvailableOneOf = tellComposition Tell.one -- | -- Dispatch the message across actors based on a key hash.@@ -114,13 +108,19 @@ -- of actors to the hash and thus determines the index -- of the actor to dispatch the message to. -- This is inspired by how partitioning is done in Kafka.-byKeyHash ::+byKeyHashOneOf :: -- | Function extracting the key from the message and hashing it. (message -> Int) -> -- | Pool of actors. [Actor message] -> Actor message-byKeyHash = tellComposition . Tell.byKeyHash+byKeyHashOneOf = tellComposition . Tell.byKeyHashOneOf++-- | Distribute the message stream to all provided actors.+--+-- You can consider this being an interface to the Product monoid.+allOf :: [Actor message] -> Actor message+allOf = tellComposition Tell.all tellComposition :: ([Tell message] -> Tell message) -> [Actor message] -> Actor message tellComposition tellReducer actors =
library/TheatreDev/Tell.hs view
@@ -21,8 +21,8 @@ all tells msg = traverse_ (\tell -> tell msg) tells -byKeyHash :: (a -> Int) -> [Tell a] -> Tell a-byKeyHash proj tells =+byKeyHashOneOf :: (a -> Int) -> [Tell a] -> Tell a+byKeyHashOneOf proj tells = let vector = Vector.fromList tells vectorLength = Vector.length vector in case vectorLength of
theatre-dev.cabal view
@@ -1,6 +1,6 @@ cabal-version: 3.0 name: theatre-dev-version: 0.2+version: 0.3 category: Concurrency, Actors synopsis: Minimalistic actor library experiments description: