You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
673 B
42 lines
673 B
3 years ago
|
package registry
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
|
||
|
"github.com/go-kratos/kratos/v2/registry"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
_ registry.Watcher = &watcher{}
|
||
|
)
|
||
|
|
||
|
type watcher struct {
|
||
|
ctx context.Context
|
||
|
cancel context.CancelFunc
|
||
|
event chan struct{}
|
||
|
set *serviceSet
|
||
|
}
|
||
|
|
||
|
func (w watcher) Next() (services []*registry.ServiceInstance, err error) {
|
||
|
select {
|
||
|
case <-w.ctx.Done():
|
||
|
err = w.ctx.Err()
|
||
|
case <-w.event:
|
||
|
}
|
||
|
ss, ok := w.set.services.Load().([]*registry.ServiceInstance)
|
||
|
if ok {
|
||
|
for _, s := range ss {
|
||
|
services = append(services, s)
|
||
|
}
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func (w *watcher) Stop() error {
|
||
|
w.cancel()
|
||
|
w.set.lock.Lock()
|
||
|
defer w.set.lock.Unlock()
|
||
|
delete(w.set.watcher, w)
|
||
|
return nil
|
||
|
}
|