|
|
@ -17,14 +17,16 @@ type watcher struct { |
|
|
|
watcher clientv3.Watcher |
|
|
|
watcher clientv3.Watcher |
|
|
|
kv clientv3.KV |
|
|
|
kv clientv3.KV |
|
|
|
first bool |
|
|
|
first bool |
|
|
|
|
|
|
|
serviceName string |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func newWatcher(ctx context.Context, key string, client *clientv3.Client) (*watcher, error) { |
|
|
|
func newWatcher(ctx context.Context, key, name string, client *clientv3.Client) (*watcher, error) { |
|
|
|
w := &watcher{ |
|
|
|
w := &watcher{ |
|
|
|
key: key, |
|
|
|
key: key, |
|
|
|
watcher: clientv3.NewWatcher(client), |
|
|
|
watcher: clientv3.NewWatcher(client), |
|
|
|
kv: clientv3.NewKV(client), |
|
|
|
kv: clientv3.NewKV(client), |
|
|
|
first: true, |
|
|
|
first: true, |
|
|
|
|
|
|
|
serviceName: name, |
|
|
|
} |
|
|
|
} |
|
|
|
w.ctx, w.cancel = context.WithCancel(ctx) |
|
|
|
w.ctx, w.cancel = context.WithCancel(ctx) |
|
|
|
w.watchChan = w.watcher.Watch(w.ctx, key, clientv3.WithPrefix(), clientv3.WithRev(0)) |
|
|
|
w.watchChan = w.watcher.Watch(w.ctx, key, clientv3.WithPrefix(), clientv3.WithRev(0)) |
|
|
@ -60,13 +62,16 @@ func (w *watcher) getInstance() ([]*registry.ServiceInstance, error) { |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
items := make([]*registry.ServiceInstance, len(resp.Kvs)) |
|
|
|
items := make([]*registry.ServiceInstance, 0, len(resp.Kvs)) |
|
|
|
for i, kv := range resp.Kvs { |
|
|
|
for _, kv := range resp.Kvs { |
|
|
|
si, err := unmarshal(kv.Value) |
|
|
|
si, err := unmarshal(kv.Value) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
items[i] = si |
|
|
|
if si.Name != w.serviceName { |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
items = append(items, si) |
|
|
|
} |
|
|
|
} |
|
|
|
return items, nil |
|
|
|
return items, nil |
|
|
|
} |
|
|
|
} |
|
|
|