11// Package mdns is a multicast dns registry
2- package mdns
2+ package registry
33
44import (
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
3635type mdnsEntry struct {
@@ -39,7 +38,7 @@ type mdnsEntry struct {
3938}
4039
4140type 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
5857type 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}
0 commit comments