broadcast-chan

Closable, fair, single-wakeup channel type that avoids 0 reader space leaks.

https://github.com/merijn/broadcast-chan

Version on this page:0.2.0
LTS Haskell 22.18:0.2.1.2@rev:2
Stackage Nightly 2023-12-26:0.2.1.2@rev:2
Latest on Hackage:0.2.1.2@rev:2

See all snapshots broadcast-chan appears in

BSD-3-Clause licensed by Merijn Verstraaten
Maintained by Merijn Verstraaten
This version can be pinned in stack with:broadcast-chan-0.2.0@sha256:9bbe2638a7952d58afa72e8a11b65742a73f4bcd5e5f144a25938ea33ef5c0b8,6207

Module documentation for 0.2.0

BroadcastChan: Closable, fair, single-wakeup, broadcast channels

BSD3 Hackage Stackage Build Status

A closable, fair, single-wakeup channel that avoids the 0 reader space leak that Control.Concurrent.Chan from base suffers from.

The Chan type from Control.Concurrent.Chan consists of both a read and write end combined into a single value. This means there is always at least 1 read end for a Chan, which keeps any values written to it alive. This is a problem for applications/libraries that want to have a channel that can have zero listeners.

Suppose we have an library that produces events and we want to let users register to receive events. If we use a channel and write all events to it, we would like to drop and garbage collect any events that take place when there are 0 listeners. The always present read end of Chan from base makes this impossible. We end up with a Chan that forever accumulates more and more events that will never get removed, resulting in a memory leak.

BroadcastChan splits channels into separate read and write ends. Any message written to a a channel with no existing read end is immediately dropped so it can be garbage collected. Once a read end is created, all messages written to the channel will be accessible to that read end.

Once all read ends for a channel have disappeared and been garbage collected, the channel will return to dropping messages as soon as they are written.

Why should I use BroadcastChan over Control.Concurrent.Chan?

  • BroadcastChan is closable,
  • BroadcastChan has no 0 reader space leak,
  • BroadcastChan has comparable or better performance.

Why should I use BroadcastChan over various (closable) STM channels?

  • BroadcastChan is single-wakeup,
  • BroadcastChan is fair,
  • BroadcastChan performs better under contention.