-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
178 lines (144 loc) · 4.1 KB
/
config.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
package gossipcache
import (
"context"
"crypto/tls"
"fmt"
"net"
"net/netip"
"net/url"
"darvaza.org/core"
"darvaza.org/slog"
"darvaza.org/slog/handlers/discard"
"github.com/hashicorp/memberlist"
"darvaza.org/gossipcache/transport"
)
const (
// DefaultCacheBasePath is the default Path under the CacheBaseURL for the groupcache
DefaultCacheBasePath = "/_groupcache/"
// DefaultCacheReplicas indicated the default number of replicas for the groupcache
DefaultCacheReplicas = 50
)
// Config represents the configuration used to setup the GossipCache node
type Config struct {
// Context
Context context.Context
// Logger is an optional logger to bind to memberlist and groupcache
Logger slog.Logger
// Transport is the Cluster's transport configuration
Transport *transport.Config
// Memberlist is the Cluster's configuration
Memberlist *memberlist.Config
// CacheReplicas is the number of replicas in the groupcache.
// If zero or negative it will be set to DefaultCacheReplicas
CacheReplicas int
// CacheBaseURL is the base of the advertised URL for the groupcache.
// Path and Query string components will be ignored
CacheBaseURL string
// CacheBasePath is the path under the CacheBaseURL where the groupcache
// handler is mounted
CacheBasePath string
// ClientTLSConfig is the tls.Config to be used when connecting to other
// nodes of the cluster when https scheme is used
ClientTLSConfig *tls.Config
}
// revive:disable:cyclomatic
// revive:disable:cognitive-complexity
// SetDefaults populates Config with defaults, except for CacheBaseURL
// which we will attempt to populate after memberlist is prepared
func (conf *Config) SetDefaults() error {
// revive:enable:cyclomatic
// revive:enable:cognitive-complexity
// Context
if conf.Context == nil {
conf.Context = context.Background()
}
// Logger
if conf.Logger == nil {
conf.Logger = discard.New()
}
// CacheReplicas
if conf.CacheReplicas <= 0 {
conf.CacheReplicas = DefaultCacheReplicas
}
// CacheBaseURL
if conf.CacheBaseURL != "" {
s, err := PrepareCacheBaseURL(conf.CacheBaseURL)
if err != nil {
// failed to sanitise
return core.Wrapf(err, "%s: %s: %s", "Config", "CacheBaseURL", "invalid")
}
conf.CacheBaseURL = s
}
// CacheBasePath
if s := conf.CacheBasePath; s == "" {
conf.CacheBasePath = DefaultCacheBasePath
} else if s[0] != '/' {
return fmt.Errorf("%s: %s: %s", "Config", "CacheBasePath", "invalid")
}
return nil
}
// revive:disable:cyclomatic
// revive:disable:cognitive-complexity
// PrepareCacheBaseURL sanitises a CacheBaseURL value
func PrepareCacheBaseURL(s string) (string, error) {
// revive:enable:cyclomatic
// revive:enable:cognitive-complexity
var u *url.URL
var err error
if s == "" {
// let's go with defaults
u = &url.URL{}
} else if u, err = url.Parse(s); err != nil {
// parse error
return "", err
}
if u.Scheme == "" {
u.Scheme = "https"
} else if u.Scheme != "http" && u.Scheme != "https" {
return "", fmt.Errorf("invalid scheme (%q)", u.Scheme)
}
if u.Host == "" {
// pick the first non-localhost address
ifaces, err := core.GetInterfacesNames("lo")
if err != nil {
return "", err
}
addrs, err := core.GetStringIPAddresses(ifaces...)
if err != nil {
return "", err
}
if len(addrs) > 0 {
u.Host = addrs[0]
} else {
u.Host = "0"
}
} else if host, port, err := net.SplitHostPort(u.Host); err != nil {
return "", core.Wrapf(err, "invalid Host (%q)", u.Host)
} else if port == "80" && u.Scheme == "http" {
u.Host = host
} else if port == "443" && u.Scheme == "https" {
u.Host = host
}
u.Path = ""
u.RawQuery = ""
u.Fragment = ""
return u.String(), nil
}
// InferCacheBaseURL produces a CacheBaseURL pointing to https on 443/tcp of
// Transport's AdveriseAddr
func InferCacheBaseURL(cfg *memberlist.Config) (string, error) {
var s string
ip, _, err := cfg.Transport.FinalAdvertiseAddr(cfg.AdvertiseAddr, 0)
if err != nil {
return "", err
}
addr, _ := netip.AddrFromSlice(ip)
addr.Unmap()
addr.WithZone("")
if addr.Is6() {
s = fmt.Sprintf("[%s]", addr.String())
} else {
s = addr.String()
}
return "https://" + s, nil
}