Documentation ¶
Overview ¶
package bchan provides 1:M value-broadcasting channels.
Receivers from bchan broadcast channels must be aware that they are using a bchan.Ch channel and call BcastAck() after every receive. Failure to do this will result in other receivers blocking instead of receiving the broadcast value.
Example ¶
package main import ( "fmt" "github.com/glycerine/bchan" "time" ) func main() { b := bchan.New(3) go func() { for { select { case v := <-b.Ch: b.BcastAck() // always do this after a receive on Ch, to keep broadcast enabled. fmt.Printf("received on Ch: %v\n", v) } } }() b.Set(4) time.Sleep(20 * time.Millisecond) b.Set(5) time.Sleep(20 * time.Millisecond) b.Clear() time.Sleep(20 * time.Millisecond) }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Bchan ¶
type Bchan struct { Ch chan interface{} // contains filtered or unexported fields }
Bchan is an 1:M non-blocking value-loadable channel. The client needs to only know about one rule: after a receive on Ch, you must call Bchan.BcastAck().
func New ¶
New constructor should be told how many recipients are expected in expectedDiameter. If the expectedDiameter is wrong the Bchan will still function, but you may get slower concurrency than if the number is accurate. It is fine to overestimate the diameter by a little or even be off completely, but the extra slots in the buffered channel take up some memory and will add linearly to the service time as they are maintained.
func (*Bchan) Bcast ¶
func (b *Bchan) Bcast(val interface{})
Bcast is the common case of doing both Set() and then On() together to start broadcasting a new value.
func (*Bchan) BcastAck ¶
func (b *Bchan) BcastAck()
BcastAck must be called immediately after a client receives on Ch. All clients on every channel receive must call BcastAck after receiving on the channel Ch. This makes such channels self-servicing, as BcastAck will re-fill the async channel with the current value.
func (*Bchan) Clear ¶
func (b *Bchan) Clear()
Clear turns off broadcasting and empties the channel of any old values.
func (*Bchan) Get ¶
func (b *Bchan) Get() interface{}
Get returns the currently set broadcast value.