Giter Club home page Giter Club logo

bcast's Introduction

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.

bcast's People

Contributors

argami avatar brimstone avatar colonelpanic8 avatar grafov avatar matthewmcneely avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

bcast's Issues

Ordering of broadcast messages is not preserved

I believe that this is because there is a race condition at

bcast/bcast.go

Line 112 in 79e4f35

}

and

bcast/bcast.go

Line 150 in 79e4f35

}

I believe that what is happening is that sometimes go routines that were scheduled later than other goroutines by these lines for a given channel get called first.

I see three ways to approach fixing this:

Option 1 is to simply bite the bullet and call out to each member's channel synchronously.
Option 2 is to start using a logical clock for events and passing those values through to the consumer
Option 3 also involves a logical clock, but it hides it from consumers by having an intermediate goroutine/channel pair running for each member that handles ensuring that the events are properly serialized

This library is buggy.

https://github.com/grafov/bcast/blob/master/bcast.go#L114

go func(out chan interface{}, received *Message) { // non blocking
     out <- received.payload
}(member, &received)

This code can block, and keep the goroutine around, effectively being a memory leak.

Along with other gotchas (like reordering the messages), and other race condition, it makes this library unsuitable for any serious usage.

While I appreciate your effort and know this software comes with no warranty, please indicate that this library is buggy in README, delete it or accept the PRs from various sources addressing problems with it.

Calling 'go group.Broadcasting(0)' results in tight loop

When invoking the Broadcasting method on a created Group with duration zero, the for loop in the function enters a tight loop. I believe this is because the test of

case <-time.After(timeout):

is always satisfied when timeout is zero. I tested out the addition of the following routine in my local build. When called the CPU usage is back to normal:

func (r *Group) ContinuousBroadcasting() {
    for {
        select {
        case received := <-r.in:
            switch received.payload.(type) {
            default: // receive a payload and broadcast it
                for _, member := range r.Members() {
                    if received.sender != member { // not return broadcast to sender

                        go func(out chan interface{}, received *Message) { // non blocking
                            out <- received.payload
                        }(member, &received)

                    }
                }
            }
        }
    }
}

Let me know if you want a pull request, or perhaps you'd like to refactor the select statement in Broadcasting yourself. BTW, love the package. Thanks!

Group function Leave return without memberLock unlocked

// Leave removes the provided member from the group
func (g *Group) Leave(leaving *Member) error {
	g.memberLock.Lock()
	memberIndex := -1
	for index, member := range g.members {
		if member == leaving {
			memberIndex = index
			break
		}
	}
	if memberIndex == -1 {
                // here, should unlock the lock
		return errors.New("Could not find provided memeber for removal")
	}
	g.members = append(g.members[:memberIndex], g.members[memberIndex+1:]...)
	leaving.close <- true // TODO: need to handle the case where there
	// is still stuff in this Members priorityQueue
	g.memberLock.Unlock()
	return nil
}

question: what happends with read channel

So i have a piece of code:

for {                                                     
  msg := v.Recv()                                      
  c.Send(msg.(string))                               
}

What exactly happens when v(Member) closes? Will it infinitely block the reading? Because if it does, how this will affect my program, will GC recognize this as an infinitely blocking channel? Or what exactly happens?
So if something is smelly happens why not export some indicator that the Member is closed to stop this for loop?

tests fail on Go 1.5.1

Hey there, I was looking into your bcast library and noticed some tests fail on my machine with Go 1.5.1:

dq@empit:bcast$ go test -v
=== RUN   TestNewGroupAndJoin
--- PASS: TestNewGroupAndJoin (0.00s)
=== RUN   TestUnjoin
--- PASS: TestUnjoin (0.00s)
=== RUN   TestBroadcast
--- FAIL: TestBroadcast (0.10s)
    bcast_test.go:85: not all messages broadcasted
=== RUN   TestBroadcastFromMember
--- FAIL: TestBroadcastFromMember (0.10s)
    bcast_test.go:114: not all messages broadcasted
=== RUN   TestGroupBroadcast
--- FAIL: TestGroupBroadcast (0.15s)
    bcast_test.go:141: not all messages broadcasted
=== RUN   TestBroadcastOnLargeNumberOfMembers
--- FAIL: TestBroadcastOnLargeNumberOfMembers (1.05s)
    bcast_test.go:168: not all messages broadcasted (11267/16256)
FAIL
exit status 1
FAIL    github.com/grafov/bcast 1.415s
dq@empit:bcast$ go version
go version go1.5.1 linux/amd64

cheers/

[Go1.9] New release to satify `dep` tool

I use your awesome library for years, but when switching to go1.9 dep seems to favour a released version instead of the master branch - somehow makes sense. :)

Can you release a new version, so that it can be picked up?

Documentation is out of date

The README.md is no longer reflective of changes that seem to have been made in the past few days. I can see the pull request that was merged suggests a "massive refactor"... it would be quite useful to know what has actually changed and how to update my code.

broadcast

Hi,

can you select between buffered and unbuffered broadcasting channel?

I would like to send a broadcast message to routine1 and routine2 using a broadcast channel...

go receiveData() {
broadcast <- rcv
}

go routine1() {
msg := <- broadcast
}

go routine2() {
msg := <- broadcast
}

Thanks,
Gerald

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.