extensible-effects-concurrent: Message passing concurrency as extensible-effect

[ bsd3, concurrency, control, effect, library, program ] [ Propose Tags ] [ Report a vulnerability ]

Modules

[Last Documentation]

  • Control
    • Eff
      • Control.Eff.Concurrent
        • Control.Eff.Concurrent.Process
          • Control.Eff.Concurrent.Process.ForkIOScheduler
          • Control.Eff.Concurrent.Process.Interactive
          • Control.Eff.Concurrent.Process.SingleThreadedScheduler
          • Control.Eff.Concurrent.Process.Timer
        • Control.Eff.Concurrent.Protocol
          • Control.Eff.Concurrent.Protocol.Broker
          • Control.Eff.Concurrent.Protocol.CallbackServer
          • Control.Eff.Concurrent.Protocol.Client
          • Control.Eff.Concurrent.Protocol.EffectfulServer
          • Control.Eff.Concurrent.Protocol.Observer
            • Control.Eff.Concurrent.Protocol.Observer.Queue
          • Control.Eff.Concurrent.Protocol.StatefulServer
          • Control.Eff.Concurrent.Protocol.Watchdog
          • Control.Eff.Concurrent.Protocol.Wrapper
        • Control.Eff.Concurrent.Pure
        • Control.Eff.Concurrent.SingleThreaded
      • Control.Eff.ExceptionExtra
      • Control.Eff.Log
        • Control.Eff.Log.Examples
        • Control.Eff.Log.Handler
        • Control.Eff.Log.Message
        • Control.Eff.Log.MessageRenderer
        • Control.Eff.Log.Writer
      • LogWriter
        • Control.Eff.LogWriter.Async
        • Control.Eff.LogWriter.Console
        • Control.Eff.LogWriter.DebugTrace
        • Control.Eff.LogWriter.File
        • Control.Eff.LogWriter.Rich
        • Control.Eff.LogWriter.UDP
        • Control.Eff.LogWriter.UnixSocket
      • Control.Eff.Loop

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0, 0.1.0.1, 0.1.1.0, 0.1.2.0, 0.1.2.1, 0.1.2.2, 0.1.3.0, 0.2.0.2, 0.2.0.3, 0.3.0.0, 0.3.0.1, 0.3.0.2, 0.4.0.0, 0.5.0.0, 0.5.0.1, 0.6.0, 0.6.1, 0.6.2, 0.6.3, 0.7.0, 0.7.1, 0.7.2, 0.7.3, 0.8, 0.9.0, 0.9.2, 0.9.2.1, 0.9.2.2, 0.10.0, 0.11.1, 0.12.0, 0.12.1, 0.13.0, 0.13.2, 0.14.0, 0.14.1, 0.14.2, 0.14.3, 0.15.0, 0.16.0, 0.16.1, 0.17.0, 0.18.0, 0.18.1, 0.19.0, 0.19.1, 0.20.0, 0.21.0, 0.21.1, 0.22.0, 0.22.1, 0.23.0, 0.24.0, 0.24.1, 0.24.2, 0.24.3, 0.25.0, 0.25.1, 0.26.0, 0.26.1, 0.27.0, 0.28.0, 0.29.0, 0.29.1, 0.29.2, 0.30.0, 0.31.0, 0.32.0, 2.0.0
Change log ChangeLog.md
Dependencies async (>=2.2 && <3), base (>=4.12 && <5), containers (>=0.5.8 && <0.7), data-default (>=0.7 && <0.8), deepseq (>=1.4 && <1.5), directory, exceptions (>=0.10 && <0.11), extensible-effects (>=5 && <6), extensible-effects-concurrent, filepath (>=1.4 && <1.5), hashable (>=1.2), hostname, lens (>=4.14 && <5), monad-control (>=1.0 && <1.1), mtl (>=2.2 && <2.3), network (>=2 && <4), safe-exceptions (>=0.1 && <0.2), stm (>=2.4.5 && <2.6), text (>=1.2 && <1.3), time (>=1.8 && <2), transformers-base (>=0.4 && <0.5) [details]
Tested with ghc ==8.6.5, ghc ==8.8.3, ghc ==8.10.2
License BSD-3-Clause
Copyright Copyright Sven Heyll
Author Sven Heyll
Maintainer sven.heyll@gmail.com
Category Concurrency, Control, Effect
Home page https://github.com/sheyll/extensible-effects-concurrent#readme
Bug tracker https://github.com/sheyll/extensible-effects-concurrent/issues
Source repo head: git clone https://github.com/sheyll/extensible-effects-concurrent
Uploaded by SvenHeyll at 2020-12-16T17:13:06Z
Distributions
Executables extensible-effects-concurrent-example-loadtest, extensible-effects-concurrent-example-embedded-protocols, extensible-effects-concurrent-example-4, extensible-effects-concurrent-example-3, extensible-effects-concurrent-example-2, extensible-effects-concurrent-example-1
Downloads 30227 total (77 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs not available [build log]
All reported builds failed as of 2020-12-16 [all 3 reports]

Readme for extensible-effects-concurrent-2.0.0

[back to package description]

extensible-effects-concurrent

DEPRECATION WARNING

THIS PROJECT WILL NO LONGER BE MAINTAINED AND I ACTIVELY ADVISE AGAINST USING IT

While some ideas seemed wortwhile, I realized that this project could not perform as good as I had hoped under high load.

This project is deprecated in favor of a small library that encapsulates only a tiny wrapper around TVars and forkIO.

Hackage

From Erlang to Haskell

This project is an attempt to implement core ideas learned from the Erlang/OTP framework in Haskell using extensible-effects.

This library sketches my personal history of working on a large, real world Erlang application, trying to bring some ideas over to Haskell.

I know about cloud-haskell and transient, but I wanted something based on 'extensible-effects', and I also wanted to deepen my understanding of it.

Modeling an Application with Processes

The fundamental approach to modelling applications in Erlang is based on the concept of concurrent, communicating processes.

Example Code

module Main where

import           Control.Eff
import           Control.Eff.Concurrent

main :: IO ()
main = defaultMain example

example :: Eff Effects ()
example = do
  person <- spawn "alice" alice
  replyToMe <- self
  sendMessage person replyToMe
  personName <- receiveMessage
  logInfo "I just met " (personName :: String)

alice :: Eff Effects ()
alice = do
  logInfo "I am waiting for someone to ask me..."
  sender <- receiveMessage
  sendMessage sender ("Alice" :: String)
  logInfo sender " message received."

This is taken from example-4.

Running this example causes this output:

DEBUG      no proc  scheduler loop entered                                       at ForkIOScheduler.hs:209
DEBUG        init!1 enter process                                                at ForkIOScheduler.hs:691
NOTICE       init!1 ++++++++ main process started ++++++++                       at ForkIOScheduler.hs:579
DEBUG       alice!2 enter process                                                at ForkIOScheduler.hs:691
INFO        alice!2 I am waiting for someone to ask me...                        at Main.hs:19
INFO        alice!2 !1 message received.                                         at Main.hs:22
DEBUG       alice!2 exit: Process finished successfully                          at ForkIOScheduler.hs:729
INFO         init!1 I just met Alice                                             at Main.hs:15
NOTICE       init!1 ++++++++ main process returned ++++++++                      at ForkIOScheduler.hs:581
DEBUG        init!1 exit: Process finished successfully                          at ForkIOScheduler.hs:729
DEBUG      no proc  scheduler loop returned                                      at ForkIOScheduler.hs:211
DEBUG      no proc  scheduler cleanup begin                                      at ForkIOScheduler.hs:205
NOTICE     no proc  cancelling processes: []                                     at ForkIOScheduler.hs:222
NOTICE     no proc  all processes cancelled                                      at ForkIOScheduler.hs:239

The mental model of the programming framework regards objects as processes with an isolated internal state.

Processes are at the center of that contraption. All actions happen in processes, and all interactions happen via messages sent between processes.

This is called Message Passing Concurrency; in this library it is provided via the Process effect.

The Process effect itself is just an abstract interface.

There are two schedulers, that interpret the Process effect:

  • A multi-threaded scheduler, based on the async
  • A pure single-threaded scheduler, based on coroutines

Using the library

For convenience, it is enough to import one of three modules:

All processes except the first process are spawned by existing processes.

When a process spawns a new process they are independent apart from the fact that the parent knows the process-id of the spawend child process.

Processes can monitor each other to be notified when a communication partner exits, potentially in unforseen ways.

Similarily processes may choose to mutually link each other.

That allows to model trees in which processes watch and start or restart each other.

Because processes never share memory, the internal - possibly broken - state of a process is gone, when a process exits; hence restarting a process will not be bothered by left-over, possibly inconsistent, state.

Timers

The Timer module contains functions to send messages after a time has passed, and reiceive messages with timeouts.

More Type-Safety: The Protocol Metaphor

As the library carefully leaves the realm of untyped messages, it uses the concept of a protocol that governs the communication between concurrent processes, which are either protocol servers or clients as a metaphor.

The communication is initiated by the client.

The idea is to indicate such a protocol using a custom data type, e.g. data TemperatureSensorReader or data SqlServer.

The library consists some tricks to restrict the kinds of messages that are acceptable when communicating with processes adhering to the protocol.

This protocol is not encoded in the users code, but rather something that the programmer keeps in his head.

In order to be appreciated by authors of real world applications, the protocol can be defined by giving an abstract message sum-type and code for spawning server processes.

It focusses on these questions:

  1. What messages does a process accept?
  2. When sending a certain message, should the sender wait for an answer?

Protocol Phantom Type

In this library, the key to a protocol is a single type, that could even be a so called phantom type, i.e. a type without any runtime values:


data UserRegistry -- look mom, no constructors!!

Such a type exists only for the type system.

It can only be used as a parameter to certain type constructors, and for defining type class and type family instances, e.g.


newtype Endpoint protocol = MkServer { _processId :: ProcessId }

data UserRegistry

startUserRegistry :: Eff e (Endpoint UserRegistry)
startUserRegistry =
  error "just an example"

Here the Endpoint has a type parameter protocol but the type is not used by the constructor to hold any values, hence we can use UserRegistry, as a parameter, since UserRegistry has no value constructors.

Protocol Data Units

Messages that belong to a protocol are called protocol data units (PDU).

Protocol Servers Endpoints

The ProcessId of a process identifies the messages box that receiveMessage will use, when waiting for an incoming message.

While it defines where the messages are collected, it does not restrict or inform about what data is handled by a process.

An Endpoint is a wrapper around the ProcessId that takes a type parameter.

The type does not have to have any values, it can be a phantom type.

This type serves only the tag the process as a server accepting messages identified by the HasPdu type class.

Server/Protocol Composability

Usually a protocol consists of some really custom PDUs and some PDUs that more or less are found in many protocols, like event listener registration and event notification.

It is therefore helpful to be able to compose protocols.

The machinery in this library allows to list several PDU instances understood by endpoints of a given protocol phantom type.

Protocol Clients

Clients use a protocol by sending Pdus indexed by some protocol phantom type to a server process.

Clients use Endpoints to address these servers, and the functions defined in the corresponding module.

Most important are these two functions:

  • cast to send fire-and-forget messages

  • call to send RPC-style requests and wait (block) for the responses. Also, when the server is not running, or crashes in while waiting, the calling process is interrupted

Protocol Servers

This library offers an API for defining practically safe to use protocol servers:

  • EffectfulServer This modules defines the framework of a process, that has a callback function that is repeatedly called when ever a message was received.

    The callback may rely on any extra effects (extensible effects).

  • StatefulServer A server based on the EffectfulServer that includes the definition of in internal state called Model, and some nice helper functions to access the model. These functions allow the use of lenses. Unlike the effect server, the effects that the callback functions can use are defined in this module.

  • CallbackServer A server based on the EffectfulServer that does not require a type class instance like the stateful and effect servers do. It can be used to define inline servers.

Events and Observers

A parameterized protocol for event handling is provided in the module:

Brokers and Watchdogs

A key part of a robust system is monitoring and possibly restarting stuff that crashes, this is done in conjunction by two modules:

A client of a process that might be restarted cannot use the ProcessId directly, but has to use an abstract ID and lookup the ProcessId from a process broker, that manages the current ProcessId of protocol server processes.

That way, when ever the server process registered at a broker crashes, a watchdog process can (re-)start the crashed server.

Additional services

Currently, a logging effect is also part of the code base.

Usage and Implementation

Should work with stack, cabal and nix.

Required GHC Extensions

In order to use the library you might need to activate some extension in order to fight some ambiguous types, stemming from the flexibility to choose different Scheduler implementations.

  • AllowAmbiguousTypes
  • TypeApplications

Planned Features

  • Stackage extensible-effects-concurrent LTS

  • Scheduler ekg Monitoring