/
channel.go
69 lines (55 loc) · 1.35 KB
/
channel.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
package main
import (
"fmt"
"math/rand"
"sync"
"time"
"github.com/konimarti/pipeline"
"github.com/konimarti/pipeline/filters"
)
func main() {
// set up channel
ch := make(chan interface{})
// create channel-based pipeline and set an OnValue trigger.
// The pipeline will send notifications every time the defined value 3
// is send through the channel.
monitor := pipeline.NewFromChan(&filters.OnValue{3}, ch)
defer monitor.Close()
// syncrhoniztion
var wg sync.WaitGroup
// publisher: random numbers to be added in irregular intervals
wg.Add(2)
go publisher(1, ch, &wg)
go publisher(2, ch, &wg)
// subscribers
wg.Add(2)
go subscriber(1, monitor, &wg)
go subscriber(2, monitor, &wg)
wg.Wait()
}
func publisher(id int, ch chan interface{}, wg *sync.WaitGroup) {
var counter int
for {
val := rand.Intn(4)
if val == 3 {
counter++
}
ch <- val
sleep := rand.Intn(2)
fmt.Printf("Publisher %d sends: value = %v, counts = %d, sleeps = %d sec \n", id, val, counter, sleep)
time.Sleep(time.Duration(sleep) * time.Second)
if counter >= 5 {
break
}
}
wg.Done()
}
func subscriber(id int, monitor pipeline.Observer, wg *sync.WaitGroup) {
sub := monitor.Subscribe()
for i := 1; i < 10; i++ {
<-sub.Event()
fmt.Printf("Subscriber %d got notified: value = %v, counts = %d\n", id, sub.Value(), i)
sub.Next()
}
wg.Done()
}