Gleam OTP

repository·main·Indexed 21 days ago

https://github.com/gleam-lang/otp

Type-safe bindings to Erlang's OTP (Open Telecom Platform) framework for Gleam programs running on the BEAM. Provides abstractions for building fault-tolerant, multi-core applications using actors and supervisors, including static and factory supervisors for creating supervision trees.

Tokens
834
Snippets
2
Records
4
Agent score
25%

What's inside gleam_otp

  1. Understand the core OTP abstractions in Gleam

    main

    Gleam OTP provides three primary levels of abstraction for building fault-tolerant programs:

    1. Process: The lowest-level building block. While all actors are processes, you should generally favor higher-level abstractions like actor or supervisor for application logic. Note that the process module is defined in the gleam_erlang library.
    2. Actor: The standard building block for stateful logic. It provides type-safe message handling and manages OTP system messages automatically.
    3. Supervisor: A process designed to manage other processes. Supervisors can restart crashed processes or terminate them during shutdown. By nesting supervisors, you can create a supervision tree, which provides hierarchical fault tolerance and monitoring.
  2. Use Supervisors for fault tolerance

    main

    Supervisors are used to create self-healing systems by monitoring other processes. This library provides two main types of supervisors:

    • Static Supervisor: For managing a fixed set of processes.
    • Factory Supervisor: For managing processes that may need to be created dynamically.

    Refer to the gleam/otp/static_supervisor and gleam/otp/factory_supervisor documentation for specific implementation details.

  3. Create a type-safe Actor with gleam_otp

    main

    The actor is the most common process type in this library. It is designed to be a type-safe building block similar to Erlang's gen_server. It automatically handles OTP system messages to support debugging and tracing.

    To use an actor, you define a state type, a message type, and a handler function that returns actor.Next(state, message). You then initialize it using actor.new, configure it with actor.on_message, and start it with actor.start.

    import gleam/erlang/process.{type Subject}
    import gleam/otp/actor
    
    pub 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))
    }