Multi-subscriber channels for Golang
The multicast module provides single-writer, multiple-reader semantics around Go channels. It attempts to maintain semantics similar to those offered by standard Go channels while guaranteeing parallel delivery (slow consumers won't hold up delivery to other listeners) and guaranteeing delivery to all registered listeners when a message is published.
- Simple API if you know how to use channels then you'll be able to use multicast without learning anything special.
- Similar Semantics to core channels mean that your approach to reasoning around channels doesn't need to change.
- Low Overheads with linear memory growth as your number of listeners increases and no buffer overheads.
import (
"fmt"
"github.com/SierraSoftworks/multicast"
)
func main() {
c := multicast.New()
go func() {
l := c.Listen()
for msg := range l.C {
fmt.Printf("Listener 1: %s\n", msg)
}
fmt.Println("Listener 1 Closed")
}()
go func() {
l := c.Listen()
for msg := range l.C {
fmt.Printf("Listener 2: %s\n", msg)
}
fmt.Println("Listener 2 Closed")
}()
c.C <- "Hello World!"
c.Close()
}
Multicast implements its channels as a linked list of listeners which automatically
forward messages they receive to the next listener before exposing them on their C
channel.
This approach removes the need to maintain an array of listeners belonging to a channel and greatly simplifies the implementation.