Created
April 30, 2012 22:47
-
-
Save kylelemons/2563380 to your computer and use it in GitHub Desktop.
Untested method of registering and unregstering broadcast channels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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