-
Notifications
You must be signed in to change notification settings - Fork 10
/
watcher.go
63 lines (59 loc) · 1.13 KB
/
watcher.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
package nasync
import (
"time"
)
// watcher watches the async.queue channel, and writes the logs to output
func (a *Async) watcher() {
var buf buffer
for {
if a.waiting {
time.Sleep(time.Second)
if len(a.taskChan) == 0 {
a.done <- true
return
}
}
timeout := time.After(time.Second / 10)
for i := 0; i < a.bufSize; i++ {
select {
case req := <-a.taskChan:
a.flushReq(&buf, req)
case <-timeout:
i = a.bufSize
case <-a.quit:
// If quit signal received, cleans the channel
for {
select {
case req := <-a.taskChan:
a.flushReq(&buf, req)
default:
a.flushBuf(&buf)
a.quit <- true
return
}
}
}
}
a.flushBuf(&buf)
}
}
// flushReq handles the request and writes the result to writer
func (a *Async) flushReq(b *buffer, t *task) {
//do print for this
b.Append(t)
}
// flushBuf flushes the content of buffer to out and reset the buffer
func (a *Async) flushBuf(b *buffer) {
tasks := b.Tasks()
if len(tasks) > 0 {
for _, t := range tasks {
a.wait.Add(1)
go func(t *task) {
t.Do()
a.wait.Done()
}(t)
}
a.wait.Wait()
b.Reset()
}
}