mirror of
https://github.com/go-kratos/kratos.git
synced 2025-01-20 03:29:45 +02:00
fix http resovler bug (#1231)
* fix http resovler bug * delete unused code
This commit is contained in:
parent
38c9def445
commit
dca963a236
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"net/url"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-kratos/kratos/v2/log"
|
||||
"github.com/go-kratos/kratos/v2/registry"
|
||||
@ -36,8 +37,9 @@ func parseTarget(endpoint string) (*Target, error) {
|
||||
}
|
||||
|
||||
type resolver struct {
|
||||
lock sync.RWMutex
|
||||
nodes []*registry.ServiceInstance
|
||||
lock sync.RWMutex
|
||||
nodes []*registry.ServiceInstance
|
||||
updater Updater
|
||||
|
||||
target *Target
|
||||
watcher registry.Watcher
|
||||
@ -53,69 +55,73 @@ func newResolver(ctx context.Context, discovery registry.Discovery, target *Targ
|
||||
target: target,
|
||||
watcher: watcher,
|
||||
logger: log.NewHelper(log.DefaultLogger),
|
||||
updater: updater,
|
||||
}
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
for {
|
||||
var executed bool
|
||||
services, err := watcher.Next()
|
||||
if err != nil {
|
||||
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 (%v) discovery endpoint: %v error %v", target, in.Endpoints, err)
|
||||
continue
|
||||
}
|
||||
if endpoint == "" {
|
||||
continue
|
||||
}
|
||||
nodes = append(nodes, in)
|
||||
}
|
||||
if len(nodes) != 0 {
|
||||
updater.Update(nodes)
|
||||
r.lock.Lock()
|
||||
r.nodes = nodes
|
||||
r.lock.Unlock()
|
||||
if block && !executed {
|
||||
executed = true
|
||||
done <- nil
|
||||
}
|
||||
} else {
|
||||
r.logger.Warnf("[http resovler]Zero endpoint found,refused to write,ser: %s ins: %v", target.Endpoint, nodes)
|
||||
}
|
||||
}
|
||||
}()
|
||||
if block {
|
||||
select {
|
||||
case e := <-done:
|
||||
if e != nil {
|
||||
watcher.Stop()
|
||||
done := make(chan error, 1)
|
||||
go func() {
|
||||
for {
|
||||
services, err := watcher.Next()
|
||||
if err != nil {
|
||||
done <- err
|
||||
return
|
||||
}
|
||||
r.update(services)
|
||||
if len(r.nodes) > 0 {
|
||||
done <- nil
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
select {
|
||||
case err := <-done:
|
||||
if err != nil {
|
||||
watcher.Stop()
|
||||
return nil, err
|
||||
}
|
||||
return r, e
|
||||
case <-ctx.Done():
|
||||
r.logger.Errorf("http client watch service %v reaching context deadline!", target)
|
||||
watcher.Stop()
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
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) fetch(ctx context.Context) []*registry.ServiceInstance {
|
||||
r.lock.RLock()
|
||||
nodes := r.nodes
|
||||
r.lock.RUnlock()
|
||||
return nodes
|
||||
func (r *resolver) update(services []*registry.ServiceInstance) {
|
||||
var nodes []*registry.ServiceInstance
|
||||
for _, in := range services {
|
||||
_, endpoint, err := parseEndpoint(in.Endpoints)
|
||||
if err != nil {
|
||||
r.logger.Errorf("Failed to parse (%v) discovery endpoint: %v error %v", r.target, in.Endpoints, err)
|
||||
continue
|
||||
}
|
||||
if endpoint == "" {
|
||||
continue
|
||||
}
|
||||
nodes = append(nodes, in)
|
||||
}
|
||||
if len(nodes) != 0 {
|
||||
r.updater.Update(nodes)
|
||||
r.lock.Lock()
|
||||
r.nodes = nodes
|
||||
r.lock.Unlock()
|
||||
} else {
|
||||
r.logger.Warnf("[http resovler]Zero endpoint found,refused to write,ser: %s ins: %v", r.target.Endpoint, nodes)
|
||||
}
|
||||
}
|
||||
|
||||
func parseEndpoint(endpoints []string) (string, string, error) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user