Skip to content

Commit

Permalink
Challenge loong#1 completed
Browse files Browse the repository at this point in the history
  • Loading branch information
n-bruno authored Jun 1, 2022
1 parent 7624971 commit 5497325
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions 1-producer-consumer/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//////////////////////////////////////////////////////////////////////
//
// Given is a producer-consumer szenario, where a producer reads in
// Given is a producer-consumer scenario, where a producer reads in
// tweets from a mockstream and a consumer is processing the
// data. Your task is to change the code so that the producer as well
// as the consumer can run concurrently
Expand All @@ -10,22 +10,27 @@ package main

import (
"fmt"
"sync"
"time"
)

func producer(stream Stream) (tweets []*Tweet) {
func producer(wg *sync.WaitGroup, stream Stream, tweets chan *Tweet) {
defer wg.Done()

for {
tweet, err := stream.Next()
if err == ErrEOF {
return tweets
return
}

tweets = append(tweets, tweet)
tweets <- tweet
}
}

func consumer(tweets []*Tweet) {
for _, t := range tweets {
func consumer(wg *sync.WaitGroup, tweets <-chan *Tweet) {
defer wg.Done()

for t := range tweets {
if t.IsTalkingAboutGo() {
fmt.Println(t.Username, "\ttweets about golang")
} else {
Expand All @@ -35,14 +40,29 @@ func consumer(tweets []*Tweet) {
}

func main() {

// This also works without making consumer a go func
// but I thought I'd practice with wait groups.
// Both functions are running concurrently, however,
// consumer only finishes after producer does (as it's dependant on it),
// making the wait at the end a bit unnecessary.
wg := new(sync.WaitGroup)

// Two go funcs hence the number two here and two "defer wg.Done()"
wg.Add(2)

start := time.Now()
stream := GetMockStream()

// Producer
tweets := producer(stream)
tweets := make(chan *Tweet)
go producer(wg, stream, tweets)

// Consumer
consumer(tweets)
go consumer(wg, tweets)

// wait for the go routines to finish
wg.Wait()

fmt.Printf("Process took %s\n", time.Since(start))
}

0 comments on commit 5497325

Please sign in to comment.