bcast

package module
v0.0.0-...-1447f06 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2019 License: BSD-3-Clause Imports: 4 Imported by: 19

README

bcast package for Go

Broadcasting on a set of channels in Go. Go channels offer different usage patterns but not ready to use broadcast pattern. This library solves the problem in direct way. Each routine keeps member structure with own input channel and single for all members output channel. Central dispatcher accepts broadcasts and resend them to all members.

Usage Go Walker

Firstly import package and create broadcast group. You may create any number of groups for different broadcasts:

		import (
			"github.com/grafov/bcast"
		)

		group := bcast.NewGroup() // create broadcast group
		go group.Broadcast(0) // accepts messages and broadcast it to all members

You may listen broadcasts limited time:

		bcast.Broadcast(2 * time.Minute) // if message not arrived during 2 min. function exits

Now join to the group from different goroutines:

		member1 := group.Join() // joined member1 from one routine

Either member may send message which received by all other members of the group:

		member1.Send("test message") // send message to all members

Also you may send message to group from nonmember of a group:

		group.Send("test message")

Method Send accepts interface{} type so any values may be broadcasted.

		member2 := group.Join() // joined member2 form another routine
		val := member1.Recv() // broadcasted value received

Another way to receive broadcasted messages is listen input channel of the member.

		val := <-*member1.In // each member keeps pointer to its own input channel

It may be convenient for example when select used.

See more examples in a test suit bcast_test.go.

Install

go get github.com/grafov/bcast

The library doesn't require external packages for build. The next package required if you want to run unit tests:

gopkg.in/fatih/set.v0

License

Library licensed under BSD 3-clause license. See LICENSE.

Project status Build Status

WIP again. There is bug found (see #12) and some possible improvements are waiting for review (#9).

API is stable. No major changes planned, maybe small improvements.

Documentation

Index

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
}

Group provides a mechanism for the broadcast of messages to a collection of channels.

func NewGroup

func NewGroup() *Group

NewGroup creates a new broadcast group.

func (*Group) Add

func (g *Group) Add(memberChannel chan interface{}) *Member

Add adds a member to the group for the provided interface channel.

func (*Group) Broadcast

func (g *Group) Broadcast(timeout time.Duration)

Broadcast messages received from one group member to others. If incoming messages not arrived during `timeout` then function returns.

func (*Group) Close

func (g *Group) Close()

Close terminates the group immediately.

func (*Group) Join

func (g *Group) Join() *Member

Join returns a new member object and handles the creation of its output channel.

func (*Group) Leave

func (g *Group) Leave(leaving *Member) error

Leave removes the provided member from the group and closes him

func (*Group) MemberCount

func (g *Group) MemberCount() int

MemberCount returns the number of members in the Broadcast Group.

func (*Group) Members

func (g *Group) Members() []*Member

Members returns a slice of Members that are currently in the Group.

func (*Group) Send

func (g *Group) Send(val interface{})

Send broadcasts a message to every one of a Group's members.

type Item

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

An Item is something we manage in a priority queue.

type Member

type Member struct {
	Read chan interface{}
	// contains filtered or unexported fields
}

Member represents member of a Broadcast group.

func (*Member) Close

func (m *Member) Close()

Close removes the member it is called on from its broadcast group and closes Read channel.

func (*Member) Recv

func (m *Member) Recv() interface{}

Recv reads one value from the member's Read channel

func (*Member) Send

func (m *Member) Send(val interface{})

Send broadcasts a message from one Member to the channels of all the other members in its group.

type Message

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

Message is an internal structure to pack messages together with info about sender.

type PriorityQueue

type PriorityQueue []*Item

A PriorityQueue implements heap.Interface and holds Items.

func (PriorityQueue) Len

func (pq PriorityQueue) Len() int

func (PriorityQueue) Less

func (pq PriorityQueue) Less(i, j int) bool

func (*PriorityQueue) Pop

func (pq *PriorityQueue) Pop() interface{}

func (*PriorityQueue) Push

func (pq *PriorityQueue) Push(x interface{})

func (PriorityQueue) Swap

func (pq PriorityQueue) Swap(i, j int)

Jump to

Keyboard shortcuts

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