Skip to content

First attempt at solving problems #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions 0-limit-crawler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ package main
import (
"fmt"
"sync"
"time"
)

// Crawl uses `fetcher` from the `mockfetcher.go` file to imitate a
// real crawler. It crawls until the maximum depth has reached.
func Crawl(url string, depth int, wg *sync.WaitGroup) {
func Crawl(url string, depth int, wg *sync.WaitGroup, throttle *time.Ticker) {
defer wg.Done()

if depth <= 0 {
Expand All @@ -35,14 +36,16 @@ func Crawl(url string, depth int, wg *sync.WaitGroup) {
for _, u := range urls {
// Do not remove the `go` keyword, as Crawl() must be
// called concurrently
go Crawl(u, depth-1, wg)
<-throttle.C
go Crawl(u, depth-1, wg, throttle)
}
}

func main() {
var wg sync.WaitGroup

wg.Add(1)
Crawl("http://golang.org/", 4, &wg)
throttle := time.NewTicker(2 * time.Second)
Crawl("http://golang.org/", 4, &wg, throttle)
wg.Wait()
}
24 changes: 16 additions & 8 deletions 1-producer-consumer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,25 @@ package main

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

func producer(stream Stream) (tweets []*Tweet) {
func producer(stream Stream, conn chan *Tweet, wg *sync.WaitGroup) {
wg.Done()
for {
tweet, err := stream.Next()
if err == ErrEOF {
return tweets
close(conn)
return
}

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

func consumer(tweets []*Tweet) {
for _, t := range tweets {
func consumer(conn chan *Tweet, wg *sync.WaitGroup) {
defer wg.Done()
for t := range conn {
if t.IsTalkingAboutGo() {
fmt.Println(t.Username, "\ttweets about golang")
} else {
Expand All @@ -38,11 +41,16 @@ func main() {
start := time.Now()
stream := GetMockStream()

conn := make(chan *Tweet)
var wg sync.WaitGroup
wg.Add(2)
// Producer
tweets := producer(stream)
go producer(stream, conn, &wg)

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

wg.Wait()

fmt.Printf("Process took %s\n", time.Since(start))
}
17 changes: 15 additions & 2 deletions 3-limit-service-time/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

package main

import "time"

// User defines the UserModel. Use this to check whether a User is a
// Premium user or not
type User struct {
Expand All @@ -21,8 +23,19 @@ type User struct {
// HandleRequest runs the processes requested by users. Returns false
// if process had to be killed
func HandleRequest(process func(), u *User) bool {
process()
return true

timer := time.NewTimer(10 * time.Second)
done := make(chan bool)
go func() {
process()
done <- true
}()
select {
case <-timer.C:
return false
case <-done:
return true
}
}

func main() {
Expand Down