|
|
|
|
@ -13,19 +13,19 @@ import (
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func producer(stream Stream) (tweets []*Tweet) {
|
|
|
|
|
func producer(stream Stream, ch chan *Tweet) {
|
|
|
|
|
for {
|
|
|
|
|
tweet, err := stream.Next()
|
|
|
|
|
if err == ErrEOF {
|
|
|
|
|
return tweets
|
|
|
|
|
close(ch)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tweets = append(tweets, tweet)
|
|
|
|
|
ch <- tweet
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func consumer(tweets []*Tweet) {
|
|
|
|
|
for _, t := range tweets {
|
|
|
|
|
func consumer(ch chan *Tweet) {
|
|
|
|
|
for t := range ch {
|
|
|
|
|
if t.IsTalkingAboutGo() {
|
|
|
|
|
fmt.Println(t.Username, "\ttweets about golang")
|
|
|
|
|
} else {
|
|
|
|
|
@ -38,11 +38,13 @@ func main() {
|
|
|
|
|
start := time.Now()
|
|
|
|
|
stream := GetMockStream()
|
|
|
|
|
|
|
|
|
|
ch := make(chan *Tweet)
|
|
|
|
|
|
|
|
|
|
// Producer
|
|
|
|
|
tweets := producer(stream)
|
|
|
|
|
go producer(stream, ch)
|
|
|
|
|
|
|
|
|
|
// Consumer
|
|
|
|
|
consumer(tweets)
|
|
|
|
|
go consumer(ch)
|
|
|
|
|
|
|
|
|
|
fmt.Printf("Process took %s\n", time.Since(start))
|
|
|
|
|
}
|
|
|
|
|
|