/
mdns_discovery.go
executable file
·188 lines (157 loc) · 4.04 KB
/
mdns_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
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
package client
import (
"context"
"encoding/json"
"net/url"
"sync"
"time"
"github.com/grandcat/zeroconf"
"github.com/paullee-me/rpcs/log"
)
type serviceMeta struct {
Service string `json:"service,omitempty"`
Meta string `json:"meta,omitempty"`
ServiceAddress string `json:"service_address,omitempty"`
}
// MDNSDiscovery is a mdns service discovery.
// It always returns the registered servers in etcd.
type MDNSDiscovery struct {
Timeout time.Duration
WatchInterval time.Duration
domain string
service string
pairs []*KVPair
chans []chan []*KVPair
mu sync.Mutex
filter ServiceDiscoveryFilter
stopCh chan struct{}
}
// NewMDNSDiscovery returns a new MDNSDiscovery.
// If domain is empty, use "local." in default.
func NewMDNSDiscovery(service string, timeout time.Duration, watchInterval time.Duration, domain string) ServiceDiscovery {
if domain == "" {
domain = "local."
}
d := &MDNSDiscovery{service: service, Timeout: timeout, WatchInterval: watchInterval, domain: domain}
d.stopCh = make(chan struct{})
var err error
d.pairs, err = d.browse()
if err != nil {
log.Warnf("failed to browse services: %v", err)
}
go d.watch()
return d
}
// NewMDNSDiscoveryTemplate returns a new MDNSDiscovery template.
func NewMDNSDiscoveryTemplate(timeout time.Duration, watchInterval time.Duration, domain string) ServiceDiscovery {
if domain == "" {
domain = "local."
}
return &MDNSDiscovery{Timeout: timeout, WatchInterval: watchInterval, domain: domain}
}
// Clone clones this ServiceDiscovery with new servicePath.
func (d *MDNSDiscovery) Clone(servicePath string) ServiceDiscovery {
return NewMDNSDiscovery(servicePath, d.Timeout, d.WatchInterval, d.domain)
}
// SetFilter sets the filer.
func (d *MDNSDiscovery) SetFilter(filter ServiceDiscoveryFilter) {
d.filter = filter
}
// GetServices returns the servers
func (d *MDNSDiscovery) GetServices() []*KVPair {
return d.pairs
}
// WatchService returns a nil chan.
func (d *MDNSDiscovery) WatchService() chan []*KVPair {
d.mu.Lock()
defer d.mu.Unlock()
ch := make(chan []*KVPair, 10)
d.chans = append(d.chans, ch)
return ch
}
func (d *MDNSDiscovery) RemoveWatcher(ch chan []*KVPair) {
d.mu.Lock()
defer d.mu.Unlock()
var chans []chan []*KVPair
for _, c := range d.chans {
if c == ch {
continue
}
chans = append(chans, c)
}
d.chans = chans
}
func (d *MDNSDiscovery) watch() {
t := time.NewTicker(d.WatchInterval)
for {
select {
case <-d.stopCh:
t.Stop()
log.Info("discovery has been closed")
return
case <-t.C:
pairs, err := d.browse()
if err == nil {
d.pairs = pairs
d.mu.Lock()
for _, ch := range d.chans {
ch := ch
go func() {
defer func() {
recover()
}()
select {
case ch <- pairs:
case <-time.After(time.Minute):
log.Warn("chan is full and new change has ben dropped")
}
}()
}
d.mu.Unlock()
}
}
}
}
func (d *MDNSDiscovery) browse() ([]*KVPair, error) {
resolver, err := zeroconf.NewResolver(nil)
if err != nil {
log.Warnf("Failed to initialize resolver: %v", err)
return nil, err
}
entries := make(chan *zeroconf.ServiceEntry)
var totalServices []*KVPair
var services []*serviceMeta
done := make(chan struct{})
go func(results <-chan *zeroconf.ServiceEntry) {
for entry := range entries {
s, _ := url.QueryUnescape(entry.Text[0])
err := json.Unmarshal([]byte(s), &services)
if err != nil {
log.Warnf("Failed to browse: %v", err)
continue
}
for _, sm := range services {
pair := &KVPair{
Key: sm.ServiceAddress,
Value: sm.Meta,
}
if d.filter != nil && !d.filter(pair) {
continue
}
totalServices = append(totalServices, pair)
}
}
close(done)
}(entries)
ctx, cancel := context.WithTimeout(context.Background(), d.Timeout)
defer cancel()
err = resolver.Browse(ctx, "_rpcxservices", d.domain, entries)
if err != nil {
log.Warnf("Failed to browse: %v", err)
}
<-done
return totalServices, nil
}
func (d *MDNSDiscovery) Close() {
close(d.stopCh)
}