|
|
@ -44,7 +44,7 @@ type resolver struct { |
|
|
|
logger *log.Helper |
|
|
|
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) |
|
|
|
watcher, err := discovery.Watch(ctx, target.Endpoint) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
@ -54,18 +54,26 @@ func newResolver(ctx context.Context, discovery registry.Discovery, target *Targ |
|
|
|
watcher: watcher, |
|
|
|
watcher: watcher, |
|
|
|
logger: log.NewHelper(log.DefaultLogger), |
|
|
|
logger: log.NewHelper(log.DefaultLogger), |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
done := make(chan error, 1) |
|
|
|
go func() { |
|
|
|
go func() { |
|
|
|
for { |
|
|
|
for { |
|
|
|
|
|
|
|
var executed bool |
|
|
|
services, err := watcher.Next() |
|
|
|
services, err := watcher.Next() |
|
|
|
if err != nil { |
|
|
|
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 |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
var nodes []*registry.ServiceInstance |
|
|
|
var nodes []*registry.ServiceInstance |
|
|
|
for _, in := range services { |
|
|
|
for _, in := range services { |
|
|
|
_, endpoint, err := parseEndpoint(in.Endpoints) |
|
|
|
_, endpoint, err := parseEndpoint(in.Endpoints) |
|
|
|
if err != nil { |
|
|
|
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 |
|
|
|
continue |
|
|
|
} |
|
|
|
} |
|
|
|
if endpoint == "" { |
|
|
|
if endpoint == "" { |
|
|
@ -79,8 +87,25 @@ func newResolver(ctx context.Context, discovery registry.Discovery, target *Targ |
|
|
|
r.nodes = nodes |
|
|
|
r.nodes = nodes |
|
|
|
r.lock.Unlock() |
|
|
|
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 |
|
|
|
return r, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|