|
|
@ -4,6 +4,7 @@ import ( |
|
|
|
"context" |
|
|
|
"context" |
|
|
|
"net/url" |
|
|
|
"net/url" |
|
|
|
"sync" |
|
|
|
"sync" |
|
|
|
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/go-kratos/kratos/v2/log" |
|
|
|
"github.com/go-kratos/kratos/v2/log" |
|
|
|
"github.com/go-kratos/kratos/v2/registry" |
|
|
|
"github.com/go-kratos/kratos/v2/registry" |
|
|
@ -38,6 +39,7 @@ func parseTarget(endpoint string) (*Target, error) { |
|
|
|
type resolver struct { |
|
|
|
type resolver struct { |
|
|
|
lock sync.RWMutex |
|
|
|
lock sync.RWMutex |
|
|
|
nodes []*registry.ServiceInstance |
|
|
|
nodes []*registry.ServiceInstance |
|
|
|
|
|
|
|
updater Updater |
|
|
|
|
|
|
|
|
|
|
|
target *Target |
|
|
|
target *Target |
|
|
|
watcher registry.Watcher |
|
|
|
watcher registry.Watcher |
|
|
@ -53,27 +55,58 @@ func newResolver(ctx context.Context, discovery registry.Discovery, target *Targ |
|
|
|
target: target, |
|
|
|
target: target, |
|
|
|
watcher: watcher, |
|
|
|
watcher: watcher, |
|
|
|
logger: log.NewHelper(log.DefaultLogger), |
|
|
|
logger: log.NewHelper(log.DefaultLogger), |
|
|
|
|
|
|
|
updater: updater, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if block { |
|
|
|
done := make(chan error, 1) |
|
|
|
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 service %v got unexpected error:=%v", target, err) |
|
|
|
done <- err |
|
|
|
if block { |
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
r.update(services) |
|
|
|
|
|
|
|
if len(r.nodes) > 0 { |
|
|
|
|
|
|
|
done <- nil |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}() |
|
|
|
select { |
|
|
|
select { |
|
|
|
case done <- err: |
|
|
|
case err := <-done: |
|
|
|
default: |
|
|
|
if err != nil { |
|
|
|
|
|
|
|
watcher.Stop() |
|
|
|
|
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
case <-ctx.Done(): |
|
|
|
|
|
|
|
r.logger.Errorf("http client watch service %v reaching context deadline!", target) |
|
|
|
|
|
|
|
watcher.Stop() |
|
|
|
|
|
|
|
return nil, ctx.Err() |
|
|
|
} |
|
|
|
} |
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
go func() { |
|
|
|
|
|
|
|
for { |
|
|
|
|
|
|
|
services, err := watcher.Next() |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
r.logger.Errorf("http client watch service %v got unexpected error:=%v", target, err) |
|
|
|
|
|
|
|
time.Sleep(time.Second) |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
r.update(services) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return r, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (r *resolver) update(services []*registry.ServiceInstance) { |
|
|
|
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 (%v) discovery endpoint: %v error %v", target, in.Endpoints, err) |
|
|
|
r.logger.Errorf("Failed to parse (%v) discovery endpoint: %v error %v", r.target, in.Endpoints, err) |
|
|
|
continue |
|
|
|
continue |
|
|
|
} |
|
|
|
} |
|
|
|
if endpoint == "" { |
|
|
|
if endpoint == "" { |
|
|
@ -82,40 +115,13 @@ func newResolver(ctx context.Context, discovery registry.Discovery, target *Targ |
|
|
|
nodes = append(nodes, in) |
|
|
|
nodes = append(nodes, in) |
|
|
|
} |
|
|
|
} |
|
|
|
if len(nodes) != 0 { |
|
|
|
if len(nodes) != 0 { |
|
|
|
updater.Update(nodes) |
|
|
|
r.updater.Update(nodes) |
|
|
|
r.lock.Lock() |
|
|
|
r.lock.Lock() |
|
|
|
r.nodes = nodes |
|
|
|
r.nodes = nodes |
|
|
|
r.lock.Unlock() |
|
|
|
r.lock.Unlock() |
|
|
|
if block && !executed { |
|
|
|
|
|
|
|
executed = true |
|
|
|
|
|
|
|
done <- nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
r.logger.Warnf("[http resovler]Zero endpoint found,refused to write,ser: %s ins: %v", target.Endpoint, nodes) |
|
|
|
r.logger.Warnf("[http resovler]Zero endpoint found,refused to write,ser: %s ins: %v", r.target.Endpoint, nodes) |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}() |
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (r *resolver) fetch(ctx context.Context) []*registry.ServiceInstance { |
|
|
|
|
|
|
|
r.lock.RLock() |
|
|
|
|
|
|
|
nodes := r.nodes |
|
|
|
|
|
|
|
r.lock.RUnlock() |
|
|
|
|
|
|
|
return nodes |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func parseEndpoint(endpoints []string) (string, string, error) { |
|
|
|
func parseEndpoint(endpoints []string) (string, string, error) { |
|
|
|