Bridge channel
A bridge channel flattens a stream of channels into one output stream while preserving cancellation and ownership.
Canonical guidance
- use a bridge when nested streams are the natural shape
- flatten through one owner goroutine
- thread cancellation through both the outer and inner streams
Use when
- dynamically discovered substreams
- staged fan-in pipelines
- stream multiplexing
Avoid
- consuming inner channels directly at many call sites
- losing track of who closes what
- flattening without cancellation
Preferred pattern
- one bridge goroutine ranges outer channels and relays inner values to one output
Anti-pattern
- spawning one permanent goroutine per inner channel with no global stop path
Explanation: This is tempting because each inner stream feels independent, but the flattening layer still needs one lifecycle owner.
Why
- flattening nested streams is mostly an ownership and shutdown problem
Related pages
Sources
- Advanced Go Concurrency Patterns - Sameer Ajmani
- Go Concurrency Patterns: Pipelines and cancellation - Sameer Ajmani