-
Notifications
You must be signed in to change notification settings - Fork 2
/
discovery.go
158 lines (129 loc) · 3.86 KB
/
discovery.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
/*
* MIT License
*
* Copyright (c) 2024-2025 Arsene Tochemey Gandote
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package dnssd
import (
"context"
"net"
"sync"
goset "github.com/deckarep/golang-set/v2"
"go.uber.org/atomic"
"github.com/tochemey/gokv/discovery"
)
const (
DomainName = "domain-name"
IPv6 = "ipv6"
)
// Discovery represents the DNS service discovery
// IP addresses are looked up by querying the default
// DNS resolver for A and AAAA records associated with the DNS name.
type Discovery struct {
mu sync.Mutex
config *Config
// states whether the actor system has started or not
initialized *atomic.Bool
}
// enforce compilation error
var _ discovery.Provider = &Discovery{}
// NewDiscovery returns an instance of the DNS discovery provider
func NewDiscovery(config *Config) *Discovery {
return &Discovery{
mu: sync.Mutex{},
config: config,
initialized: atomic.NewBool(false),
}
}
// ID returns the discovery provider id
func (d *Discovery) ID() string {
return "dns-sd"
}
// Initialize initializes the plugin: registers some internal data structures, clients etc.
func (d *Discovery) Initialize() error {
d.mu.Lock()
defer d.mu.Unlock()
if d.initialized.Load() {
return discovery.ErrAlreadyInitialized
}
return d.config.Validate()
}
// Register registers this node to a service discovery directory.
func (d *Discovery) Register() error {
d.mu.Lock()
defer d.mu.Unlock()
if d.initialized.Load() {
return discovery.ErrAlreadyRegistered
}
d.initialized = atomic.NewBool(true)
return nil
}
// Deregister removes this node from a service discovery directory.
func (d *Discovery) Deregister() error {
d.mu.Lock()
defer d.mu.Unlock()
if !d.initialized.Load() {
return discovery.ErrNotInitialized
}
d.initialized = atomic.NewBool(false)
return nil
}
// DiscoverPeers returns a list of known nodes.
func (d *Discovery) DiscoverPeers() ([]string, error) {
d.mu.Lock()
defer d.mu.Unlock()
if !d.initialized.Load() {
return nil, discovery.ErrNotInitialized
}
ctx := context.Background()
// set ipv6 filter
v6 := false
if d.config.IPv6 != nil {
v6 = *d.config.IPv6
}
var err error
// only extract ipv6
if v6 {
ips, err := net.DefaultResolver.LookupIP(ctx, "ip6", d.config.DomainName)
if err != nil {
return nil, err
}
ipList := make([]string, len(ips))
for index, ip := range ips {
ipList[index] = ip.String()
}
return goset.NewSet[string](ipList...).ToSlice(), nil
}
// lookup the addresses based upon the dns name
addrs, err := net.DefaultResolver.LookupIPAddr(ctx, d.config.DomainName)
if err != nil {
return nil, err
}
ipList := make([]string, len(addrs))
for index, addr := range addrs {
ipList[index] = addr.IP.String()
}
return goset.NewSet[string](ipList...).ToSlice(), nil
}
// Close closes the provider
func (d *Discovery) Close() error {
return nil
}