-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.go
64 lines (48 loc) · 876 Bytes
/
gen.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
package infake
import (
"log"
"sync"
"math/rand"
)
type Gen struct {
RndSrc rand.Source
Series []*Series
}
func NewGen(cfg Config) (*Gen, error) {
rndSrc := &LockedSource{src: rand.NewSource(cfg.Seed)}
gen := &Gen{
RndSrc: rndSrc,
}
gen.Series = make([]*Series, len(cfg.Series))
for i, seriesCfg := range cfg.Series {
s, err := NewSeries(seriesCfg)
if err != nil {
return nil, err
}
gen.Series[i] = s
}
return gen, nil
}
func (gen *Gen) Generate() (<-chan Point, error) {
var wg sync.WaitGroup
c := make(chan Point)
wg.Add(len(gen.Series))
for _, series := range gen.Series {
go func(series *Series) {
defer wg.Done()
sc, err := series.Generate(gen.RndSrc)
if err != nil {
log.Print(err)
return
}
for p := range sc {
c <- p
}
}(series)
}
go func() {
wg.Wait()
close(c)
}()
return c, nil
}