|
|
|
@ -44,7 +44,7 @@ type resolver struct { |
|
|
|
|
logger *log.Helper |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func newResolver(ctx context.Context, discovery registry.Discovery, target *Target, updater Updater) (*resolver, error) { |
|
|
|
|
func newResolver(ctx context.Context, discovery registry.Discovery, target *Target, updater Updater, block bool) (*resolver, error) { |
|
|
|
|
watcher, err := discovery.Watch(ctx, target.Endpoint) |
|
|
|
|
if err != nil { |
|
|
|
|
return nil, err |
|
|
|
@ -54,18 +54,26 @@ func newResolver(ctx context.Context, discovery registry.Discovery, target *Targ |
|
|
|
|
watcher: watcher, |
|
|
|
|
logger: log.NewHelper(log.DefaultLogger), |
|
|
|
|
} |
|
|
|
|
done := make(chan error, 1) |
|
|
|
|
go func() { |
|
|
|
|
for { |
|
|
|
|
var executed bool |
|
|
|
|
services, err := watcher.Next() |
|
|
|
|
if err != nil { |
|
|
|
|
r.logger.Errorf("http client watch services got unexpected error:=%v", err) |
|
|
|
|
r.logger.Errorf("http client watch service %v got unexpected error:=%v", target, err) |
|
|
|
|
if block { |
|
|
|
|
select { |
|
|
|
|
case done <- err: |
|
|
|
|
default: |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
var nodes []*registry.ServiceInstance |
|
|
|
|
for _, in := range services { |
|
|
|
|
_, endpoint, err := parseEndpoint(in.Endpoints) |
|
|
|
|
if err != nil { |
|
|
|
|
r.logger.Errorf("Failed to parse discovery endpoint: %v error %v", in.Endpoints, err) |
|
|
|
|
r.logger.Errorf("Failed to parse (%v) discovery endpoint: %v error %v", target, in.Endpoints, err) |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
if endpoint == "" { |
|
|
|
@ -79,8 +87,25 @@ func newResolver(ctx context.Context, discovery registry.Discovery, target *Targ |
|
|
|
|
r.nodes = nodes |
|
|
|
|
r.lock.Unlock() |
|
|
|
|
} |
|
|
|
|
if block && !executed { |
|
|
|
|
executed = true |
|
|
|
|
done <- nil |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}() |
|
|
|
|
if block { |
|
|
|
|
select { |
|
|
|
|
case e := <-done: |
|
|
|
|
if e != nil { |
|
|
|
|
watcher.Stop() |
|
|
|
|
} |
|
|
|
|
return r, e |
|
|
|
|
case <-ctx.Done(): |
|
|
|
|
r.logger.Errorf("http client watch service %v reaching context deadline!", target) |
|
|
|
|
watcher.Stop() |
|
|
|
|
return nil, ctx.Err() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return r, nil |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|