-
Notifications
You must be signed in to change notification settings - Fork 726
/
eventchannel.go
144 lines (118 loc) · 2.59 KB
/
eventchannel.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
package eventchannel
import (
"bytes"
"compress/gzip"
"sync"
"time"
"github.com/benbjohnson/clock"
"github.com/golang/glog"
)
type Metrics struct {
bufferSize int64
eventCount int64
}
type Limit struct {
maxByteSize int64
maxEventCount int64
maxTime time.Duration
}
type EventChannel struct {
gz *gzip.Writer
buff *bytes.Buffer
ch chan []byte
endCh chan int
metrics Metrics
muxGzBuffer sync.RWMutex
send Sender
limit Limit
clock clock.Clock
}
func NewEventChannel(sender Sender, clock clock.Clock, maxByteSize, maxEventCount int64, maxTime time.Duration) *EventChannel {
b := &bytes.Buffer{}
gzw := gzip.NewWriter(b)
c := EventChannel{
gz: gzw,
buff: b,
ch: make(chan []byte),
endCh: make(chan int),
metrics: Metrics{},
send: sender,
limit: Limit{maxByteSize, maxEventCount, maxTime},
clock: clock,
}
go c.start()
return &c
}
func (c *EventChannel) Push(event []byte) {
c.ch <- event
}
func (c *EventChannel) Close() {
c.endCh <- 1
}
func (c *EventChannel) buffer(event []byte) {
c.muxGzBuffer.Lock()
defer c.muxGzBuffer.Unlock()
_, err := c.gz.Write(event)
if err != nil {
glog.Warning("[pubstack] fail to compress, skip the event")
return
}
c.metrics.eventCount++
c.metrics.bufferSize += int64(len(event))
}
func (c *EventChannel) isBufferFull() bool {
c.muxGzBuffer.RLock()
defer c.muxGzBuffer.RUnlock()
return c.metrics.eventCount >= c.limit.maxEventCount || c.metrics.bufferSize >= c.limit.maxByteSize
}
func (c *EventChannel) reset() {
// reset buffer
c.gz.Reset(c.buff)
c.buff.Reset()
// reset metrics
c.metrics.eventCount = 0
c.metrics.bufferSize = 0
}
func (c *EventChannel) flush() {
c.muxGzBuffer.Lock()
defer c.muxGzBuffer.Unlock()
if c.metrics.eventCount == 0 || c.metrics.bufferSize == 0 {
return
}
// reset buffers and writers
defer c.reset()
// finish writing gzip header
err := c.gz.Close()
if err != nil {
glog.Warning("[pubstack] fail to close gzipped buffer")
return
}
// copy the current buffer to send the payload in a new thread
payload := make([]byte, c.buff.Len())
_, err = c.buff.Read(payload)
if err != nil {
glog.Warning("[pubstack] fail to copy the buffer")
return
}
// send events (async)
go c.send(payload)
}
func (c *EventChannel) start() {
ticker := c.clock.Ticker(c.limit.maxTime)
for {
select {
case <-c.endCh:
c.flush()
return
// event is received
case event := <-c.ch:
c.buffer(event)
if c.isBufferFull() {
c.flush()
}
// time between 2 flushes has passed
case <-ticker.C:
c.flush()
}
}
}