1
1
// Package mdns is a multicast dns registry
2
- package mdns
2
+ package registry
3
3
4
4
import (
5
5
"bytes"
@@ -17,7 +17,6 @@ import (
17
17
18
18
"github.com/google/uuid"
19
19
log "go-micro.dev/v5/logger"
20
- "go-micro.dev/v5/registry"
21
20
"go-micro.dev/v5/util/mdns"
22
21
)
23
22
@@ -30,7 +29,7 @@ type mdnsTxt struct {
30
29
Metadata map [string ]string
31
30
Service string
32
31
Version string
33
- Endpoints []* registry. Endpoint
32
+ Endpoints []* Endpoint
34
33
}
35
34
36
35
type mdnsEntry struct {
@@ -39,7 +38,7 @@ type mdnsEntry struct {
39
38
}
40
39
41
40
type mdnsRegistry struct {
42
- opts * registry. Options
41
+ opts * Options
43
42
services map [string ][]* mdnsEntry
44
43
45
44
// watchers
@@ -56,7 +55,7 @@ type mdnsRegistry struct {
56
55
}
57
56
58
57
type mdnsWatcher struct {
59
- wo registry. WatchOptions
58
+ wo WatchOptions
60
59
ch chan * mdns.ServiceEntry
61
60
exit chan struct {}
62
61
// the registry
@@ -128,9 +127,9 @@ func decode(record []string) (*mdnsTxt, error) {
128
127
129
128
return txt , nil
130
129
}
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 ... )
134
133
135
134
// set the domain
136
135
domain := mdnsDomain
@@ -148,18 +147,18 @@ func newRegistry(opts ...registry.Option) registry.Registry {
148
147
}
149
148
}
150
149
151
- func (m * mdnsRegistry ) Init (opts ... registry. Option ) error {
150
+ func (m * mdnsRegistry ) Init (opts ... Option ) error {
152
151
for _ , o := range opts {
153
152
o (m .opts )
154
153
}
155
154
return nil
156
155
}
157
156
158
- func (m * mdnsRegistry ) Options () registry. Options {
157
+ func (m * mdnsRegistry ) Options () Options {
159
158
return * m .opts
160
159
}
161
160
162
- func (m * mdnsRegistry ) Register (service * registry. Service , opts ... registry .RegisterOption ) error {
161
+ func (m * mdnsRegistry ) Register (service * Service , opts ... RegisterOption ) error {
163
162
m .Lock ()
164
163
defer m .Unlock ()
165
164
@@ -264,7 +263,7 @@ func (m *mdnsRegistry) Register(service *registry.Service, opts ...registry.Regi
264
263
return gerr
265
264
}
266
265
267
- func (m * mdnsRegistry ) Deregister (service * registry. Service , opts ... registry .DeregisterOption ) error {
266
+ func (m * mdnsRegistry ) Deregister (service * Service , opts ... DeregisterOption ) error {
268
267
m .Lock ()
269
268
defer m .Unlock ()
270
269
@@ -299,9 +298,9 @@ func (m *mdnsRegistry) Deregister(service *registry.Service, opts ...registry.De
299
298
return nil
300
299
}
301
300
302
- func (m * mdnsRegistry ) GetService (service string , opts ... registry. GetOption ) ([]* registry. Service , error ) {
301
+ func (m * mdnsRegistry ) GetService (service string , opts ... GetOption ) ([]* Service , error ) {
303
302
logger := m .opts .Logger
304
- serviceMap := make (map [string ]* registry. Service )
303
+ serviceMap := make (map [string ]* Service )
305
304
entries := make (chan * mdns.ServiceEntry , 10 )
306
305
done := make (chan bool )
307
306
@@ -341,7 +340,7 @@ func (m *mdnsRegistry) GetService(service string, opts ...registry.GetOption) ([
341
340
342
341
s , ok := serviceMap [txt .Version ]
343
342
if ! ok {
344
- s = & registry. Service {
343
+ s = & Service {
345
344
Name : txt .Service ,
346
345
Version : txt .Version ,
347
346
Endpoints : txt .Endpoints ,
@@ -358,7 +357,7 @@ func (m *mdnsRegistry) GetService(service string, opts ...registry.GetOption) ([
358
357
logger .Logf (log .InfoLevel , "[mdns]: invalid endpoint received: %v" , e )
359
358
continue
360
359
}
361
- s .Nodes = append (s .Nodes , & registry. Node {
360
+ s .Nodes = append (s .Nodes , & Node {
362
361
Id : strings .TrimSuffix (e .Name , "." + p .Service + "." + p .Domain + "." ),
363
362
Address : addr ,
364
363
Metadata : txt .Metadata ,
@@ -381,7 +380,7 @@ func (m *mdnsRegistry) GetService(service string, opts ...registry.GetOption) ([
381
380
<- done
382
381
383
382
// create list and return
384
- services := make ([]* registry. Service , 0 , len (serviceMap ))
383
+ services := make ([]* Service , 0 , len (serviceMap ))
385
384
386
385
for _ , service := range serviceMap {
387
386
services = append (services , service )
@@ -390,7 +389,7 @@ func (m *mdnsRegistry) GetService(service string, opts ...registry.GetOption) ([
390
389
return services , nil
391
390
}
392
391
393
- func (m * mdnsRegistry ) ListServices (opts ... registry. ListOption ) ([]* registry. Service , error ) {
392
+ func (m * mdnsRegistry ) ListServices (opts ... ListOption ) ([]* Service , error ) {
394
393
serviceMap := make (map [string ]bool )
395
394
entries := make (chan * mdns.ServiceEntry , 10 )
396
395
done := make (chan bool )
@@ -405,7 +404,7 @@ func (m *mdnsRegistry) ListServices(opts ...registry.ListOption) ([]*registry.Se
405
404
// set domain
406
405
p .Domain = m .domain
407
406
408
- var services []* registry. Service
407
+ var services []* Service
409
408
410
409
go func () {
411
410
for {
@@ -420,7 +419,7 @@ func (m *mdnsRegistry) ListServices(opts ...registry.ListOption) ([]*registry.Se
420
419
name := strings .TrimSuffix (e .Name , "." + p .Service + "." + p .Domain + "." )
421
420
if ! serviceMap [name ] {
422
421
serviceMap [name ] = true
423
- services = append (services , & registry. Service {Name : name })
422
+ services = append (services , & Service {Name : name })
424
423
}
425
424
case <- p .Context .Done ():
426
425
close (done )
@@ -440,8 +439,8 @@ func (m *mdnsRegistry) ListServices(opts ...registry.ListOption) ([]*registry.Se
440
439
return services , nil
441
440
}
442
441
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
445
444
for _ , o := range opts {
446
445
o (& wo )
447
446
}
@@ -538,7 +537,7 @@ func (m *mdnsRegistry) String() string {
538
537
return "mdns"
539
538
}
540
539
541
- func (m * mdnsWatcher ) Next () (* registry. Result , error ) {
540
+ func (m * mdnsWatcher ) Next () (* Result , error ) {
542
541
for {
543
542
select {
544
543
case e := <- m .ch :
@@ -563,7 +562,7 @@ func (m *mdnsWatcher) Next() (*registry.Result, error) {
563
562
action = "create"
564
563
}
565
564
566
- service := & registry. Service {
565
+ service := & Service {
567
566
Name : txt .Service ,
568
567
Version : txt .Version ,
569
568
Endpoints : txt .Endpoints ,
@@ -584,18 +583,18 @@ func (m *mdnsWatcher) Next() (*registry.Result, error) {
584
583
addr = e .Addr .String ()
585
584
}
586
585
587
- service .Nodes = append (service .Nodes , & registry. Node {
586
+ service .Nodes = append (service .Nodes , & Node {
588
587
Id : strings .TrimSuffix (e .Name , suffix ),
589
588
Address : addr ,
590
589
Metadata : txt .Metadata ,
591
590
})
592
591
593
- return & registry. Result {
592
+ return & Result {
594
593
Action : action ,
595
594
Service : service ,
596
595
}, nil
597
596
case <- m .exit :
598
- return nil , registry . ErrWatcherStopped
597
+ return nil , ErrWatcherStopped
599
598
}
600
599
}
601
600
}
@@ -607,13 +606,14 @@ func (m *mdnsWatcher) Stop() {
607
606
default :
608
607
close (m .exit )
609
608
// remove self from the registry
609
+
610
610
m .registry .mtx .Lock ()
611
611
delete (m .registry .watchers , m .id )
612
612
m .registry .mtx .Unlock ()
613
613
}
614
614
}
615
615
616
616
// NewRegistry returns a new default registry which is mdns.
617
- func NewMDNSRegistry (opts ... registry. Option ) registry. Registry {
617
+ func NewMDNSRegistry (opts ... Option ) Registry {
618
618
return newRegistry (opts ... )
619
619
}
0 commit comments