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

TheatreDev.Actor

Synopsis

Documentation

data Actor message Source #

Controls of an actor, which processes the messages of type message. The processing runs on a dedicated green thread.

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, firstAvailableOneOf and byKeyHashOneOf.

Instances

Instances details
Contravariant Actor Source # 
Instance details

Defined in TheatreDev.Actor

Methods

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

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

Decidable Actor Source # 
Instance details

Defined in TheatreDev.Actor

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.Actor

Methods

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

conquer :: Actor a #

Acquisition

spawnStatefulIndividual Source #

Arguments

:: state

Initial state.

-> (state -> IO ())

Clean up when killed or exception is thrown.

-> (state -> message -> IO state)

Process a message and update state.

-> IO (Actor message)

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

Spawn an actor which processes messages in isolated executions and threads state.

spawnStatefulBatched Source #

Arguments

:: state

Initial state.

-> (state -> IO ())

Clean up when killed or exception is thrown.

-> (state -> NonEmpty message -> IO state)

Process a batch of messages and update state.

-> IO (Actor message)

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

Spawn an actor which processes all available messages in one execution and threads state.

spawnStatelessIndividual Source #

Arguments

:: IO ()

Clean up when killed or exception is thrown.

-> (message -> IO ())

Interpret a message.

-> IO (Actor message)

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

Spawn an actor which processes messages in isolated executions.

spawnStatelessBatched Source #

Arguments

:: IO ()

Clean up when killed or exception is thrown.

-> (NonEmpty message -> IO ())

Interpret a batch of messages.

-> IO (Actor message)

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

Spawn an actor which processes all available messages in one execution.

Control

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

Add a message to the end of the queue of the messages to be processed by the provided actor.

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

Command the actor to stop registering new messages, process all the registered ones and execute the clean up action.

This action executes immediately. If you want to block waiting for the actor to actually die, after kill you can run wait.

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

Block waiting for the actor to die either due to getting killed or due to its interpreter action throwing an exception. The exception will get rethrown here.

Composition

firstAvailableOneOf :: [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 =
  firstAvailableOneOf <$> replicateM size spawn

You can consider this being an interface to the Sum monoid.

byKeyHashOneOf 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.

allOf :: [Actor message] -> Actor message Source #

Distribute the message stream to all provided actors.

You can consider this being an interface to the Product monoid.