Hive.Group

hive · API reference

Local process groups.

Local process groups: a named set of members, no uniqueness.

The local analog of :pg — many members per name, used to fan a message out to all of them. Same Type.Id-witnessed typing as Registry; same kcas backing.

type t

A local group table.

type 'a key

A typed group name. Members of this group have type 'a.

type membership

Returned by join; the only way to leave.

val create : unit -> t

Create an empty group table.

ocaml]
let groups = Hive.Group.create ()
val key : string -> 'a key

Fresh key for a group name; share the key to share the group.

Two calls with the same string create incompatible keys. Define the key once next to the worker or capability it names, then reuse that value.

val name : _ key -> string

The textual group name carried by a key.

val join : t -> 'a key -> 'a -> (membership, [ `Name_in_use ]) result

Add a member. Error Name_in_use` when the name is already a group under a different key.

ocaml]
let key = Hive.Group.key "chat:room-1" in
let token = Result.get_ok (Hive.Group.join groups key client_addr)
val leave : t -> membership -> unit

Remove the member added by this membership. Idempotent.

val members : t -> 'a key -> 'a list

Current members, newest first. Empty when the name is free or owned by a different key.

ocaml]
List.iter (fun addr -> ignore (Hive.Addr.send addr (`Text "hello")))
  (Hive.Group.members groups key)