forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
async.go
193 lines (164 loc) · 3.58 KB
/
async.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package logstash
import (
"sync/atomic"
"time"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/outputs/transport"
"github.com/urso/go-lumber/client/v2"
)
type asyncClient struct {
*transport.Client
client *v2.AsyncClient
win window
connect func() error
}
type msgRef struct {
count int32
batch []common.MapStr
err error
cb func([]common.MapStr, error)
win *window
batchSize int
}
func newAsyncLumberjackClient(
conn *transport.Client,
queueSize int,
compressLevel int,
maxWindowSize int,
timeout time.Duration,
beat string,
) (*asyncClient, error) {
c := &asyncClient{}
c.Client = conn
c.win.init(defaultStartMaxWindowSize, maxWindowSize)
enc, err := makeLogstashEventEncoder(beat)
if err != nil {
return nil, err
}
c.connect = func() error {
err := c.Client.Connect()
if err == nil {
c.client, err = v2.NewAsyncClientWithConn(c.Client,
queueSize,
v2.JSONEncoder(enc),
v2.Timeout(timeout),
v2.CompressionLevel(compressLevel))
}
return err
}
return c, nil
}
func (c *asyncClient) Connect(timeout time.Duration) error {
logp.Debug("logstash", "connect")
return c.connect()
}
func (c *asyncClient) Close() error {
logp.Debug("logstash", "close connection")
if c.client != nil {
err := c.client.Close()
c.client = nil
return err
}
return c.Client.Close()
}
func (c *asyncClient) AsyncPublishEvent(
cb func(error),
event common.MapStr,
) error {
data := []interface{}{event}
return c.client.Send(func(seq uint32, err error) { cb(err) }, data)
}
func (c *asyncClient) AsyncPublishEvents(
cb func([]common.MapStr, error),
events []common.MapStr,
) error {
publishEventsCallCount.Add(1)
if len(events) == 0 {
debug("send nil")
cb(nil, nil)
return nil
}
ref := &msgRef{
count: 1,
batch: events,
batchSize: len(events),
win: &c.win,
cb: cb,
err: nil,
}
for len(events) > 0 {
n, err := c.publishWindowed(ref, events)
debug("%v events out of %v events sent to logstash. Continue sending",
n, len(events))
events = events[n:]
if err != nil {
c.win.shrinkWindow()
_ = c.Close()
logp.Err("Failed to publish events caused by: %v", err)
eventsNotAcked.Add(int64(len(events)))
return err
}
}
ref.dec()
return nil
}
func (c *asyncClient) publishWindowed(
ref *msgRef,
events []common.MapStr,
) (int, error) {
batchSize := len(events)
windowSize := c.win.get()
debug("Try to publish %v events to logstash with window size %v",
batchSize, windowSize)
// prepare message payload
if batchSize > windowSize {
events = events[:windowSize]
}
err := c.sendEvents(ref, events)
if err != nil {
return 0, err
}
return len(events), nil
}
func (c *asyncClient) sendEvents(ref *msgRef, events []common.MapStr) error {
window := make([]interface{}, len(events))
for i, event := range events {
window[i] = event
}
atomic.AddInt32(&ref.count, 1)
return c.client.Send(ref.callback, window)
}
func (r *msgRef) callback(seq uint32, err error) {
if err != nil {
r.fail(seq, err)
} else {
r.done(seq)
}
}
func (r *msgRef) done(n uint32) {
ackedEvents.Add(int64(n))
r.batch = r.batch[n:]
r.dec()
}
func (r *msgRef) fail(n uint32, err error) {
ackedEvents.Add(int64(n))
r.err = err
r.batch = r.batch[n:]
r.dec()
}
func (r *msgRef) dec() {
i := atomic.AddInt32(&r.count, -1)
if i > 0 {
return
}
err := r.err
if err != nil {
eventsNotAcked.Add(int64(len(r.batch)))
r.win.shrinkWindow()
r.cb(r.batch, err)
} else {
r.win.tryGrowWindow(r.batchSize)
r.cb(nil, nil)
}
}