2019-01-18 20:29:17 +03:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/registry"
|
2019-01-18 20:29:17 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Watcher struct {
|
|
|
|
id string
|
|
|
|
wo registry.WatchOptions
|
|
|
|
res chan *registry.Result
|
|
|
|
exit chan bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Watcher) Next() (*registry.Result, error) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case r := <-m.res:
|
2020-06-25 13:02:35 +03:00
|
|
|
if r.Service == nil {
|
2019-01-18 20:29:17 +03:00
|
|
|
continue
|
|
|
|
}
|
2020-06-25 13:02:35 +03:00
|
|
|
|
|
|
|
if len(m.wo.Service) > 0 && m.wo.Service != r.Service.Name {
|
2020-06-18 14:39:19 +03:00
|
|
|
continue
|
|
|
|
}
|
2020-06-25 13:02:35 +03:00
|
|
|
|
|
|
|
// extract domain from service metadata
|
|
|
|
var domain string
|
|
|
|
if r.Service.Metadata != nil && len(r.Service.Metadata["domain"]) > 0 {
|
|
|
|
domain = r.Service.Metadata["domain"]
|
|
|
|
} else {
|
|
|
|
domain = registry.DefaultDomain
|
|
|
|
}
|
|
|
|
|
|
|
|
// only send the event if watching the wildcard or this specific domain
|
|
|
|
if m.wo.Domain == registry.WildcardDomain || m.wo.Domain == domain {
|
|
|
|
return r, nil
|
|
|
|
}
|
2019-01-18 20:29:17 +03:00
|
|
|
case <-m.exit:
|
|
|
|
return nil, errors.New("watcher stopped")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Watcher) Stop() {
|
|
|
|
select {
|
|
|
|
case <-m.exit:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
close(m.exit)
|
|
|
|
}
|
|
|
|
}
|