This repository has been archived by the owner on Jun 23, 2020. It is now read-only.
/
cluster.go
203 lines (168 loc) · 4.56 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package catalog
import (
"crypto/sha1"
"fmt"
"github.com/hashicorp/consul/api"
"github.com/mitchellh/mapstructure"
"sort"
"strings"
)
const (
ConnectZoneName = "connect.consul"
ServiceZoneName = "service.consul"
)
// ClusterInfo represents a collection of service instances
// registered in consul catalog. A cluster represents
// only one type of service.
type ClusterInfo interface {
Name() string
Endpoints() []Endpoint
IsConnectEnabled() bool
Hash() string
Settings() (*ClusterSettings, error)
}
var _ ClusterInfo = &Cluster{}
type Cluster struct {
name string
isConnect bool
services []*api.CatalogService
}
type ClusterSettings struct {
ConnTimeout int64 `mapstructure:"flightpath-cluster-conn_timeout"`
PerConnBufLimitBytes uint32 `mapstructure:"flightpath-cluster-per_conn_buf_limit_bytes"`
MaxReqPerConn uint32 `mapstructure:"flightpath-cluster-max_req_per_conn"`
TcpKeepaliveProbes uint32 `mapstructure:"flightpath-cluster-tcp_keepalive_probes"`
TcpKeepaliveTime uint32 `mapstructure:"flightpath-cluster-tcp_keepalive_time"`
TcpKeepaliveInterval uint32 `mapstructure:"flightpath-cluster-tcp_keepalive_interval"`
RetryOn string `mapstructure:"flightpath-retry-on"`
RetryAttempts uint32 `mapstructure:"flightpath-retry-attempts"`
RetryAttemptTimeout int64 `mapstructure:"flightpath-retry-per_try_timeout"`
RetryBackoffBase int64 `mapstructure:"flightpath-retry-backoff_base_interval"`
RetryBackoffMax int64 `mapstructure:"flightpath-retry-backoff_max_interval"`
}
// TODO: right now we don't care about new or old cluster
// settings. If these settings change across deployments
// we'll just pick the latest one and work with that.
// An upcoming version will support creating multiple
// clusters per service e.g. canary, strand, etc
// which will stick with their own cluster settings.
func (c *Cluster) Settings() (*ClusterSettings, error) {
var (
settings = map[string]string{}
result = new(ClusterSettings)
latest uint64 = 0
)
for _, s := range c.services {
if latest < s.CreateIndex {
latest = s.CreateIndex
settings = s.ServiceMeta
}
}
err := mapstructure.WeakDecode(settings, result)
if err != nil {
return nil, err
}
result.Canonicalize()
return result, nil
}
func (cs *ClusterSettings) Canonicalize() {
if cs.ConnTimeout == 0 {
cs.ConnTimeout = 10
}
if cs.PerConnBufLimitBytes == 0 {
cs.PerConnBufLimitBytes = 32768
}
if cs.MaxReqPerConn == 0 {
cs.MaxReqPerConn = 10000
}
if cs.TcpKeepaliveProbes == 0 {
cs.TcpKeepaliveProbes = 9
}
if cs.TcpKeepaliveTime == 0 {
cs.TcpKeepaliveTime = 5 * 60
}
if cs.TcpKeepaliveInterval == 0 {
cs.TcpKeepaliveInterval = 90
}
if cs.RetryOn != "" {
if cs.RetryAttempts == 0 {
cs.RetryAttempts = 3
}
if cs.RetryAttemptTimeout == 0 {
cs.RetryAttemptTimeout = 5
}
if cs.RetryBackoffBase == 0 {
cs.RetryBackoffBase = 1
}
if cs.RetryBackoffMax == 0 {
cs.RetryBackoffMax = 6
}
}
}
func Hash(l []ClusterInfo) string {
var ids []string
for _, s := range l {
ids = append(ids, s.Hash())
}
sort.Strings(ids)
cid := strings.Join(ids, "")
return fmt.Sprintf("%x", sha1.Sum([]byte(cid)))[:10]
}
func (c *Cluster) Hash() string {
var ids []string
for _, s := range c.services {
ids = append(ids, s.ID)
}
sort.Strings(ids)
cid := strings.Join(ids, "")
return fmt.Sprintf("%x", sha1.Sum([]byte(cid)))[:10]
}
func (c *Cluster) Name() string {
return c.name
}
func (c *Cluster) Endpoints() []Endpoint {
var results []Endpoint
for _, service := range c.services {
routing := getRoutingInfo(service)
results = append(results, Endpoint{
name: service.ID,
serviceName: service.ServiceName,
isConnect: c.IsConnectEnabled(),
addr: service.Address,
port: service.ServicePort,
routing: routing,
})
}
return results
}
func getRoutingInfo(service *api.CatalogService) map[string][]string {
results := map[string][]string{}
for k, v := range service.ServiceMeta {
if !strings.HasPrefix(k, "flightpath-route") {
continue
}
domain := "*"
uriMatch := "/"
if strings.HasPrefix(v, "/") {
// value is a path match
uriMatch = v
} else {
// value has a domain and potentially a path
idx := strings.Index(v, "/")
if idx == -1 {
domain = v
} else {
domain = v[:idx]
uriMatch = v[idx:]
}
}
if _, ok := results[domain]; !ok {
results[domain] = []string{}
}
results[domain] = append(results[domain], uriMatch)
}
return results
}
func (c *Cluster) IsConnectEnabled() bool {
return c.isConnect
}