/chip

Registry library for gleam

Primary LanguageGleam

chip

Package Version Hex Docs

A local process registry that plays along with Gleam's Subject type.

It can hold a set of subjects to later retrieve individually or dispatch as a group. Will also automatically delist dead processes.

Example

Lets assemble the pieces to build a simple counter actor:

import gleam/erlang/process
import gleam/otp/actor

pub opaque type Message {
  Inc
  Current(client: process.Subject(Int))
}

pub fn increment(counter) {
  actor.send(counter, Inc)
}

pub fn current(counter) {
  actor.call(counter, Current(_), 10)
}

fn loop(message: Message, count: Int) {
  case message {
    Inc -> {
      actor.Continue(count + 1, option.None)
    }

    Current(client) -> {
      process.send(client, count)
      actor.Continue(count, option.None)
    }
  }
}

We start our registry and create new instances of a counter:

import gleam/otp/actor
import chip/group

pub fn main() {
  let assert Ok(registry) = chip.start()

  let assert Ok(counter_1) = actor.start(0, loop)
  let assert Ok(counter_2) = actor.start(0, loop)
  let assert Ok(counter_3) = actor.start(0, loop)

  chip.register(registry, chip.new(counter_1) |> chip.tag(1))
  chip.register(registry, chip.new(counter_2) |> chip.tag(2))
  chip.register(registry, chip.new(counter_3) |> chip.tag(3))
  
  process.sleep_forever()
}

Later, we may retrieve a member:

let assert Ok(counter) = chip.find(registry, 2)
let assert 0 = counter.current(counter)

Or broadcast a message to all members:

chip.dispatch(registry, fn(counter) {
  actor.increment(counter)
}) 

let assert Ok(counter) = chip.find(registry, 2)
let assert 1 = counter.current(counter)

The road towards V1

Feature-wise this is near beign complete. Still planning to integrate:

  • Adjust the API to be more in-line with other Registry libraries like elixir's Registry, erlang's pg or Syn.
  • Document modules.
  • Basic test case scenarios.
  • Should play well with gleam style of supervisors.
  • Document guides and use-cases.
  • Build a benchmark that measures performance and memory consuption.
  • Implement an ETS backend.
  • Benchmark in-process backend vs ETS backend.

Previous Art

This registry takes and combines some ideas from Elixir’s Registry, Erlang’s pg and Syn.

Alternatives

Singularity is a gleam library that offers registry capabilities but focusing more on singleton actors, therefore it is better suited for keeping track of actors that need to be passed around as configuration through your app.

Installation

gleam add chip