-
Notifications
You must be signed in to change notification settings - Fork 125
/
glue.go
55 lines (45 loc) · 791 Bytes
/
glue.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
package points
import (
"bytes"
"fmt"
"time"
)
func Glue(exit chan bool, in chan *Points, chunkSize int, chunkTimeout time.Duration, callback func([]byte)) {
var p *Points
var ok bool
buf := bytes.NewBuffer(nil)
flush := func() {
if buf.Len() == 0 {
return
}
callback(buf.Bytes())
buf = bytes.NewBuffer(nil)
}
ticker := time.NewTicker(chunkTimeout)
defer ticker.Stop()
for {
p = nil
select {
case p, ok = <-in:
if !ok { // in chan closed
flush()
return
}
// pass
case <-ticker.C:
flush()
case <-exit:
return
}
if p == nil {
continue
}
for _, d := range p.Data {
s := fmt.Sprintf("%s %v %v\n", p.Metric, d.Value, d.Timestamp)
if buf.Len()+len(s) > chunkSize {
flush()
}
buf.Write([]byte(s))
}
}
}