-
Notifications
You must be signed in to change notification settings - Fork 1
/
consumer.go
54 lines (47 loc) · 1.03 KB
/
consumer.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
package player
import (
"context"
"sync"
"github.com/rs/zerolog/log"
)
// Player is interface to replay original data from provided filename.
type Player interface {
Play(filename string) error
}
// Consumer consumes a batch of files to process then concurrently.
type Consumer struct {
filenames *chan string
jobs chan string
replayer Player
}
// NewConsumer creates instance of Consumer with given parameters.
func NewConsumer(l *chan string, j chan string, r Player) *Consumer {
c := &Consumer{filenames: l, jobs: j, replayer: r}
return c
}
// Work does work.
func (c *Consumer) Work(wg *sync.WaitGroup) {
defer wg.Done()
for job := range c.jobs {
if job == "" {
return
}
err := c.replayer.Play(job)
if err != nil {
log.Error().Err(err).Str("job", job).Msg("replay failed")
return
}
}
}
// Consume consumes workload and pushes for workers.
func (c *Consumer) Consume(ctx context.Context) {
for {
select {
case job := <-*c.filenames:
c.jobs <- job
case <-ctx.Done():
close(c.jobs)
return
}
}
}