-
Notifications
You must be signed in to change notification settings - Fork 0
/
work.go
81 lines (68 loc) · 1.32 KB
/
work.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
73
74
75
76
77
78
79
80
81
package collection
import (
"sync"
)
// SongTasks runs a bunch of tasks on a song concurrenly
// with a given ratelimiter.
type SongTasks struct {
concurrency int
rate <-chan struct{}
qrw sync.RWMutex
queue chan Song
filter func(Song) bool
cb func(Song)
}
// NewSongTasks creates a new SongTask that will execute cb() for every item
// that returns true when passed through filter().
// filter will also be executed concurrenly but never ratelimited.
func NewSongTasks(
concurrency int,
rate <-chan struct{},
filter func(Song) bool,
cb func(Song),
) *SongTasks {
return &SongTasks{
concurrency: concurrency,
rate: rate,
queue: make(chan Song, concurrency),
filter: filter,
cb: cb,
}
}
func (t *SongTasks) Start() {
list := make([]Song, 0)
for i := 0; i < t.concurrency; i++ {
go func() {
for s := range t.queue {
if !t.filter(s) {
continue
}
t.qrw.Lock()
list = append(list, s)
t.qrw.Unlock()
}
}()
go func() {
for range t.rate {
t.qrw.RLock()
l := len(list)
t.qrw.RUnlock()
if l == 0 {
continue
}
t.qrw.Lock()
if len(list) == 0 {
t.qrw.Unlock()
continue
}
s := list[0]
list = list[1:]
t.qrw.Unlock()
t.cb(s)
}
}()
}
}
func (t *SongTasks) Add(s Song) {
t.queue <- s
}