-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch.go
95 lines (81 loc) · 1.83 KB
/
batch.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
package go_batcher
import (
"time"
"sync"
"fmt"
)
type Batch struct {
name string // Name of batcher
mutex sync.Mutex
input chan interface{}
maxCapacity int // Max count of channel
timeout time.Duration // Timeout of force calling callback func
callback cbFunc // Callback function
}
func NewBatch(name string, maxCapacity int, timeout time.Duration, callback cbFunc) *Batch {
batcher := &Batch{
name: name,
maxCapacity: maxCapacity,
timeout: timeout,
callback: callback,
}
return batcher
}
func (bc *Batch) String() string {
return fmt.Sprintf("Batch { name:%s, maxCapacity:%d, timeout:%s }", bc.name, bc.maxCapacity, bc.timeout)
}
// Push single data into batcher
func (bc *Batch) Push(data interface{}) {
bc.mutex.Lock()
defer bc.mutex.Unlock()
if bc.input == nil {
bc.input = make(chan interface{}, bc.maxCapacity)
go bc.run()
}
bc.input <- data
}
// Push batch of data into batcher
func (bc *Batch) Batch(batch []interface{}) {
bc.mutex.Lock()
defer bc.mutex.Unlock()
batchLen := len(batch)
// If the length of batch is larger than maxCapacity
if batchLen > bc.maxCapacity {
batch = batch[:bc.maxCapacity]
}
if bc.input == nil {
bc.input = make(chan interface{}, bc.maxCapacity)
go bc.run()
}
for _, item := range batch {
bc.input <- item
}
}
func (bc *Batch) run() {
var batch []interface{}
timer := time.NewTimer(bc.timeout)
for {
select {
case <-timer.C:
bc.callback(batch)
bc.close()
return
case item := <-bc.input:
batch = append(batch, item)
if len(batch) >= bc.maxCapacity {
// Callback with batch
bc.callback(batch)
// Init batch array
batch = []interface{}{}
}
}
}
}
func (bc *Batch) close() {
bc.mutex.Lock()
defer bc.mutex.Unlock()
if bc.input != nil {
close(bc.input)
bc.input = nil
}
}