Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Synopsis
- data Actor message
- spawnStatefulIndividual :: state -> (state -> message -> IO state) -> (state -> IO ()) -> IO (Actor message)
- spawnStatefulBatched :: state -> (state -> NonEmpty message -> IO state) -> (state -> IO ()) -> IO (Actor message)
- spawnStatelessIndividual :: (message -> IO ()) -> IO () -> IO (Actor message)
- spawnStatelessBatched :: (NonEmpty message -> IO ()) -> IO () -> IO (Actor message)
- tell :: Actor message -> message -> IO ()
- kill :: Actor message -> IO ()
- wait :: Actor message -> IO ()
- oneOf :: [Actor message] -> Actor message
- allOf :: [Actor message] -> Actor message
- byKeyHash :: (message -> Int) -> [Actor message] -> Actor message
Documentation
Controls of an actor, which processes the messages of type message
.
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
.
Acquisition
spawnStatefulIndividual :: state -> (state -> message -> IO state) -> (state -> IO ()) -> IO (Actor message) Source #
spawnStatefulBatched :: state -> (state -> NonEmpty message -> IO state) -> (state -> IO ()) -> IO (Actor message) Source #
spawnStatelessIndividual Source #
:: (message -> IO ()) | Interpreter of a message. |
-> IO () | Clean up when killed. |
-> IO (Actor message) | Fork a thread to run the handler daemon on and produce a handle to control it. |
Given an interpreter of messages, fork a thread to run the handler daemon on and produce a handle to control that actor.
Killing that actor will make it process all the messages in the queue first. All the messages sent to it after killing won't be processed.
Control
Composition
oneOf :: [Actor message] -> Actor message Source #
Distribute the message stream across actors. The message gets delivered to the first available one.
E.g., using this combinator in combination with replicateM
you can construct pools:
spawnPool :: Int -> IO (Actor message) -> IO (Actor message) spawnPool size spawn = oneOf <$> replicateM size spawn
You can consider this being an interface to the Sum monoid.
allOf :: [Actor message] -> Actor message Source #
You can consider this being an interface to the Product monoid.
:: (message -> Int) | Function extracting the key from the message and hashing it. |
-> [Actor message] | Pool of actors. |
-> Actor message |
Dispatch the message across actors based on a key hash.
This lets you ensure of a property that messages with the same key will arrive to the same actor, letting you maintain a local associated state in the actors.
The implementation applies a modulo equal to the amount 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.