forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
failover_client.go
151 lines (127 loc) · 3.46 KB
/
failover_client.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
package modeutil
import (
"errors"
"math/rand"
"time"
"github.com/elastic/beats/libbeat/outputs"
"github.com/elastic/beats/libbeat/outputs/mode"
)
type failOverClient struct {
conns []mode.ProtocolClient
active int
}
type asyncFailOverClient struct {
conns []mode.AsyncProtocolClient
active int
}
type clientList interface {
Active() int
Len() int
Get(i int) mode.Connectable
Activate(i int)
}
var (
// ErrNoConnectionConfigured indicates no configured connections for publishing.
ErrNoConnectionConfigured = errors.New("No connection configured")
errNoActiveConnection = errors.New("No active connection")
)
func NewFailoverClient(clients []mode.ProtocolClient) []mode.ProtocolClient {
if len(clients) <= 1 {
return clients
}
return []mode.ProtocolClient{&failOverClient{conns: clients, active: -1}}
}
func (f *failOverClient) Active() int { return f.active }
func (f *failOverClient) Len() int { return len(f.conns) }
func (f *failOverClient) Get(i int) mode.Connectable { return f.conns[i] }
func (f *failOverClient) Activate(i int) { f.active = i }
func (f *failOverClient) Connect(to time.Duration) error {
return connect(f, to)
}
func (f *failOverClient) Close() error {
return closeActive(f)
}
func (f *failOverClient) PublishEvents(data []outputs.Data) ([]outputs.Data, error) {
if f.active < 0 {
return data, errNoActiveConnection
}
return f.conns[f.active].PublishEvents(data)
}
func (f *failOverClient) PublishEvent(data outputs.Data) error {
if f.active < 0 {
return errNoActiveConnection
}
return f.conns[f.active].PublishEvent(data)
}
func NewAsyncFailoverClient(clients []mode.AsyncProtocolClient) []mode.AsyncProtocolClient {
if len(clients) <= 1 {
return clients
}
return []mode.AsyncProtocolClient{
&asyncFailOverClient{conns: clients, active: -1},
}
}
func (f *asyncFailOverClient) Active() int { return f.active }
func (f *asyncFailOverClient) Len() int { return len(f.conns) }
func (f *asyncFailOverClient) Get(i int) mode.Connectable { return f.conns[i] }
func (f *asyncFailOverClient) Activate(i int) { f.active = i }
func (f *asyncFailOverClient) Connect(to time.Duration) error {
return connect(f, to)
}
func (f *asyncFailOverClient) Close() error {
return closeActive(f)
}
func (f *asyncFailOverClient) AsyncPublishEvents(
cb func([]outputs.Data, error),
data []outputs.Data,
) error {
if f.active < 0 {
return errNoActiveConnection
}
return f.conns[f.active].AsyncPublishEvents(cb, data)
}
func (f *asyncFailOverClient) AsyncPublishEvent(
cb func(error),
data outputs.Data,
) error {
if f.active < 0 {
return errNoActiveConnection
}
return f.conns[f.active].AsyncPublishEvent(cb, data)
}
func connect(lst clientList, to time.Duration) error {
active := lst.Active()
l := lst.Len()
next := 0
switch {
case l == 0:
return ErrNoConnectionConfigured
case l == 1:
next = 0
case l == 2 && 0 <= active && active <= 1:
next = 1 - active
default:
for {
// Connect to random server to potentially spread the
// load when large number of beats with same set of sinks
// are started up at about the same time.
next = rand.Int() % l
if next != active {
break
}
}
}
conn := lst.Get(next)
lst.Activate(next)
return conn.Connect(to)
}
func closeActive(lst clientList) error {
active := lst.Active()
if active < 0 {
return nil
}
conn := lst.Get(active)
err := conn.Close()
lst.Activate(-1)
return err
}