Skip to content

Commit

Permalink
add N of workers flag and truncate number of comments limit
Browse files Browse the repository at this point in the history
  • Loading branch information
azimut committed Jul 27, 2022
1 parent 7b8688a commit 2b2e7ad
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
5 changes: 3 additions & 2 deletions cmd/hackerview/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (

type options struct {
timeout time.Duration
userAgent string
useColors bool
usePretty bool
width int
limit int
workers uint
}

var opt options
Expand All @@ -30,6 +30,7 @@ func init() {
flag.BoolVar(&opt.usePretty, "P", true, "use pretty formatting")
flag.IntVar(&opt.width, "w", 80, "fixed with")
flag.IntVar(&opt.limit, "l", 0, "limits the ammount of comments to fetch")
flag.UintVar(&opt.workers, "W", 3, "number of workers to fetch comments")
}

func usage() {
Expand All @@ -45,7 +46,7 @@ func run(args []string, stdout io.Writer) error {
return errors.New("missing URL argument")
}
url = flag.Args()[0]
op, comments, err := hackernews.Fetch(url, opt.timeout, opt.limit)
op, comments, err := hackernews.Fetch(url, opt.timeout, opt.limit, opt.workers)
if err != nil {
return errors.New("could not fetch url")
}
Expand Down
31 changes: 19 additions & 12 deletions internal/hackernews/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"github.com/caser/gophernews"
)

const NUM_OF_WORKERS = 3

func unix2time(t int) time.Time {
return time.Unix(int64(t), 0)
}
Expand All @@ -26,7 +24,12 @@ func newOp(story *gophernews.Story, selfUrl string) Op {
}
}

func Fetch(rawUrl string, timeout time.Duration, limit int) (doc *Op, c *[]Comment, err error) {
func Fetch(
rawUrl string,
timeout time.Duration,
limit int,
workers uint,
) (doc *Op, c *[]Comment, err error) {
url, storyId, err := effectiveUrl(rawUrl)
if err != nil {
return nil, nil, err
Expand All @@ -38,13 +41,21 @@ func Fetch(rawUrl string, timeout time.Duration, limit int) (doc *Op, c *[]Comme
}
op := newOp(story, url)
ids := story.Kids
limit = min(len(ids), limit)
if limit > 0 {
ids = ids[:limit]
}
comments := fetchComments(ids) // TODO: error
comments := fetchComments(ids, workers) // TODO: error
return &op, &comments, nil
}

func min(a, b int) int {
if a < b {
return a
}
return b
}

func fetchStory(client *gophernews.Client, id int) (*gophernews.Story, error) {
story, err := client.GetStory(id)
if err != nil {
Expand All @@ -56,14 +67,16 @@ func fetchStory(client *gophernews.Client, id int) (*gophernews.Story, error) {
return &story, nil
}

func fetchComments(comments []int) []Comment {
func fetchComments(comments []int, workers uint) []Comment {
idsChan := make(chan int, 5)
commentsCh := make(chan result, 5)
var wg sync.WaitGroup
wg.Add(len(comments))
go sendWork(comments, idsChan)
go closeAfterWait(&wg, commentsCh)
spawnWorkers(&wg, idsChan, commentsCh)
for i := 0; i < int(workers); i++ {
go worker(&wg, idsChan, commentsCh)
}
return collector(&wg, idsChan, commentsCh)
}

Expand All @@ -79,12 +92,6 @@ func sendWork(ids []int, input chan<- int) {
close(input)
}

func spawnWorkers(wg *sync.WaitGroup, in <-chan int, out chan<- result) {
for i := 0; i < NUM_OF_WORKERS; i++ {
go worker(wg, in, out)
}
}

type result struct {
comment gophernews.Comment
err error
Expand Down

0 comments on commit 2b2e7ad

Please sign in to comment.