forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 9
/
single.go
177 lines (147 loc) · 4.16 KB
/
single.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
package single
import (
"errors"
"time"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/op"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/outputs"
"github.com/elastic/beats/libbeat/outputs/mode"
)
// Mode sends all Output on one single connection. If connection is
// not available, the output plugin blocks until the connection is either available
// again or the connection mode is closed by Close.
type Mode struct {
conn mode.ProtocolClient
isConnected bool
closed bool // mode closed flag to break publisher loop
timeout time.Duration // connection timeout
backoff *common.Backoff
// maximum number of configured send attempts. If set to 0, publisher will
// block until event has been successfully published.
maxAttempts int
}
var (
errNeedBackoff = errors.New("need to backoff")
debugf = logp.MakeDebug("output")
)
// New creates a new single connection mode using exactly one
// ProtocolClient connection.
func New(
client mode.ProtocolClient,
maxAttempts int,
waitRetry, timeout, maxWaitRetry time.Duration,
) (*Mode, error) {
s := &Mode{
conn: client,
timeout: timeout,
backoff: common.NewBackoff(nil, waitRetry, maxWaitRetry),
maxAttempts: maxAttempts,
}
return s, nil
}
func (s *Mode) connect() error {
if s.isConnected {
return nil
}
err := s.conn.Connect(s.timeout)
s.isConnected = err == nil
return err
}
// Close closes the underlying connection.
func (s *Mode) Close() error {
s.closed = true
return s.closeClient()
}
func (s *Mode) closeClient() error {
err := s.conn.Close()
s.isConnected = false
return err
}
// PublishEvents tries to publish the events with retries if connection becomes
// unavailable. On failure PublishEvents tries to reconnect.
func (s *Mode) PublishEvents(
signaler op.Signaler,
opts outputs.Options,
data []outputs.Data,
) error {
return s.publish(signaler, opts, func() (bool, bool) {
for len(data) > 0 {
var err error
total := len(data)
data, err = s.conn.PublishEvents(data)
if err != nil {
logp.Info("Error publishing events (retrying): %s", err)
madeProgress := len(data) < total
return false, madeProgress
}
}
return true, false
})
}
// PublishEvent forwards a single event. On failure PublishEvent tries to reconnect.
func (s *Mode) PublishEvent(
signaler op.Signaler,
opts outputs.Options,
data outputs.Data,
) error {
return s.publish(signaler, opts, func() (bool, bool) {
if err := s.conn.PublishEvent(data); err != nil {
logp.Info("Error publishing event (retrying): %s", err)
return false, false
}
return true, false
})
}
// publish is used to publish events using the configured protocol client.
// It provides general error handling and back off support used on failed
// send attempts. To be used by PublishEvent and PublishEvents.
// The send callback will try to progress sending traffic and returns kind of
// progress made in ok or resetFail. If ok is set to true, send finished
// processing events. If ok is false but resetFail is set, send was partially
// successful. If send was partially successful, the fail counter is reset thus up
// to maxAttempts send attempts without any progress might be executed.
func (s *Mode) publish(
signaler op.Signaler,
opts outputs.Options,
send func() (ok bool, resetFail bool),
) error {
fails := 0
var err error
guaranteed := opts.Guaranteed || s.maxAttempts == 0
for !s.closed && (guaranteed || fails < s.maxAttempts) {
ok := false
resetFail := false
if err := s.connect(); err != nil {
logp.Err("Connecting error publishing events (retrying): %s", err)
goto sendFail
}
ok, resetFail = send()
if !ok {
s.closeClient()
goto sendFail
}
debugf("send completed")
s.backoff.Reset()
op.SigCompleted(signaler)
return nil
sendFail:
debugf("send fail")
fails++
if resetFail {
debugf("reset fails")
s.backoff.Reset()
fails = 0
}
s.backoff.Wait()
if !guaranteed && (s.maxAttempts > 0 && fails == s.maxAttempts) {
// max number of attempts reached
debugf("max number of attempts reached")
break
}
}
debugf("messages dropped")
mode.Dropped(1)
op.SigFailed(signaler, err)
return nil
}