Skip to content

fix downloadPages func in listing 9-11 #8

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
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
141 changes: 69 additions & 72 deletions chapter9/listing9.9_11/extractwordsmulti.go
Original file line number Diff line number Diff line change
@@ -1,89 +1,86 @@
package main

import (
"fmt"
"github.com/cutajarj/ConcurrentProgrammingWithGo/chapter9/listing9.10"
"io"
"net/http"
"regexp"
"strings"
"fmt"
"github.com/cutajarj/ConcurrentProgrammingWithGo/chapter9/listing9.10"
"io"
"net/http"
"regexp"
"strings"
)

func extractWords(quit <-chan int, pages <-chan string) <-chan string {
words := make(chan string)
go func() {
defer close(words)
wordRegex := regexp.MustCompile(`[a-zA-Z]+`)
moreData, page := true, ""
for moreData {
select {
case page, moreData = <-pages:
if moreData {
for _, word := range wordRegex.FindAllString(page, -1) {
words <- strings.ToLower(word)
}
}
case <-quit:
return
}
}
}()
return words
words := make(chan string)
go func() {
defer close(words)
wordRegex := regexp.MustCompile(`[a-zA-Z]+`)
moreData, page := true, ""
for moreData {
select {
case page, moreData = <-pages:
if moreData {
for _, word := range wordRegex.FindAllString(page, -1) {
words <- strings.ToLower(word)
}
}
case <-quit:
return
}
}
}()
return words
}

func downloadPages(quit <-chan int, urls <-chan string) <-chan string {
pages := make(chan string)
go func() {
defer close(pages)
moreData, url := true, ""
for moreData {
select {
case url, moreData = <-urls:
if moreData {
resp, _ := http.Get(url)
if resp.StatusCode != 200 {
panic("Server's error: " + resp.Status)
}
body, _ := io.ReadAll(resp.Body)
pages <- string(body)
resp.Body.Close()
}
case <-quit:
return
}
}
}()
return pages
pages := make(chan string)
go func() {
defer close(pages)
select {
case url, ok := <-urls:
if ok {
resp, _ := http.Get(url)
if resp.StatusCode != 200 {
panic("Server's error: " + resp.Status)
}
body, _ := io.ReadAll(resp.Body)
pages <- string(body)
resp.Body.Close()
}
case <-quit:
return
}
}()
return pages
}

func generateUrls(quit <-chan int) <-chan string {
urls := make(chan string)
go func() {
defer close(urls)
for i := 100; i <= 130; i++ {
url := fmt.Sprintf("https://rfc-editor.org/rfc/rfc%d.txt", i)
select {
case urls <- url:
case <-quit:
return
}
}
}()
return urls
urls := make(chan string)
go func() {
defer close(urls)
for i := 100; i <= 130; i++ {
url := fmt.Sprintf("https://rfc-editor.org/rfc/rfc%d.txt", i)
select {
case urls <- url:
case <-quit:
return
}
}
}()
return urls
}

const downloaders = 20

func main() {
quit := make(chan int)
defer close(quit)
urls := generateUrls(quit)
pages := make([]<-chan string, downloaders)
for i := 0; i < downloaders; i++ {
pages[i] = downloadPages(quit, urls)
}
results := extractWords(quit, listing9_10.FanIn(quit, pages...))
for result := range results {
fmt.Println(result)
}
}
quit := make(chan int)
defer close(quit)
urls := generateUrls(quit)
pages := make([]<-chan string, downloaders)
for i := 0; i < downloaders; i++ {
pages[i] = downloadPages(quit, urls)
}
results := extractWords(quit, listing9_10.FanIn(quit, pages...))
for result := range results {
fmt.Println(result)
}
}