/
batcher.go
125 lines (108 loc) · 3.17 KB
/
batcher.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
// Copyright (C) ENEO Tecnologia SL - 2016
//
// Authors: Diego Fernández Barrera <dfernandez@redborder.com> <bigomby@gmail.com>
// Eugenio Pérez Martín <eugenio@redborder.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/lgpl-3.0.txt>.
package batcher
import (
"sync"
"github.com/benbjohnson/clock"
"github.com/redBorder/rbforwarder/utils"
"github.com/streamrail/concurrent-map"
)
// Batcher allows to merge multiple messages in a single one
type Batcher struct {
id int // Worker ID
batches cmap.ConcurrentMap
readyBatches chan *Batch
clk clock.Clock
finished chan struct{}
incoming chan struct {
m *utils.Message
done utils.Done
}
Config
}
// Workers returns the number of workers
func (batcher *Batcher) Workers() int {
return batcher.Config.Workers
}
// Spawn starts a gorutine that can receive:
// - New messages that will be added to a existing or new batch of messages
// - A batch of messages that is ready to send (i.e. batch timeout has expired)
func (batcher *Batcher) Spawn(id int) utils.Composer {
var wg sync.WaitGroup
b := *batcher
b.id = id
b.batches = cmap.New()
b.readyBatches = make(chan *Batch)
b.finished = make(chan struct{})
b.incoming = make(chan struct {
m *utils.Message
done utils.Done
})
if b.clk == nil {
b.clk = clock.New()
}
wg.Add(1)
go func() {
wg.Done()
for {
select {
case message := <-b.incoming:
if !message.m.Opts.Has("batch_group") {
message.done(message.m, 0, "")
} else {
tmp, _ := message.m.Opts.Get("batch_group")
group := tmp.(string)
if tmp, exists := b.batches.Get(group); exists {
batch := tmp.(*Batch)
batch.Add(message.m)
if batch.MessageCount >= b.Config.Limit && !batch.Sent {
batch.Send(func() {
b.batches.Remove(group)
batch.Done(batch.Message, 0, "limit")
batch.Sent = true
})
}
} else {
b.batches.Set(group, NewBatch(message.m, group, b.Config.Deflate,
message.done, b.clk, b.Config.TimeoutMillis, b.readyBatches))
}
}
b.finished <- struct{}{}
case batch := <-b.readyBatches:
if !batch.Sent {
batch.Send(func() {
b.batches.Remove(batch.Group)
batch.Done(batch.Message, 0, "timeout")
batch.Sent = true
})
}
}
}
}()
wg.Wait()
return &b
}
// OnMessage is called when a new message is receive. Add the new message to
// a batch
func (batcher *Batcher) OnMessage(m *utils.Message, done utils.Done) {
batcher.incoming <- struct {
m *utils.Message
done utils.Done
}{m, done}
<-batcher.finished
}