Skip to content

Commit 1fe2638

Browse files
authored
Implement MDNS Registry (#2767)
- Removed existing mDNS test file and replaced it with a new implementation. - Added a new `mdns_registry.go` file that contains the MDNS registry logic. - Updated the default registry to use the new MDNS registry. - Refactored tests to accommodate the new MDNS registry implementation. - Implemented service registration, deregistration, and service discovery using mDNS. - Added encoding and decoding functions for mDNS TXT records. - Implemented a watcher for monitoring service changes in the MDNS registry.
1 parent 97275d3 commit 1fe2638

File tree

5 files changed

+49
-57
lines changed

5 files changed

+49
-57
lines changed

cmd/cmd.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"go-micro.dev/v5/registry"
2626
"go-micro.dev/v5/registry/consul"
2727
"go-micro.dev/v5/registry/etcd"
28-
"go-micro.dev/v5/registry/mdns"
2928
"go-micro.dev/v5/registry/nats"
3029
"go-micro.dev/v5/selector"
3130
"go-micro.dev/v5/server"
@@ -249,7 +248,7 @@ var (
249248
"consul": consul.NewConsulRegistry,
250249
"memory": registry.NewMemoryRegistry,
251250
"nats": nats.NewNatsRegistry,
252-
"mdns": mdns.NewMDNSRegistry,
251+
"mdns": registry.NewMDNSRegistry,
253252
"etcd": etcd.NewEtcdRegistry,
254253
}
255254

registry/mdns/mdns.go

Lines changed: 0 additions & 5 deletions
This file was deleted.

registry/mdns/mdns_registry.go renamed to registry/mdns_registry.go

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Package mdns is a multicast dns registry
2-
package mdns
2+
package registry
33

44
import (
55
"bytes"
@@ -17,7 +17,6 @@ import (
1717

1818
"github.com/google/uuid"
1919
log "go-micro.dev/v5/logger"
20-
"go-micro.dev/v5/registry"
2120
"go-micro.dev/v5/util/mdns"
2221
)
2322

@@ -30,7 +29,7 @@ type mdnsTxt struct {
3029
Metadata map[string]string
3130
Service string
3231
Version string
33-
Endpoints []*registry.Endpoint
32+
Endpoints []*Endpoint
3433
}
3534

3635
type mdnsEntry struct {
@@ -39,7 +38,7 @@ type mdnsEntry struct {
3938
}
4039

4140
type mdnsRegistry struct {
42-
opts *registry.Options
41+
opts *Options
4342
services map[string][]*mdnsEntry
4443

4544
// watchers
@@ -56,7 +55,7 @@ type mdnsRegistry struct {
5655
}
5756

5857
type mdnsWatcher struct {
59-
wo registry.WatchOptions
58+
wo WatchOptions
6059
ch chan *mdns.ServiceEntry
6160
exit chan struct{}
6261
// the registry
@@ -128,9 +127,9 @@ func decode(record []string) (*mdnsTxt, error) {
128127

129128
return txt, nil
130129
}
131-
func newRegistry(opts ...registry.Option) registry.Registry {
132-
mergedOpts := append([]registry.Option{registry.Timeout(time.Millisecond * 100)}, opts...)
133-
options := registry.NewOptions(mergedOpts...)
130+
func newRegistry(opts ...Option) Registry {
131+
mergedOpts := append([]Option{Timeout(time.Millisecond * 100)}, opts...)
132+
options := NewOptions(mergedOpts...)
134133

135134
// set the domain
136135
domain := mdnsDomain
@@ -148,18 +147,18 @@ func newRegistry(opts ...registry.Option) registry.Registry {
148147
}
149148
}
150149

151-
func (m *mdnsRegistry) Init(opts ...registry.Option) error {
150+
func (m *mdnsRegistry) Init(opts ...Option) error {
152151
for _, o := range opts {
153152
o(m.opts)
154153
}
155154
return nil
156155
}
157156

158-
func (m *mdnsRegistry) Options() registry.Options {
157+
func (m *mdnsRegistry) Options() Options {
159158
return *m.opts
160159
}
161160

162-
func (m *mdnsRegistry) Register(service *registry.Service, opts ...registry.RegisterOption) error {
161+
func (m *mdnsRegistry) Register(service *Service, opts ...RegisterOption) error {
163162
m.Lock()
164163
defer m.Unlock()
165164

@@ -264,7 +263,7 @@ func (m *mdnsRegistry) Register(service *registry.Service, opts ...registry.Regi
264263
return gerr
265264
}
266265

267-
func (m *mdnsRegistry) Deregister(service *registry.Service, opts ...registry.DeregisterOption) error {
266+
func (m *mdnsRegistry) Deregister(service *Service, opts ...DeregisterOption) error {
268267
m.Lock()
269268
defer m.Unlock()
270269

@@ -299,9 +298,9 @@ func (m *mdnsRegistry) Deregister(service *registry.Service, opts ...registry.De
299298
return nil
300299
}
301300

302-
func (m *mdnsRegistry) GetService(service string, opts ...registry.GetOption) ([]*registry.Service, error) {
301+
func (m *mdnsRegistry) GetService(service string, opts ...GetOption) ([]*Service, error) {
303302
logger := m.opts.Logger
304-
serviceMap := make(map[string]*registry.Service)
303+
serviceMap := make(map[string]*Service)
305304
entries := make(chan *mdns.ServiceEntry, 10)
306305
done := make(chan bool)
307306

@@ -341,7 +340,7 @@ func (m *mdnsRegistry) GetService(service string, opts ...registry.GetOption) ([
341340

342341
s, ok := serviceMap[txt.Version]
343342
if !ok {
344-
s = &registry.Service{
343+
s = &Service{
345344
Name: txt.Service,
346345
Version: txt.Version,
347346
Endpoints: txt.Endpoints,
@@ -358,7 +357,7 @@ func (m *mdnsRegistry) GetService(service string, opts ...registry.GetOption) ([
358357
logger.Logf(log.InfoLevel, "[mdns]: invalid endpoint received: %v", e)
359358
continue
360359
}
361-
s.Nodes = append(s.Nodes, &registry.Node{
360+
s.Nodes = append(s.Nodes, &Node{
362361
Id: strings.TrimSuffix(e.Name, "."+p.Service+"."+p.Domain+"."),
363362
Address: addr,
364363
Metadata: txt.Metadata,
@@ -381,7 +380,7 @@ func (m *mdnsRegistry) GetService(service string, opts ...registry.GetOption) ([
381380
<-done
382381

383382
// create list and return
384-
services := make([]*registry.Service, 0, len(serviceMap))
383+
services := make([]*Service, 0, len(serviceMap))
385384

386385
for _, service := range serviceMap {
387386
services = append(services, service)
@@ -390,7 +389,7 @@ func (m *mdnsRegistry) GetService(service string, opts ...registry.GetOption) ([
390389
return services, nil
391390
}
392391

393-
func (m *mdnsRegistry) ListServices(opts ...registry.ListOption) ([]*registry.Service, error) {
392+
func (m *mdnsRegistry) ListServices(opts ...ListOption) ([]*Service, error) {
394393
serviceMap := make(map[string]bool)
395394
entries := make(chan *mdns.ServiceEntry, 10)
396395
done := make(chan bool)
@@ -405,7 +404,7 @@ func (m *mdnsRegistry) ListServices(opts ...registry.ListOption) ([]*registry.Se
405404
// set domain
406405
p.Domain = m.domain
407406

408-
var services []*registry.Service
407+
var services []*Service
409408

410409
go func() {
411410
for {
@@ -420,7 +419,7 @@ func (m *mdnsRegistry) ListServices(opts ...registry.ListOption) ([]*registry.Se
420419
name := strings.TrimSuffix(e.Name, "."+p.Service+"."+p.Domain+".")
421420
if !serviceMap[name] {
422421
serviceMap[name] = true
423-
services = append(services, &registry.Service{Name: name})
422+
services = append(services, &Service{Name: name})
424423
}
425424
case <-p.Context.Done():
426425
close(done)
@@ -440,8 +439,8 @@ func (m *mdnsRegistry) ListServices(opts ...registry.ListOption) ([]*registry.Se
440439
return services, nil
441440
}
442441

443-
func (m *mdnsRegistry) Watch(opts ...registry.WatchOption) (registry.Watcher, error) {
444-
var wo registry.WatchOptions
442+
func (m *mdnsRegistry) Watch(opts ...WatchOption) (Watcher, error) {
443+
var wo WatchOptions
445444
for _, o := range opts {
446445
o(&wo)
447446
}
@@ -538,7 +537,7 @@ func (m *mdnsRegistry) String() string {
538537
return "mdns"
539538
}
540539

541-
func (m *mdnsWatcher) Next() (*registry.Result, error) {
540+
func (m *mdnsWatcher) Next() (*Result, error) {
542541
for {
543542
select {
544543
case e := <-m.ch:
@@ -563,7 +562,7 @@ func (m *mdnsWatcher) Next() (*registry.Result, error) {
563562
action = "create"
564563
}
565564

566-
service := &registry.Service{
565+
service := &Service{
567566
Name: txt.Service,
568567
Version: txt.Version,
569568
Endpoints: txt.Endpoints,
@@ -584,18 +583,18 @@ func (m *mdnsWatcher) Next() (*registry.Result, error) {
584583
addr = e.Addr.String()
585584
}
586585

587-
service.Nodes = append(service.Nodes, &registry.Node{
586+
service.Nodes = append(service.Nodes, &Node{
588587
Id: strings.TrimSuffix(e.Name, suffix),
589588
Address: addr,
590589
Metadata: txt.Metadata,
591590
})
592591

593-
return &registry.Result{
592+
return &Result{
594593
Action: action,
595594
Service: service,
596595
}, nil
597596
case <-m.exit:
598-
return nil, registry.ErrWatcherStopped
597+
return nil, ErrWatcherStopped
599598
}
600599
}
601600
}
@@ -607,13 +606,14 @@ func (m *mdnsWatcher) Stop() {
607606
default:
608607
close(m.exit)
609608
// remove self from the registry
609+
610610
m.registry.mtx.Lock()
611611
delete(m.registry.watchers, m.id)
612612
m.registry.mtx.Unlock()
613613
}
614614
}
615615

616616
// NewRegistry returns a new default registry which is mdns.
617-
func NewMDNSRegistry(opts ...registry.Option) registry.Registry {
617+
func NewMDNSRegistry(opts ...Option) Registry {
618618
return newRegistry(opts...)
619619
}

registry/mdns/mdns_test.go renamed to registry/mdns_test.go

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
package mdns
1+
package registry
22

33
import (
44
"os"
55
"testing"
66
"time"
7-
8-
"go-micro.dev/v5/registry"
97
)
108

119
func TestMDNS(t *testing.T) {
@@ -14,11 +12,11 @@ func TestMDNS(t *testing.T) {
1412
t.Skip()
1513
}
1614

17-
testData := []*registry.Service{
15+
testData := []*Service{
1816
{
1917
Name: "test1",
2018
Version: "1.0.1",
21-
Nodes: []*registry.Node{
19+
Nodes: []*Node{
2220
{
2321
Id: "test1-1",
2422
Address: "10.0.0.1:10001",
@@ -31,7 +29,7 @@ func TestMDNS(t *testing.T) {
3129
{
3230
Name: "test2",
3331
Version: "1.0.2",
34-
Nodes: []*registry.Node{
32+
Nodes: []*Node{
3533
{
3634
Id: "test2-1",
3735
Address: "10.0.0.2:10002",
@@ -44,7 +42,7 @@ func TestMDNS(t *testing.T) {
4442
{
4543
Name: "test3",
4644
Version: "1.0.3",
47-
Nodes: []*registry.Node{
45+
Nodes: []*Node{
4846
{
4947
Id: "test3-1",
5048
Address: "10.0.0.3:10003",
@@ -57,7 +55,7 @@ func TestMDNS(t *testing.T) {
5755
{
5856
Name: "test4",
5957
Version: "1.0.4",
60-
Nodes: []*registry.Node{
58+
Nodes: []*Node{
6159
{
6260
Id: "test4-1",
6361
Address: "[::]:10004",
@@ -71,10 +69,10 @@ func TestMDNS(t *testing.T) {
7169

7270
travis := os.Getenv("TRAVIS")
7371

74-
var opts []registry.Option
72+
var opts []Option
7573

7674
if travis == "true" {
77-
opts = append(opts, registry.Timeout(time.Millisecond*100))
75+
opts = append(opts, Timeout(time.Millisecond*100))
7876
}
7977

8078
// new registry
@@ -158,14 +156,14 @@ func TestEncoding(t *testing.T) {
158156
Metadata: map[string]string{
159157
"foo": "bar",
160158
},
161-
Endpoints: []*registry.Endpoint{
159+
Endpoints: []*Endpoint{
162160
{
163161
Name: "endpoint1",
164-
Request: &registry.Value{
162+
Request: &Value{
165163
Name: "request",
166164
Type: "request",
167165
},
168-
Response: &registry.Value{
166+
Response: &Value{
169167
Name: "response",
170168
Type: "response",
171169
},
@@ -215,11 +213,11 @@ func TestWatcher(t *testing.T) {
215213
t.Skip()
216214
}
217215

218-
testData := []*registry.Service{
216+
testData := []*Service{
219217
{
220218
Name: "test1",
221219
Version: "1.0.1",
222-
Nodes: []*registry.Node{
220+
Nodes: []*Node{
223221
{
224222
Id: "test1-1",
225223
Address: "10.0.0.1:10001",
@@ -232,7 +230,7 @@ func TestWatcher(t *testing.T) {
232230
{
233231
Name: "test2",
234232
Version: "1.0.2",
235-
Nodes: []*registry.Node{
233+
Nodes: []*Node{
236234
{
237235
Id: "test2-1",
238236
Address: "10.0.0.2:10002",
@@ -245,7 +243,7 @@ func TestWatcher(t *testing.T) {
245243
{
246244
Name: "test3",
247245
Version: "1.0.3",
248-
Nodes: []*registry.Node{
246+
Nodes: []*Node{
249247
{
250248
Id: "test3-1",
251249
Address: "10.0.0.3:10003",
@@ -258,7 +256,7 @@ func TestWatcher(t *testing.T) {
258256
{
259257
Name: "test4",
260258
Version: "1.0.4",
261-
Nodes: []*registry.Node{
259+
Nodes: []*Node{
262260
{
263261
Id: "test4-1",
264262
Address: "[::]:10004",
@@ -270,7 +268,7 @@ func TestWatcher(t *testing.T) {
270268
},
271269
}
272270

273-
testFn := func(service, s *registry.Service) {
271+
testFn := func(service, s *Service) {
274272
if s == nil {
275273
t.Fatalf("Expected one result for %s got nil", service.Name)
276274
}
@@ -300,10 +298,10 @@ func TestWatcher(t *testing.T) {
300298

301299
travis := os.Getenv("TRAVIS")
302300

303-
var opts []registry.Option
301+
var opts []Option
304302

305303
if travis == "true" {
306-
opts = append(opts, registry.Timeout(time.Millisecond*100))
304+
opts = append(opts, Timeout(time.Millisecond*100))
307305
}
308306

309307
// new registry

registry/registry.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,5 @@ func String() string {
9595
}
9696

9797
var (
98-
DefaultRegistry = NewMemoryRegistry()
98+
DefaultRegistry = NewMDNSRegistry()
9999
)

0 commit comments

Comments
 (0)