forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
async.go
79 lines (64 loc) · 1.99 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
package publisher
import (
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/outputs"
)
type asyncPublisher struct {
outputs []worker
pub *PublisherType
}
const (
defaultBulkSize = 2048
)
func newAsyncPublisher(pub *PublisherType, hwm, bulkHWM int, ws *common.WorkerSignal) *asyncPublisher {
p := &asyncPublisher{pub: pub}
var outputs []worker
for _, out := range pub.Output {
outputs = append(outputs, asyncOutputer(ws, hwm, bulkHWM, out))
}
p.outputs = outputs
return p
}
func (p *asyncPublisher) client() eventPublisher {
return p
}
func (p *asyncPublisher) PublishEvent(ctx Context, event common.MapStr) bool {
p.send(message{context: ctx, event: event})
return true
}
func (p *asyncPublisher) PublishEvents(ctx Context, events []common.MapStr) bool {
p.send(message{context: ctx, events: events})
return true
}
func (p *asyncPublisher) send(m message) {
if p.pub.disabled {
debug("publisher disabled")
outputs.SignalCompleted(m.context.Signal)
return
}
// m.signal is not set yet. But a async client type supporting signals might
// be implemented in the future.
// If m.Signal is nil, NewSplitSignaler will return nil -> signaler will
// only set if client did send one
if m.context.Signal != nil && len(p.outputs) > 1 {
m.context.Signal = outputs.NewSplitSignaler(m.context.Signal, len(p.outputs))
}
for _, o := range p.outputs {
o.send(m)
}
}
func asyncOutputer(ws *common.WorkerSignal, hwm, bulkHWM int, worker *outputWorker) worker {
config := worker.config
flushInterval := config.FlushInterval
maxBulkSize := config.BulkMaxSize
logp.Info("Flush Interval set to: %v", flushInterval)
logp.Info("Max Bulk Size set to: %v", maxBulkSize)
// batching disabled
if flushInterval <= 0 || maxBulkSize <= 0 {
return worker
}
debug("create bulk processing worker (interval=%v, bulk size=%v)",
flushInterval, maxBulkSize)
return newBulkWorker(ws, hwm, bulkHWM, worker, flushInterval, maxBulkSize)
}