tokio_gen_server

Crates.iotokio_gen_server
lib.rstokio_gen_server
version0.5.0
sourcesrc
created_at2024-02-10 09:43:56.075705
updated_at2024-07-10 07:08:33.841459
descriptionElixir/Erlang-GenServer-like actor on the Tokio runtime.
homepage
repositoryhttps://github.com/SichangHe/tokio_gen_server
max_upload_size
id1134837
size68,061
Steven HΓ© (SΔ«chΓ ng) (SichangHe)

documentation

README

Tokio GenServer

A simple Elixir/Erlang-GenServer-like actor implementation on Tokio.

Please see the documentation for information on usage and examples.

  • Simple: Define 3 messages types and at least one callback, and you have an actor. No macro magic. No global "system"s or "pollution".
  • Powerful: Initialization and exit callbacks. On completion, returns the actor itself, the whole running environment, and run result.
  • Lightweight: Little code. No boxing except the ones introduced by Tokio.
  • Blocking aCTOR (Bctor) for use with synchronous code.

NB: What is a GenServer? A Generic Server is a persistent process that handles messages sent to it. That's it. The message can either be a cast (fire-and-forget) or a call (request-response).

Major difference from Erlang

  • Do not panic (crash). Return anyhow::Error instead.
  • Tokio scheduling is cooperative, not preemptive. Your long-running async code should call yield_now in the middle so they can be interrupted.
  • Blanket supervision implementations are not very useful. Instead, let your actors spawn children actors, and let children send messages to their parents using before_exit to handle children exiting.

Alternatives

Hydra:

  • πŸ‘ Implements "proper" "process"; interfaces closer to Erlang.
  • πŸ‘ Type-erasure on "child", "pid".
  • πŸ‘ Supervisors.
  • πŸ‘ Multi-node.
  • ❓ Catches unwind (also bad because of overhead?).
  • ❓ Uses flume channels: may be faster, but more dependencies.
  • πŸ‘Ž Requires creating "applications" and "pollutes" the codebase.
  • πŸ‘Ž Call, cast, and reply message types are not distinguished.
  • πŸ‘Ž Uses magic global variables to manage "processes".
  • πŸ‘Ž No blocking.
  • πŸ‘Ž Requires messages to be serializable.
  • πŸ‘Ž Most traits are not object-safe.
Commit count: 47

cargo fmt