Skip to content

Commit

Permalink
limit goroutines to cpu count
Browse files Browse the repository at this point in the history
  • Loading branch information
TiltedToast committed Jul 24, 2022
1 parent f85d761 commit f6373e4
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

# Test binary, built with `go test -c`
*.test
output/
/output

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
)

require (
github.com/alitto/pond v1.8.0
github.com/andres-erbsen/clock v0.0.0-20160526145045-9e14626cd129 // indirect
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U=
github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI=
github.com/alitto/pond v1.8.0 h1:/4wnAU0vOjhsUxOxjtXuNb59oh0J+Jjukf6gtkWpGJk=
github.com/alitto/pond v1.8.0/go.mod h1:xQn3P/sHTYcU/1BR3i86IGIrilcrGC2LiS+E2+CJWsI=
github.com/andres-erbsen/clock v0.0.0-20160526145045-9e14626cd129 h1:MzBOUgng9orim59UnfUTLRjMpd09C5uEVQ6RPGeCaVI=
github.com/andres-erbsen/clock v0.0.0-20160526145045-9e14626cd129/go.mod h1:rFgpPQZYZ8vdbc+48xibu8ALc3yeyd64IhHS+PU6Yyg=
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
Expand Down
18 changes: 12 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ package main
import (
"encoding/json"
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/schollz/progressbar/v3"
"go.uber.org/ratelimit"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
"strconv"
"sync"
"time"

"github.com/PuerkitoBio/goquery"
"github.com/schollz/progressbar/v3"
"go.uber.org/ratelimit"
)

type inputOptions struct {
Expand Down Expand Up @@ -99,7 +101,7 @@ func main() {
}

dl_bar := progressbar.NewOptions(len(posts),
progressbar.OptionSetDescription(fmt.Sprintf("Downloading posts")),
progressbar.OptionSetDescription("Downloading posts"),
progressbar.OptionEnableColorCodes(true),
progressbar.OptionFullWidth(),
progressbar.OptionShowCount(),
Expand All @@ -116,11 +118,16 @@ func main() {
wg.Add(len(posts))
start := time.Now()

maxGoroutines := runtime.NumCPU()
guard := make(chan Post, maxGoroutines)

for _, post := range posts {
guard <- post
go func(post Post) {
defer wg.Done()
downloadPost(post, options)
dl_bar.Add(1)
<-guard
}(post)
}
wg.Wait()
Expand Down Expand Up @@ -174,7 +181,6 @@ func downloadPost(post Post, options inputOptions) {
fmt.Println("Error writing post:", post.ID)
return
}

}

func fetchPostsFromPage(tag string, totalPageAmount int) []Post {
Expand All @@ -185,7 +191,7 @@ func fetchPostsFromPage(tag string, totalPageAmount int) []Post {
rl := ratelimit.New(10)

pages_bar := progressbar.NewOptions(totalPageAmount,
progressbar.OptionSetDescription(fmt.Sprintf("Fetching posts")),
progressbar.OptionSetDescription("Fetching posts"),
progressbar.OptionEnableColorCodes(true),
progressbar.OptionFullWidth(),
progressbar.OptionShowCount(),
Expand Down

0 comments on commit f6373e4

Please sign in to comment.