-
Notifications
You must be signed in to change notification settings - Fork 14
/
hub.go
58 lines (45 loc) · 983 Bytes
/
hub.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package discovery
import (
"context"
"crypto/x509"
"sync"
)
type HubConfig struct {
Addr string
Name string
Insecure bool
PinnedCert *x509.Certificate
}
type HubConnectDetails interface {
Address() string
InsecureTLS() bool
X509Cert() *x509.Certificate
}
type HubConfigProvider interface {
Take(context.Context) (HubConfig, bool)
Return(HubConfig)
}
type StaticHubConfigs struct {
mu sync.Mutex
configs []HubConfig
}
func HubConfigs(cfg ...HubConfig) HubConfigProvider {
return &StaticHubConfigs{configs: cfg}
}
func (h *StaticHubConfigs) Take(ctx context.Context) (HubConfig, bool) {
h.mu.Lock()
defer h.mu.Unlock()
if len(h.configs) == 0 {
return HubConfig{}, false
}
cfg := h.configs[0]
h.configs = h.configs[1:]
return cfg, true
}
func (h *StaticHubConfigs) Return(cfg HubConfig) {
h.mu.Lock()
defer h.mu.Unlock()
h.configs = append(h.configs, cfg)
}