Skip to content

Instantly share code, notes, and snippets.

@kylelemons
Created April 30, 2012 22:47
Show Gist options
  • Save kylelemons/2563380 to your computer and use it in GitHub Desktop.
Save kylelemons/2563380 to your computer and use it in GitHub Desktop.
Untested method of registering and unregstering broadcast channels
func manager() {
chans := map[chan data]bool
active := sync.WaitGroup{}
for {
select {
case c := <-register:
chans[c] = true
case c := <-unregister:
active.Wait()
delete(chans, c)
close(c)
case d := <-broadcast:
current := make([]chan data, 0, len(chans))
for c := range chans {
current = append(current, c)
}
active.Add(1)
go func(d data) {
defer active.Done()
for _, c := range current {
c <- d
}
}(d)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment