roundrobin
An actor that load balances pushed messages to registered subjects in a roundrobin fashion. Subjects can be dynamically registered and unregistered
Types
Messages that can be sent to the Roundrobin actor.
pub type Message(e) {
Register(Subject(e))
Unregister(Subject(e))
Push(e)
}
Constructors
-
Register(Subject(e))
Registeres a new loadbalancer target
-
Unregister(Subject(e))
Removes a loadbalancer target
-
Push(e)
Emit an event that will be loadbalanced to the registered targets
Functions
pub fn handler(
msg: Message(a),
targets: List(Subject(a)),
) -> Next(Message(a), List(Subject(a)))
The handler to start the round robin actor
Examples:
import fresnel/roundrobin
import gleam/otp/actor
import gleam/erlang/process
import gleam/io
type Event {
Lights
Camera
Action
}
fn target(event: Event, id: String) -> actor.Next(Event, String) {
case event {
Lights -> {
io.println(id<>": lights")
actor.continue(id)
}
Camera -> {
io.println(id<>": camera")
actor.continue(id)
}
Action -> {
io.println(id<>": action!")
actor.continue(id)
}
}
}
pub fn main() {
let assert Ok(loadbalancer) = actor.start([], roundrobin.handler)
let assert Ok(listener1) = actor.start("1", target)
let assert Ok(listener2) = actor.start("2", target)
process.send(loadbalancer, Lights)
process.send(loadbalancer, Camera)
process.send(loadbalancer, Action)
}