broadcast

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2020 License: BSD-2-Clause Imports: 3 Imported by: 0

README

Broadcast

go.dev reference

Package broadcast implements a typed, one to many, broadcast mechanism for channels. Broadcasts are non-blocking, values are discarded on any receive channels which are not able to proceed.

Example

type mesg struct {
	n int
	s string
}

// Pass mesg{} to NewGroup to create mesg broadcast group
g := broadcast.NewGroup(mesg{})

// Get the send channel
s0 := g.SendChannel().(chan<- mesg)

// Create two receive channels each buffering 5 values
r1 := g.ReceiveChannel(5).(<-chan mesg)
r2 := g.ReceiveChannel(5).(<-chan mesg)

// Send 5 values
for i := 0; i < 5; i++ {
	s0 <- mesg{i, fmt.Sprintf("message %d", i)}
}

// No more values to send
close(s0)

// Receive the values from r1
for v := range r1 {
	fmt.Println(v)
}

// Receive the values from r2
for v := range r2 {
	fmt.Println(v)
}

Documentation

Overview

Package broadcast implements a typed, one to many, broadcast mechanism for channels. Broadcasts are non-blocking, values are discarded on any receive channels which are not able to proceed.

Example (Int)
package main

import (
	"fmt"

	"github.com/malcsm/broadcast"
)

func main() {
	type mesg struct {
		n int
		s string
	}

	// Pass int(0) to NewGroup to create int broadcast group
	g := broadcast.NewGroup(int(0))

	// Get the send channel
	s := g.SendChannel().(chan<- int)

	// Create a receive channel
	r := g.ReceiveChannel(1).(<-chan int)

	// Send and receive two values
	s <- 1
	fmt.Println(<-r)
	s <- 2
	fmt.Println(<-r)

	close(s)

}
Output:
1
2
Example (Struct)
package main

import (
	"fmt"

	"github.com/malcsm/broadcast"
)

func main() {
	type mesg struct {
		n int
		s string
	}

	// Pass mesg{} to NewGroup to create mesg broadcast group
	g := broadcast.NewGroup(mesg{})

	// Get the send channel
	s0 := g.SendChannel().(chan<- mesg)

	// Create two receive channels each buffering 5 values
	r1 := g.ReceiveChannel(5).(<-chan mesg)
	r2 := g.ReceiveChannel(5).(<-chan mesg)

	// Send 5 values
	for i := 0; i < 5; i++ {
		s0 <- mesg{i, fmt.Sprintf("message %d", i)}
	}

	// No more values to send
	close(s0)

	// Receive the values from r1
	for v := range r1 {
		fmt.Println(v)
	}

	// Receive the values from r2
	for v := range r2 {
		fmt.Println(v)
	}

}
Output:
{0 message 0}
{1 message 1}
{2 message 2}
{3 message 3}
{4 message 4}
{0 message 0}
{1 message 1}
{2 message 2}
{3 message 3}
{4 message 4}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Group

type Group struct {
	// contains filtered or unexported fields
}

A Group has a send channel and any number of receive channels. A group has a type associated with it. The values sent over the send and receive channels are all of this type.

func NewGroup

func NewGroup(i interface{}) *Group

NewGroup creates a Group with the associated type of the type of i.

func (*Group) CloseReceiveChannel

func (g *Group) CloseReceiveChannel(c interface{}) error

CloseReceiveChannel closes the receive channel and stops any further values being sent on it. It must be used instead of the Close built-in when closing the receive channel, otherwise a 'send on closed channel' panic is likely. CloseReceiveChannel will return an error if c is not of type <-chan T, where T is the type associated with the group.

func (*Group) ReceiveChannel

func (g *Group) ReceiveChannel(n int) interface{}

ReceiveChannel creates and returns a receive channel, with buffer size n. If the buffer is full, any values sent on the send channel will be discarded until there is space in the buffer. When the associated type of the Group is T, the return value is guaranteed to be of type <-chan T, and it is safe to perform a type assertion of:

ch := g.ReceiveChannel(1).(<-chan T)

func (*Group) SendChannel

func (g *Group) SendChannel() interface{}

SendChannel returns the send channel of the group. It can be called more than once and will always return the same channel. Closing the send channel will close all the receive channels in the Group, and the resources used by the Group will be freed. When the associated type of the Group is T, the return value is guaranteed to be of type chan<- T, and it is safe to perform a type assertion of:

ch := g.SendChannel().(chan<- T)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL