https://github.com/atedja/go-eventcast
Simple event broadcasting
https://github.com/atedja/go-eventcast
channels event-broadcasting go golang
Last synced: 6 months ago
JSON representation
Simple event broadcasting
- Host: GitHub
- URL: https://github.com/atedja/go-eventcast
- Owner: atedja
- License: other
- Created: 2015-11-24T09:06:08.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2017-03-12T01:09:00.000Z (over 9 years ago)
- Last Synced: 2024-06-20T07:55:40.224Z (about 2 years ago)
- Topics: channels, event-broadcasting, go, golang
- Language: Go
- Homepage:
- Size: 6.84 KB
- Stars: 22
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# eventcast
[](https://travis-ci.org/atedja/go-eventcast)
Simple event broadcasting.
### Examples
#### Signaling arbitrary number of workers
```go
// spawn workers
for i := 0; i < 100; i++ {
go func() {
closed := eventcast.Listen("we are closed")
for {
select {
case <-closed:
return
default:
// do other things
}
}
}()
}
// somewhere, sometime later..
eventcast.Broadcast("we are closed")
```
#### Broadcasting a value to multiple listeners
```go
// goroutines waiting for some result or timeout.
for i := 0; i < 10; i++ {
go func() {
select {
case value := <-eventcast.Listen("result"):
// do something with value
case <-time.After(1 * time.Second):
// timeout!
break
}
}()
}
// some worker
go func() {
// doing something...
value := "result of processing some data"
eventcast.BroadcastWithValue("result", value)
// continue doing more
}()
```
#### Racing Your Pigs
```go
// go pig!
finished := eventcast.Listen("finished")
for i := 0; i < 10; i++ {
go func(i int) {
<-eventcast.Listen("ready")
<-eventcast.Listen("set")
<-eventcast.Listen("go")
time.Sleep(time.Duration(rand.Intn(5000)) * time.Millisecond)
eventcast.BroadcastWithValue("finished", i)
}(i)
}
// Allow some time for the pigs to get ready
<-time.After(10 * time.Milisecond)
eventcast.Broadcast("ready")
<-time.After(1 * time.Second)
eventcast.Broadcast("set")
<-time.After(1 * time.Second)
eventcast.Broadcast("go")
winner := <-finished
fmt.Println("Winner is Pig", winner.(int))
```