-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
prosumer.go
163 lines (146 loc) · 3.69 KB
/
prosumer.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
package prosumer
import (
"context"
"log"
"sync"
"time"
)
type worker struct {
*queue
close chan struct{}
waitClose *sync.WaitGroup
consumer Consumer
batchSize int
batchInterval time.Duration
cb Callback
}
func (w worker) start() {
received := make([]Element, 0, w.batchSize)
watchdog := time.NewTimer(w.batchInterval)
doWork := func() {
watchdog.Stop()
// don't need to drain the channel here
// since the channel already drained in `for loop` below
// if !watchdog.Stop() {
// // try to drain the channel
// select {
// // we can't get here
// case <-watchdog.C:
// if enable_race {
// log.Println("drain the channel")
// }
// // for those reach batchInterval
// default:
// if enable_race {
// log.Println("channel already drained")
// }
// }
// }
if len(received) > 0 {
err := w.consumer(received)
w.cb(received, err)
received = received[:0]
}
watchdog.Reset(w.batchInterval)
}
loop:
for {
if e, ok := w.queue.dequeue(); ok {
received = append(received, e)
if len(received) >= w.batchSize {
if enable_race {
log.Println("doWork for reach batchSize")
}
doWork()
} else {
select {
case <-watchdog.C:
if enable_race {
log.Println("doWork for reach batchInterval while dequeue")
}
doWork()
default:
}
}
} else {
select {
case <-w.close:
doWork()
watchdog.Stop()
w.waitClose.Done()
break loop
case <-watchdog.C:
if enable_race {
log.Println("doWork for reach batchInterval")
}
doWork()
}
}
}
}
// Coordinator implements a producer-consumer workflow.
// Put() add new elements into inner buffer queue, and will be processed by Consumer.
type Coordinator struct {
// inner buffer
*queue
*worker
numConsumer int
close chan struct{} // notify worker to close
waitClose *sync.WaitGroup
}
func NewCoordinator(config Config) Coordinator {
var waitClose sync.WaitGroup
waitClose.Add(config.numConsumer + 1) // +1 for queue
closeCh := make(chan struct{})
q := newQueue(config.bufferSize, config.rp, &waitClose)
c := Coordinator{
queue: q,
worker: &worker{
queue: q,
close: closeCh,
waitClose: &waitClose,
consumer: config.consumer,
batchSize: config.batchSize,
batchInterval: config.batchInterval,
cb: config.cb,
},
numConsumer: config.numConsumer,
close: closeCh,
waitClose: &waitClose,
}
return c
}
// Start workers to consume elements from queue.
func (c Coordinator) Start() {
for i := 0; i < c.numConsumer; i++ {
go c.worker.start()
}
}
// Close closes the Coordinator, no more element can be put any more.
// It can be graceful, which means:
// 1. blocking
// 2. all remaining elements in buffer queue will make sure to be consumed.
func (c Coordinator) Close(graceful bool) error {
c.queue.close(graceful)
close(c.close) // read from closed chan always return non-blocking
if graceful {
c.waitClose.Wait()
}
return nil
}
// Put new element into inner buffer queue. It return error when inner buffer queue is full, and elements failed putting to queue is the first return value.
// Due to different rejectPolicy, multiple elements may be discarded before current element put successfully.
// Common usages pattern:
//
// discarded, err := c.Put(e)
// if err != nil {
// fmt.Errorf("discarded elements %+v for err %v", discarded, err)
// }
//
func (c Coordinator) Put(ctx context.Context, e Element) ([]Element, error) {
return c.queue.enqueue(ctx, e)
}
// RemainingCapacity return how many elements inner buffer queue can hold.
func (c Coordinator) RemainingCapacity() int {
return c.queue.cap() - c.queue.size()
}