forked from peterhellberg/hn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getitems.go
72 lines (57 loc) · 1.33 KB
/
getitems.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package hn
import (
"context"
"fmt"
"sync"
"sync/atomic"
"time"
)
const defaultMaxGoroutines = 20
// GetItems gets a set of from the HN API in parallel.
func (client *Client) GetItems(ctx context.Context, items []int, maxGoroutines int) ([]Item, error) {
if maxGoroutines == 0 {
maxGoroutines = defaultMaxGoroutines
}
n := len(items)
results := make([]Item, n)
// Use a channel as a rate limiter. If there are more than
// nGoroutines running reading from the channel will block
// until one goroutine releases.
sem := make(chan struct{}, maxGoroutines)
acquire := func() { sem <- struct{}{} }
release := func() { <-sem }
var wg sync.WaitGroup
var nSuccess int64
contextCanceled := false
STORIES:
for index, itemID := range items {
acquire()
wg.Add(1)
go func(idx, id int) {
defer release()
defer wg.Done()
c, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
item, err := client.Item(c, id)
if err != nil {
return
}
atomic.AddInt64(&nSuccess, 1)
results[idx] = *item
}(index, itemID)
select {
case <-ctx.Done():
contextCanceled = true
break STORIES
default:
}
}
wg.Wait()
if nSuccess != int64(n) {
if contextCanceled {
return results, ctx.Err()
}
return results, fmt.Errorf("Didn't successfully fetch all items.")
}
return results, nil
}