bchan

package module
Version: v0.0.0-...-ad30cd8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 10, 2017 License: MIT Imports: 1 Imported by: 7

README

bchan

bchan is a golang lib for 1:M value-broadcasting channels

godoc: https://godoc.org/github.com/glycerine/bchan

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"
	"time"

	"github.com/glycerine/bchan"
)

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

func New(expectedDiameter int) *Bchan

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.

func (*Bchan) On

func (b *Bchan) On()

On turns on the broadcast channel without changing the value to be transmitted.

func (*Bchan) Set

func (b *Bchan) Set(val interface{})

Set stores a value to be broadcast and clears any prior queued up old values. Call On() after set to activate the new value. See also Bcast() that does Set() followed by On() in one call.

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL