|
|
@ -2,6 +2,7 @@ package discovery |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"context" |
|
|
|
"context" |
|
|
|
|
|
|
|
"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" |
|
|
@ -15,14 +16,22 @@ type Option func(o *builder) |
|
|
|
|
|
|
|
|
|
|
|
// WithLogger with builder logger.
|
|
|
|
// WithLogger with builder logger.
|
|
|
|
func WithLogger(logger log.Logger) Option { |
|
|
|
func WithLogger(logger log.Logger) Option { |
|
|
|
return func(o *builder) { |
|
|
|
return func(b *builder) { |
|
|
|
o.logger = logger |
|
|
|
b.logger = logger |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// WithTimeout with timeout option.
|
|
|
|
|
|
|
|
func WithTimeout(timeout time.Duration) Option { |
|
|
|
|
|
|
|
return func(b *builder) { |
|
|
|
|
|
|
|
b.timeout = timeout |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type builder struct { |
|
|
|
type builder struct { |
|
|
|
discoverer registry.Discovery |
|
|
|
discoverer registry.Discovery |
|
|
|
logger log.Logger |
|
|
|
logger log.Logger |
|
|
|
|
|
|
|
timeout time.Duration |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// NewBuilder creates a builder which is used to factory registry resolvers.
|
|
|
|
// NewBuilder creates a builder which is used to factory registry resolvers.
|
|
|
@ -30,6 +39,7 @@ func NewBuilder(d registry.Discovery, opts ...Option) resolver.Builder { |
|
|
|
b := &builder{ |
|
|
|
b := &builder{ |
|
|
|
discoverer: d, |
|
|
|
discoverer: d, |
|
|
|
logger: log.DefaultLogger, |
|
|
|
logger: log.DefaultLogger, |
|
|
|
|
|
|
|
timeout: time.Second * 10, |
|
|
|
} |
|
|
|
} |
|
|
|
for _, o := range opts { |
|
|
|
for _, o := range opts { |
|
|
|
o(b) |
|
|
|
o(b) |
|
|
@ -37,23 +47,28 @@ func NewBuilder(d registry.Discovery, opts ...Option) resolver.Builder { |
|
|
|
return b |
|
|
|
return b |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (d *builder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) { |
|
|
|
func (b *builder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) { |
|
|
|
w, err := d.discoverer.Watch(context.Background(), target.Endpoint) |
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), b.timeout) |
|
|
|
|
|
|
|
defer cancel() |
|
|
|
|
|
|
|
w, err := b.discoverer.Watch(ctx, target.Endpoint) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
} |
|
|
|
ctx, cancel := context.WithCancel(context.Background()) |
|
|
|
|
|
|
|
r := &discoveryResolver{ |
|
|
|
r := &discoveryResolver{ |
|
|
|
w: w, |
|
|
|
w: w, |
|
|
|
cc: cc, |
|
|
|
cc: cc, |
|
|
|
ctx: ctx, |
|
|
|
ctx: ctx, |
|
|
|
cancel: cancel, |
|
|
|
cancel: cancel, |
|
|
|
log: log.NewHelper(d.logger), |
|
|
|
log: log.NewHelper(b.logger), |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
r.ctx, r.cancel = context.WithCancel(context.Background()) |
|
|
|
go r.watch() |
|
|
|
go r.watch() |
|
|
|
|
|
|
|
|
|
|
|
return r, nil |
|
|
|
return r, nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (d *builder) Scheme() string { |
|
|
|
// Scheme return scheme of discovery
|
|
|
|
|
|
|
|
func (*builder) Scheme() string { |
|
|
|
return name |
|
|
|
return name |
|
|
|
} |
|
|
|
} |
|
|
|