theatre-dev-0.0.1: Minimalistic actor library experiments
Safe HaskellSafe-Inferred
LanguageHaskell2010

TheatreDev.StmBased

Synopsis

Documentation

data Actor message Source #

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.

Instances

Instances details
Contravariant Actor Source # 
Instance details

Defined in TheatreDev.StmBased

Methods

contramap :: (a' -> a) -> Actor a -> Actor a' #

(>$) :: b -> Actor b -> Actor a #

Decidable Actor Source # 
Instance details

Defined in TheatreDev.StmBased

Methods

lose :: (a -> Void) -> Actor a #

choose :: (a -> Either b c) -> Actor b -> Actor c -> Actor a #

Divisible Actor Source # 
Instance details

Defined in TheatreDev.StmBased

Methods

divide :: (a -> (b, c)) -> Actor b -> Actor c -> Actor a #

conquer :: Actor a #

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 #

Arguments

:: (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.

spawnStatelessBatched Source #

Arguments

:: (NonEmpty message -> IO ())

Interpreter of a batch of messages.

-> IO ()

Clean up when killed.

-> IO (Actor message)

Fork a thread to run the handler daemon on and produce a handle to control it.

Control

tell :: Actor message -> message -> IO () Source #

kill :: Actor message -> IO () Source #

wait :: Actor message -> IO () Source #

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.

byKeyHash Source #

Arguments

:: (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.