-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
101 lines (79 loc) · 2.09 KB
/
main.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"fmt"
go_concurrent_pipeline "github.com/ankit-arora/go-utils/go-concurrent-pipeline"
go_pipeline "github.com/ankit-arora/go-utils/go-concurrent-pipeline/go-pipeline"
goroutines_pool "github.com/ankit-arora/go-utils/go-concurrent-pipeline/goroutines-pool"
go_shutdown_hook "github.com/ankit-arora/go-utils/go-shutdown-hook"
"strconv"
"time"
)
func main() {
//only pool example starts
p, err := goroutines_pool.NewPool(5)
if err != nil {
fmt.Println(err)
return
}
go_shutdown_hook.ADD(func() {
fmt.Println("stopping pool p")
p.Shutdown()
fmt.Println("stopping pool p-done")
})
for i := 0; i <= 10; i++ {
j := i
p.Submit(func() {
fmt.Println("task " + strconv.Itoa(j))
})
}
//only pool example ends
//only pipeline example starts
pipeline, err := go_pipeline.NewPipeline(10, 2*time.Second, func(key string, data []interface{}) {
fmt.Print(key + " -> ")
fmt.Println(data)
})
go_shutdown_hook.ADD(func() {
fmt.Println("shutting down pipeline")
pipeline.Shutdown()
fmt.Println("shutting down pipeline-done")
})
for i := 0; i < 52; i++ {
pipeline.Add("0-pipeline", i)
pipeline.Add("1-pipeline", i*2)
}
pipeline.Add("2-pipeline", "foo")
pipeline.Add("2-pipeline", "bar")
//time.Sleep(1 * time.Second)
go func() {
pipeline.Add("2-pipeline", "foo-1")
}()
pipeline.Add("2-pipeline", "bar-1")
//time.Sleep(10 * time.Second)
//only pipeline example ends
//concurrent pipeline example starts
cp, err := go_concurrent_pipeline.NewConcurrentPipeline(10, 10, 3*time.Second,
func(key int, i []string) {
fmt.Printf("%d->%s\n", key, i)
})
go_shutdown_hook.ADD(func() {
fmt.Println("shutting down concurrent_pipeline")
cp.Shutdown()
fmt.Println("shutting down concurrent_pipeline-done")
})
if err != nil {
fmt.Println(err)
return
}
for i := 0; i < 51; i++ {
cp.Add(10, fmt.Sprintf("%d", i))
}
time.Sleep(10 * time.Second)
for i := 51; i < 101; i++ {
cp.Add(11, fmt.Sprintf("%d", i))
}
for i := 101; i < 151; i++ {
cp.Add(10, fmt.Sprintf("%d", i))
}
////concurrent pipeline example ends
go_shutdown_hook.Wait()
}