-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataflow.go
executable file
·153 lines (140 loc) · 3.77 KB
/
dataflow.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package dataflow
import (
"context"
"log"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
)
const (
DEFAULT_WORKERS = 2000
DEFAULT_CHAN_LEN = 1000
INPUT_STAGE = "input"
SOURCE_STAGE = "source"
OUTPUT_STAGE = "output"
)
type CallbackType func(ctx context.Context, rch chan interface{}, wch chan interface{}, wg *sync.WaitGroup)
type CancelFunc func()
type Stage struct {
StageType string
Stages []*Stage
ApplyFunc CallbackType
Wg sync.WaitGroup
Cancel context.CancelFunc
Ctx context.Context
ReaderChan chan interface{}
WriteChan chan interface{}
Workers int
ChanCap int
ChanOn bool
test int
}
var DataFlow Stage
func init() {
DataFlow.WriteChan = make(chan interface{}, 1000)
DataFlow.Ctx, DataFlow.Cancel = context.WithCancel(context.Background())
}
func (s *Stage) Input(Callback CallbackType, Workers int, ChanLen int) *Stage {
e := new(Stage)
e.StageType = INPUT_STAGE
e.Workers = DEFAULT_WORKERS
if Workers > 0 {
e.Workers = Workers
}
e.ApplyFunc = Callback
s.Stages = append(s.Stages, e)
return e
}
func (s *Stage) Register(Callback CallbackType, Workers int, ChanLen int) *Stage {
e := new(Stage)
e.Workers = DEFAULT_WORKERS
if Workers > 0 {
e.Workers = Workers
}
if ChanLen > 0 {
e.WriteChan = make(chan interface{}, ChanLen)
} else {
e.WriteChan = make(chan interface{}, DEFAULT_CHAN_LEN)
}
e.ApplyFunc = Callback
e.ChanOn = true
s.Stages = append(s.Stages, e)
return e
}
func (s *Stage) Run() {
if len(s.Stages) > 0 {
lastStage := s
for _, Stage := range s.Stages {
if Stage.StageType == INPUT_STAGE {
for i := 0; i < Stage.Workers; i++ {
s.Wg.Add(1)
go Stage.ApplyFunc(s.Ctx, s.ReaderChan, s.WriteChan, &s.Wg)
}
continue
}
for i := 0; i < Stage.Workers; i++ {
s.Wg.Add(1)
go Stage.ApplyFunc(s.Ctx, lastStage.WriteChan, Stage.WriteChan, &s.Wg)
}
lastStage = Stage
}
}
if strings.Contains(os.Args[0], "Test") {
log.Println("Stage Run End")
time.Sleep(20 * time.Second)
s.Stop()
log.Println("Stage Run Wait after")
}
}
func (s *Stage) Listen() {
s.Wg.Add(1)
go func() {
defer s.Wg.Done()
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGUSR1, syscall.SIGUSR2)
for si := range c {
switch si {
case syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT:
log.Println("\nService Stoping By signal:", si)
s.Stop()
return
case syscall.SIGUSR1:
log.Println("usr1", s)
case syscall.SIGUSR2:
log.Println("usr2", s)
default:
log.Println("other", s)
}
}
}()
s.Wg.Wait()
}
func (s *Stage) Stop() {
log.Println("Stop")
s.Cancel()
time.Sleep(3 * time.Second)
s.CloseChannel(s.WriteChan)
if len(s.Stages) > 0 {
for idx, Stage := range s.Stages {
log.Println("stage", idx, Stage.StageType)
if Stage.ChanOn == true {
s.CloseChannel(Stage.WriteChan)
}
}
}
log.Println("Clear end")
return
}
func (s *Stage) CloseChannel(c chan interface{}) {
for {
log.Println("CloseChannel ch len:", len(c))
if len(c) == 0 {
close(c)
return
}
time.Sleep(1 * time.Second)
}
}