-
Notifications
You must be signed in to change notification settings - Fork 2
/
cluster.go
144 lines (125 loc) · 2.87 KB
/
cluster.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
package hec
import (
"io"
"math/rand"
"net/http"
"sync"
"github.com/google/uuid"
)
type Cluster struct {
HEC
// Inner clients
clients []*Client
mtx sync.Mutex
maxRetries int
}
func NewCluster(serverURLs []string, token string) HEC {
id := uuid.New()
channel := id.String()
clients := make([]*Client, len(serverURLs))
for i, serverURL := range serverURLs {
clients[i] = &Client{
httpClient: http.DefaultClient,
serverURL: serverURL,
token: token,
keepAlive: true,
channel: channel,
retries: 0, // try only once for each client
maxLength: defaultMaxContentLength,
}
}
return &Cluster{
clients: clients,
maxRetries: -1, // default: try all clients
}
}
func (c *Cluster) SetHTTPClient(httpClient *http.Client) {
c.mtx.Lock()
for _, client := range c.clients {
client.SetHTTPClient(httpClient)
}
c.mtx.Unlock()
}
func (c *Cluster) SetKeepAlive(enable bool) {
c.mtx.Lock()
for _, client := range c.clients {
client.SetKeepAlive(enable)
}
c.mtx.Unlock()
}
func (c *Cluster) SetChannel(channel string) {
c.mtx.Lock()
for _, client := range c.clients {
client.SetChannel(channel)
}
c.mtx.Unlock()
}
func (c *Cluster) SetMaxRetry(retries int) {
c.maxRetries = retries
}
func (c *Cluster) SetMaxContentLength(size int) {
c.mtx.Lock()
for _, client := range c.clients {
client.SetMaxContentLength(size)
}
c.mtx.Unlock()
}
func (c *Cluster) SetCompression(compression string) {
c.mtx.Lock()
for _, client := range c.clients {
client.SetCompression(compression)
}
c.mtx.Unlock()
}
func (c *Cluster) WriteEvent(event *Event) error {
return c.retry(func(client *Client) error {
return client.WriteEvent(event)
})
}
func (c *Cluster) WriteBatch(events []*Event) error {
return c.retry(func(client *Client) error {
return client.WriteBatch(events)
})
}
func (c *Cluster) WriteRaw(reader io.ReadSeeker, metadata *EventMetadata) error {
startAt, _ := reader.Seek(0, io.SeekCurrent)
return c.retry(func(client *Client) error {
reader.Seek(startAt, io.SeekStart)
return client.WriteRaw(reader, metadata)
})
}
func (c *Cluster) retry(writeFunc func(*Client) error) error {
exclude := make([]*Client, 0)
var err error
for t := 0; t < len(c.clients) && t != c.maxRetries; t++ {
client := pick(c.clients, exclude)
if err = writeFunc(client); err != nil {
if err == ErrEventTooLong {
return err
} else if res, ok := err.(*Response); !ok || retriable(res.Code) {
// If failed to write into this client, exclude it and try others
exclude = append(exclude, client)
continue
}
} else {
return nil
}
}
return err
}
func pick(clients []*Client, exclude []*Client) *Client {
var choice *Client
for choice == nil {
choice = clients[rand.Int()%len(clients)]
if exclude == nil {
break
}
for _, bad := range exclude {
if bad == choice {
choice = nil
break
}
}
}
return choice
}