mirror of
https://github.com/go-kratos/kratos.git
synced 2025-11-06 08:59:18 +02:00
refactor: move plugins to contrib dir (#1399)
* move to contrib dir Co-authored-by: chenzhihui <zhihui_chen@foxmail.com>
This commit is contained in:
41
contrib/registry/zookeeper/watcher.go
Normal file
41
contrib/registry/zookeeper/watcher.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package zookeeper
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user