Created
February 6, 2016 05:26
-
-
Save mountkin/8d982e4611467ba87ac2 to your computer and use it in GitHub Desktop.
This file contains 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
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) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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