Documentation ¶
Overview ¶
Package broadcast implements multi-listener broadcast channels. See https://godoc.org/github.com/tjgq/broadcast for original implementation.
To create an un-buffered broadcast channel, just declare a Broadcaster:
var b broadcast.Broadcaster
To create a buffered broadcast channel with capacity n, call New:
b := broadcast.New(n)
To add a listener to a channel, call Listen and read from Channel():
l := b.Listen() for v := range l.Channel() { // ... }
To send to the channel, call Send:
b.Send("Hello world!") v <- l.Channel() // returns interface{}("Hello world!")
To remove a listener, call Discard.
l.Discard()
To close the broadcast channel, call Discard. Any existing or future listeners will read from a closed channel:
b.Discard() v, ok <- l.Channel() // returns ok == false
Index ¶
Constants ¶
const ErrClosedChannel = broadcastError("send after close")
ErrClosedChannel means the caller attempted to send to one or more closed broadcast channels.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Broadcaster ¶
type Broadcaster struct {
// contains filtered or unexported fields
}
Broadcaster implements a Publisher. The zero value is a usable un-buffered channel.
func NewBroadcaster ¶
func NewBroadcaster(n int) *Broadcaster
NewBroadcaster returns a new Broadcaster with the given capacity (0 means un-buffered).
func (*Broadcaster) Discard ¶
func (b *Broadcaster) Discard()
Discard closes the channel, disabling the sending of further messages.
func (*Broadcaster) Listen ¶
func (b *Broadcaster) Listen() *Listener
Listen returns a Listener for the broadcast channel.
func (*Broadcaster) Send ¶
func (b *Broadcaster) Send(v interface{}) error
Send broadcasts a message to each listener's channel. Sending on a closed channel causes a runtime panic. This method is non-blocking, and will return errors if unable to send on a given listener's channel.
func (*Broadcaster) SendWithTimeout ¶
func (b *Broadcaster) SendWithTimeout(v interface{}, timeout time.Duration) error
SendWithTimeout broadcasts a message to each listener's channel. Sending on a closed channel causes a runtime panic. This method blocks for a duration of up to `timeout` on each channel. Returns error(s) if it is unable to send on a given listener's channel within `timeout` duration.