forked from redBorder/rbforwarder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.go
109 lines (93 loc) · 2.64 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// 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 (
"bufio"
"bytes"
"compress/zlib"
"io"
"sync"
"sync/atomic"
"time"
"github.com/benbjohnson/clock"
"github.com/davidredborder/rbforwarder/utils"
)
// Batch groups multiple messages
type Batch struct {
Group string
Deflate bool
Message *utils.Message
Buf *bytes.Buffer
Writer io.Writer
MessageCount uint64 // Current number of messages in the buffer
Done utils.Done // Call to pass the message to the next handler
Timer *clock.Timer
Sent bool
}
// NewBatch creates a new instance of Batch
func NewBatch(m *utils.Message, group string, deflate bool, done utils.Done,
clk clock.Clock, timeoutMillis uint, ready chan *Batch) *Batch {
var wg sync.WaitGroup
payload, _ := m.PopPayload()
b := &Batch{
Group: group,
Deflate: deflate,
Done: done,
Message: m,
MessageCount: 1,
Buf: new(bytes.Buffer),
Sent: false,
}
if b.Deflate {
b.Writer = zlib.NewWriter(b.Buf)
} else {
b.Writer = bufio.NewWriter(b.Buf)
}
b.Writer.Write(payload)
if timeoutMillis > 0 {
b.Timer = clk.Timer(time.Duration(timeoutMillis) * time.Millisecond)
wg.Add(1)
go func() {
wg.Done()
<-b.Timer.C
if atomic.LoadUint64(&b.MessageCount) > 0 {
ready <- b
}
}()
}
wg.Wait()
return b
}
// Send the batch of messages to the next handler in the pipeline
func (b *Batch) Send(cb func()) {
if b.Deflate {
b.Writer.(*zlib.Writer).Flush()
} else {
b.Writer.(*bufio.Writer).Flush()
}
b.Message.PushPayload(b.Buf.Bytes())
cb()
}
// Add merges a new message in the buffer
func (b *Batch) Add(m *utils.Message) {
newReport := m.Reports.Pop()
b.Message.Reports.Push(newReport)
newPayload, _ := m.PopPayload()
b.Writer.Write(newPayload)
atomic.AddUint64(&b.MessageCount, 1)
}