Skip to content

Instantly share code, notes, and snippets.

@mountkin
Created February 6, 2016 05:26
Show Gist options
  • Save mountkin/8d982e4611467ba87ac2 to your computer and use it in GitHub Desktop.
Save mountkin/8d982e4611467ba87ac2 to your computer and use it in GitHub Desktop.
package main
import (
"time"
"github.com/docker/docker/pkg/pubsub"
)
func main() {
pub := pubsub.NewPublisher(0, 1)
sub := pub.Subscribe()
go func() {
for i := 0; i < 10; i++ {
pub.Publish(i)
time.Sleep(time.Millisecond * 500)
}
pub.Evict(sub)
}()
for {
select {
case i, ok := <-sub:
if !ok {
return
}
println(i.(int))
time.Sleep(time.Second)
}
}
}
@mountkin
Copy link
Author

mountkin commented Feb 6, 2016

If the subscriber is slower than the publisher, some of the messages will be lost.

Run the code you'll find the output is something like

0
1
2
4
6
8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment