bcast

package module
v0.0.0-...-cc9d19a Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2018 License: BSD-3-Clause Imports: 2 Imported by: 0

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/NimbleIndustry/bcast"
		)

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

You may listen broadcasts limited time:

		bcast.Broadcasting(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/NimbleIndustry/bcast

No external dependencies beside standard packages.

License

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

Project status Build Status

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

Documentation

Overview

Broadcast over channels.

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
}

Represents broadcast group.

func NewGroup

func NewGroup() *Group

Create new broadcast group.

func (*Group) Add

func (r *Group) Add(out chan interface{})

func (*Group) Broadcast

func (r *Group) Broadcast()

Broadcast messages received from one group member to others. See https://github.com/grafov/bcast/issues/4 for rationale.

func (*Group) BroadcastFor

func (r *Group) BroadcastFor(totalTimeout time.Duration, messageTimeout time.Duration)

Broadcast messages received from one group member to others. If incoming messages not arrived during `totalTimeout` then function returns. Will wait `messageTimeout` for message to be read by a group member

func (*Group) Close

func (r *Group) Close()

Close the group immediately

func (*Group) Join

func (r *Group) Join() *Member

Join new member to broadcast.

func (*Group) MemberCount

func (r *Group) MemberCount() int

func (*Group) Members

func (r *Group) Members() []chan interface{}

func (*Group) Remove

func (r *Group) Remove(received Message)

func (*Group) Send

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

Broadcast message to all group members.

type Member

type Member struct {
	In chan interface{} // (public) get messages from others to own channel
	// contains filtered or unexported fields
}

Represents member of broadcast group.

func (*Member) Close

func (r *Member) Close()

Unjoin member from broadcast group.

func (*Member) Recv

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

Get broadcast message. As alternative you may get it from `In` channel.

func (*Member) Send

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

Broadcast message from one member to others except sender.

type Message

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

Internal structure to pack messages together with info about sender.

Jump to

Keyboard shortcuts

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