Current section
Files
Jump to
Current section
Files
README.md
# Gleam OTPFault tolerant multi-core programs with OTP, the BEAM actor framework.[](https://hex.pm/packages/gleam_otp)[](https://hexdocs.pm/gleam_otp/)```shellgleam add gleam_otp@1``````gleamimport gleam/erlang/process.{type Subject}import gleam/otp/actorpub fn main() { // Start an actor let assert Ok(actor) = actor.new(0) |> actor.on_message(handle_message) |> actor.start // Send some messages to the actor actor.send(actor.data, Add(5)) actor.send(actor.data, Add(3)) // Send a message and get a reply assert actor.call(actor.data, waiting: 10, sending: Get) == 8}pub fn handle_message(state: Int, message: Message) -> actor.Next(Int, Message) { case message { Add(i) -> { let state = state + i actor.continue(state) } Get(reply) -> { actor.send(reply, state) actor.continue(state) } }}pub type Message { Add(Int) Get(Subject(Int))}```Gleam’s actor system is built with a few primary goals:- Full type safety of actors and messages.- Be compatible with Erlang’s OTP actor framework.- Provide fault tolerance and self-healing through supervisors.- Have equivalent performance to Erlang’s OTP.This library documents its abstractions and functionality, but you may also wishto read the documentation or other material on Erlang’s OTP framework to get afuller understanding of OTP, the problems it solves, and the motivations for itsdesign.Not all Erlang/OTP functionality is included in this library. Some is notpossible to represent in a type safe way, so it is not included. Other featuresare still in development, such as further process supervision strategies.## Common types of actorThis library provides several different types of actor that can be used inGleam programs.### ProcessThe process is the lowest level building block of OTP, all other actors arebuilt on top of processes either directly or indirectly. Typically thisabstraction would not be used very often in Gleam applications, favourother actor types that provide more functionality.Gleam's [process](https://hexdocs.pm/gleam_erlang/gleam/erlang/process.html) module is defined in the `gleam_erlang` library.### ActorThe `actor` is the most commonly used process type in Gleam and serves as a goodbuilding block for other abstractions. Like Erlang's `gen_server` it handlesOTP's system messages automatically to enable OTP's debugging and tracingfunctionality.[Documentation](https://hexdocs.pm/gleam_otp/gleam/otp/actor.html)### SupervisorSupervisors are processes that start and then supervise other processes.They can restart them if they crash, and terminate them when the application isshutting down.Supervisors can start other supervisors, resulting in a hierarchical processstructure called a supervision tree, providing fault tolerance and monitoringbenefits to a Gleam application.- [gleam/otp/static_supervisor](https://hexdocs.pm/gleam_otp/gleam/otp/static_supervisor.html) documentation.- [gleam/otp/factory_supervisor](https://hexdocs.pm/gleam_otp/gleam/otp/factory_supervisor.html) documentation.## Limitations and known issuesActors do not yet support all OTP system messages, so some of the OTP debuggingAPIs may not be fully functional. These unsupported messages are discarded byactors.If find that you have a need for one of the unimplemented system messages, openan issue and we will implement support for it.