You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.1 KiB
56 lines
1.1 KiB
package discovery
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/go-kratos/kratos/v2/log"
|
|
"github.com/go-kratos/kratos/v2/registry"
|
|
"google.golang.org/grpc/resolver"
|
|
)
|
|
|
|
const name = "discovery"
|
|
|
|
// Option is builder option.
|
|
type Option func(o *builder)
|
|
|
|
// WithLogger with builder logger.
|
|
func WithLogger(logger log.Logger) Option {
|
|
return func(o *builder) {
|
|
o.logger = logger
|
|
}
|
|
}
|
|
|
|
type builder struct {
|
|
discoverer registry.Discoverer
|
|
logger log.Logger
|
|
}
|
|
|
|
// NewBuilder creates a builder which is used to factory registry resolvers.
|
|
func NewBuilder(d registry.Discoverer, opts ...Option) resolver.Builder {
|
|
b := &builder{
|
|
discoverer: d,
|
|
logger: log.DefaultLogger,
|
|
}
|
|
for _, o := range opts {
|
|
o(b)
|
|
}
|
|
return b
|
|
}
|
|
|
|
func (d *builder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) {
|
|
w, err := d.discoverer.Watch(context.Background(), target.Endpoint)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
r := &discoveryResolver{
|
|
w: w,
|
|
cc: cc,
|
|
log: log.NewHelper("grpc/resolver/discovery", d.logger),
|
|
}
|
|
go r.watch()
|
|
return r, nil
|
|
}
|
|
|
|
func (d *builder) Scheme() string {
|
|
return name
|
|
}
|
|
|