-
Notifications
You must be signed in to change notification settings - Fork 0
/
slidewindow.go
187 lines (156 loc) · 3.44 KB
/
slidewindow.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package slidewindow
import (
"context"
"errors"
"sync"
)
type SlideWindow struct {
Concurrency uint64
Total uint64
Init func(ctx context.Context, s *Session) error // running in order
Task func(ctx context.Context, s *Session) error // running concurrently
Done func(ctx context.Context, s *Session) error // running in order
}
type Session struct {
index uint64
values sync.Map
}
func (s *Session) Set(key string, value interface{}) {
s.values.Store(key, value)
}
func (s *Session) Get(key string) (interface{}, bool) {
return s.values.Load(key)
}
func (s *Session) Index() uint64 {
return s.index
}
func (sw *SlideWindow) Start(ctx context.Context) error {
if sw.Total*sw.Concurrency == 0 {
return errors.New("param 'Total' and 'Concurrency' cannot be zero")
}
if sw.Init == nil || sw.Task == nil || sw.Done == nil {
return errors.New("method Init、Task、Done cannot be nil")
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
var errOccured error
taskQueue := make(chan *Session, sw.Concurrency)
finishQueue := make(chan *Session, sw.Concurrency)
wg := sync.WaitGroup{}
// start task consumers
wg.Add(int(sw.Concurrency))
for i := 0; i < int(sw.Concurrency); i++ {
go func() {
defer wg.Done()
sw.consumeRoutine(ctx, taskQueue, finishQueue, func(err error) {
cancel()
if err != context.Canceled {
errOccured = err
}
})
}()
}
// start task producer
wg.Add(1)
go func() {
defer wg.Done()
defer close(taskQueue)
sw.produceRoutine(ctx, taskQueue, finishQueue, func(err error) {
cancel()
if err != context.Canceled {
errOccured = err
}
})
}()
wg.Wait()
if errOccured != nil {
return errOccured
}
return ctx.Err()
}
func (sw *SlideWindow) produceRoutine(ctx context.Context,
taskQueue chan<- *Session, finishQueue chan *Session, onErr func(err error)) {
// initialize cells
cells := make([]Session, sw.Total)
for i := range cells {
cells[i].index = uint64(i)
}
finishMap := make(map[uint64]*Session, sw.Concurrency)
produceAt := -1
finishAt := -1
// push the first one
if err := sw.Init(ctx, &cells[0]); err != nil {
onErr(err)
return
}
taskQueue <- &cells[0]
produceAt++
for {
var task *Session
var ok bool
select {
case <-ctx.Done():
onErr(ctx.Err())
return
case task, ok = <-finishQueue:
if !ok {
return
}
}
finishMap[task.index] = task
// take availables
for {
if v, exist := finishMap[uint64(finishAt+1)]; exist {
if err := sw.Done(ctx, v); err != nil {
onErr(err)
return
}
delete(finishMap, uint64(finishAt+1))
finishAt++
} else {
break
}
}
// produce
if produceAt-finishAt < int(sw.Concurrency) {
for i := 0; i < int(sw.Concurrency)-(produceAt-finishAt); i++ {
next := produceAt + 1
if next == int(sw.Total) {
break
}
if err := sw.Init(ctx, &cells[next]); err != nil {
onErr(err)
return
}
taskQueue <- &cells[next]
produceAt = next
}
}
if finishAt == int(sw.Total-1) {
close(finishQueue)
}
}
}
func (sw *SlideWindow) consumeRoutine(ctx context.Context,
taskQueue <-chan *Session, finishQueue chan<- *Session, onErr func(err error)) {
for {
var task *Session
var ok bool
select {
case <-ctx.Done():
onErr(ctx.Err())
return
case task, ok = <-taskQueue:
if !ok {
return
}
}
// do task
if err := sw.Task(ctx, task); err != nil {
onErr(err)
return
}
// notify
finishQueue <- task
}
}