forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
callbacks.go
159 lines (134 loc) · 3.51 KB
/
callbacks.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
package modetest
import (
"sync"
"time"
"github.com/elastic/beats/libbeat/outputs"
)
type errNetTimeout struct{}
func (e errNetTimeout) Error() string { return "errNetTimeout" }
func (e errNetTimeout) Timeout() bool { return true }
func (e errNetTimeout) Temporary() bool { return false }
func CloseOK() error {
return nil
}
func ConnectOK(timeout time.Duration) error {
return nil
}
func ConnectFail(err error) func(time.Duration) error {
return func(timeout time.Duration) error {
return err
}
}
func ConnectFailN(n int, err error) func(time.Duration) error {
cnt := makeCounter(n, err)
return func(timeout time.Duration) error {
return cnt()
}
}
func PublishIgnore([]outputs.Data) ([]outputs.Data, error) {
return nil, nil
}
func PublishCollect(
collected *[][]outputs.Data,
) func(data []outputs.Data) ([]outputs.Data, error) {
mutex := sync.Mutex{}
return func(data []outputs.Data) ([]outputs.Data, error) {
mutex.Lock()
defer mutex.Unlock()
*collected = append(*collected, data)
return nil, nil
}
}
func PublishFailStart(
n int,
pub func(data []outputs.Data) ([]outputs.Data, error),
) func(data []outputs.Data) ([]outputs.Data, error) {
return PublishFailWith(n, errNetTimeout{}, pub)
}
func PublishFailWith(
n int,
err error,
pub func([]outputs.Data) ([]outputs.Data, error),
) func([]outputs.Data) ([]outputs.Data, error) {
inc := makeCounter(n, err)
return func(data []outputs.Data) ([]outputs.Data, error) {
if err := inc(); err != nil {
return data, err
}
return pub(data)
}
}
func PublishCollectAfterFailStart(
n int,
collected *[][]outputs.Data,
) func(data []outputs.Data) ([]outputs.Data, error) {
return PublishFailStart(n, PublishCollect(collected))
}
func PublishCollectAfterFailStartWith(
n int,
err error,
collected *[][]outputs.Data,
) func(data []outputs.Data) ([]outputs.Data, error) {
return PublishFailWith(n, err, PublishCollect(collected))
}
func AsyncPublishIgnore(func([]outputs.Data, error), []outputs.Data) error {
return nil
}
func AsyncPublishCollect(
collected *[][]outputs.Data,
) func(func([]outputs.Data, error), []outputs.Data) error {
mutex := sync.Mutex{}
return func(cb func([]outputs.Data, error), data []outputs.Data) error {
mutex.Lock()
defer mutex.Unlock()
*collected = append(*collected, data)
cb(nil, nil)
return nil
}
}
func AsyncPublishFailStart(
n int,
pub func(func([]outputs.Data, error), []outputs.Data) error,
) func(func([]outputs.Data, error), []outputs.Data) error {
return AsyncPublishFailStartWith(n, errNetTimeout{}, pub)
}
func AsyncPublishFailStartWith(
n int,
err error,
pub func(func([]outputs.Data, error), []outputs.Data) error,
) func(func([]outputs.Data, error), []outputs.Data) error {
inc := makeCounter(n, err)
return func(cb func([]outputs.Data, error), data []outputs.Data) error {
if err := inc(); err != nil {
return err
}
return pub(cb, data)
}
}
func AsyncPublishCollectAfterFailStart(
n int,
collected *[][]outputs.Data,
) func(func([]outputs.Data, error), []outputs.Data) error {
return AsyncPublishFailStart(n, AsyncPublishCollect(collected))
}
func AsyncPublishCollectAfterFailStartWith(
n int,
err error,
collected *[][]outputs.Data,
) func(func([]outputs.Data, error), []outputs.Data) error {
return AsyncPublishFailStartWith(n, err, AsyncPublishCollect(collected))
}
func makeCounter(n int, err error) func() error {
mutex := sync.Mutex{}
count := 0
return func() error {
mutex.Lock()
defer mutex.Unlock()
if count < n {
count++
return err
}
count = 0
return nil
}
}