fix(registry/consul):fix can't get service instance in async mode (#1731)

* fix(registry/consul):fix can't get service instance in async mode

* fix(registry/consul): return error if find service failed
pull/1740/head
letian 3 years ago committed by GitHub
parent 17201cd284
commit 1e749de5db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      contrib/registry/consul/registry.go

@ -144,18 +144,24 @@ func (r *Registry) Watch(ctx context.Context, name string) (registry.Watcher, er
} }
if !ok { if !ok {
go r.resolve(set) err := r.resolve(set)
if err != nil {
return nil, err
}
} }
return w, nil return w, nil
} }
func (r *Registry) resolve(ss *serviceSet) { func (r *Registry) resolve(ss *serviceSet) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
services, idx, err := r.cli.Service(ctx, ss.serviceName, 0, true) services, idx, err := r.cli.Service(ctx, ss.serviceName, 0, true)
cancel() cancel()
if err == nil && len(services) > 0 { if err != nil {
return err
} else if len(services) > 0 {
ss.broadcast(services) ss.broadcast(services)
} }
go func() {
ticker := time.NewTicker(time.Second) ticker := time.NewTicker(time.Second)
defer ticker.Stop() defer ticker.Stop()
for { for {
@ -173,4 +179,7 @@ func (r *Registry) resolve(ss *serviceSet) {
} }
idx = tmpIdx idx = tmpIdx
} }
}()
return nil
} }

Loading…
Cancel
Save