From cf9017740929a60cc0087ca06ad590721f8df6f0 Mon Sep 17 00:00:00 2001 From: lesismal Date: Mon, 25 Apr 2022 18:01:34 +0800 Subject: [PATCH] micro: discovery wait watch --- extension/micro/etcd/discovery.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/extension/micro/etcd/discovery.go b/extension/micro/etcd/discovery.go index 8123eae..12349d9 100644 --- a/extension/micro/etcd/discovery.go +++ b/extension/micro/etcd/discovery.go @@ -18,15 +18,17 @@ type Discovery struct { } func (ds *Discovery) init(done chan struct{}) { + doneWatch := make(chan struct{}) go util.Safe(func() { - ds.lazyInit(done) + ds.lazyInit(done, doneWatch) }) - ds.watch() + ds.watch(doneWatch) } -func (ds *Discovery) lazyInit(done chan struct{}) { +func (ds *Discovery) lazyInit(done, doneWatch chan struct{}) { defer close(done) + <-doneWatch time.Sleep(time.Second / 100) resp, err := ds.client.Get(context.Background(), ds.prefix, clientv3.WithPrefix()) if err != nil { @@ -40,8 +42,9 @@ func (ds *Discovery) lazyInit(done chan struct{}) { } } -func (ds *Discovery) watch() { +func (ds *Discovery) watch(doneWatch chan struct{}) { rch := ds.client.Watch(context.Background(), ds.prefix, clientv3.WithPrefix()) + close(doneWatch) log.Info("Discovery watching: %s", ds.prefix) for wresp := range rch { for _, ev := range wresp.Events {