add client address option, fix noop resolver

This commit is contained in:
Adphi 2021-11-22 14:04:47 +01:00
parent c6557d12f2
commit e8f0a2f03d
5 changed files with 28 additions and 20 deletions

View File

@ -36,8 +36,14 @@ func New(opts ...Option) (Client, error) {
if !c.opts.secure {
c.opts.dialOptions = append(c.opts.dialOptions, grpc.WithInsecure())
}
if c.opts.addr == "" {
c.addr = fmt.Sprintf("%s:///%s", c.opts.registry.String(), c.opts.name)
if c.opts.version != "" {
} else if strings.HasPrefix(c.opts.addr, "tcp://"){
c.addr = strings.Replace(c.opts.addr, "tcp://", "", 1)
} else {
c.addr = c.opts.addr
}
if c.opts.version != "" && c.opts.addr == "" {
c.addr = c.addr + ":" + strings.TrimSpace(c.opts.version)
}
return c, nil

View File

@ -36,6 +36,12 @@ func WithVersion(version string) Option {
}
}
func WithAddress(address string) Option {
return func(o *options) {
o.addr = address
}
}
func WithTLSConfig(conf *tls.Config) Option {
return func(o *options) {
o.tlsConfig = conf
@ -58,6 +64,7 @@ type options struct {
registry registry.Registry
name string
version string
addr string
tlsConfig *tls.Config
secure bool
dialOptions []grpc.DialOption

View File

@ -18,7 +18,6 @@ import (
metrics2 "go.linka.cloud/grpc/interceptors/metrics"
validation2 "go.linka.cloud/grpc/interceptors/validation"
"go.linka.cloud/grpc/logger"
"go.linka.cloud/grpc/registry/mdns"
"go.linka.cloud/grpc/service"
)
@ -61,6 +60,7 @@ func httpLogger(next http.Handler) http.Handler {
func main() {
name := "greeter"
version := "v0.0.1"
secure := true
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
@ -75,9 +75,9 @@ func main() {
svc, err = service.New(
service.WithContext(ctx),
service.WithName(name),
service.WithVersion("v0.0.1"),
service.WithVersion(version),
service.WithAddress(address),
service.WithRegistry(mdns.NewRegistry()),
// service.WithRegistry(mdns.NewRegistry()),
service.WithReflection(true),
service.WithSecure(secure),
service.WithAfterStart(func() error {
@ -108,9 +108,10 @@ func main() {
}()
<-ready
s, err := client.New(
client.WithName("greeter"),
client.WithVersion("v0.0.1"),
client.WithRegistry(mdns.NewRegistry()),
// client.WithName(name),
// client.WithVersion(version),
client.WithAddress("localhost:9991"),
// client.WithRegistry(mdns.NewRegistry()),
client.WithSecure(secure),
)
if err != nil {

View File

@ -1,6 +1,8 @@
package noop
import (
"errors"
"google.golang.org/grpc/resolver"
"go.linka.cloud/grpc/registry"
@ -42,17 +44,9 @@ func (n noop) ListServices(option ...registry.ListOption) ([]*registry.Service,
}
func (n noop) Watch(option ...registry.WatchOption) (registry.Watcher, error) {
return noopWatcher{}, nil
return nil, errors.New("watch not supported")
}
func (n noop) String() string {
return "passthroug"
}
type noopWatcher struct{}
func (n noopWatcher) Next() (*registry.Result, error) {
return &registry.Result{}, nil
}
func (n noopWatcher) Stop() {}

View File

@ -34,7 +34,7 @@ type resolvr struct {
}
func (r *resolvr) run() {
if r.reg.String() == "noop" {
if r.reg.String() == "passthroug" {
return
}
var name, version string
@ -68,9 +68,9 @@ func (r *resolvr) run() {
}
// TODO(adphi): implement
switch res.Action {
case "create":
case registry.Create.String():
case "delete":
case registry.Delete.String():
}
r.cc.UpdateState(resolver.State{Addresses: r.addrs})